Implementation idea: Use GestureDetector to wrap the outermost MaterialApp, listen for onTap click event, and use FocusSocpe related API to control the display and hiding of software. Hide the keyboard when it is displayed.

To encapsulate the Widget

/// Since there is no need to hold state, the StatelessWidget is inherited here
class HideKeyboard extends StatelessWidget {
  final Widget child;

  const HideKeyboard({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: child,
      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if(! currentFocus.hasPrimaryFocus && currentFocus.focusedChild ! =null) {
          /// Removing focus is equivalent to turning off the keyboardFocusManager.instance.primaryFocus.unfocus(); }}); }}Copy the code

use

main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    /// Just put it on the top of the app.
    return HideKeyboard(
      child: MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: Text("Hide soft keyboard demo"),
          ),
          body: Container(
            child: Text("hello,world""" "" "" "" "" " }}Copy the code