美文网首页
Flutter——全局点击背景收起键盘

Flutter——全局点击背景收起键盘

作者: Lucky_Blue | 来源:发表于2021-10-22 23:56 被阅读0次
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          initialRoute: "/",
          routes: {
            "/": (context) => HomePage(),
          },
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          builder: (BuildContext context, child) => Scaffold(
            body: GestureDetector(
              child: child,
              onTap: () {
                hideKeyboard(context);
              },
            ),
          ),
        );
      }
    
      void hideKeyboard(BuildContext context) {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
          FocusManager.instance.primaryFocus.unfocus();
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:Flutter——全局点击背景收起键盘

          本文链接:https://www.haomeiwen.com/subject/rxehaltx.html