美文网首页
Flutter 全局收起键盘

Flutter 全局收起键盘

作者: 倪大头 | 来源:发表于2019-11-14 10:49 被阅读0次

    main.dart里在MaterialApp外面套一层点击手势

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            //收起键盘
            FocusScope.of(context).requestFocus(FocusNode());
          },
          child: MaterialApp(
            title: '我是Title',
            theme: ThemeData(
              primarySwatch: Colors.blue, //主题色
              primaryColor: Colors.white, //导航色
            ),
            home: MyTabBar(),
          ),
        );
      }
    }
    

    其中behavior: HitTestBehavior.translucent属性有三个值:

    enum HitTestBehavior {
      /// Targets that defer to their children receive events within their bounds
      /// only if one of their children is hit by the hit test.
      deferToChild,
    
      /// Opaque targets can be hit by hit tests, causing them to both receive
      /// events within their bounds and prevent targets visually behind them from
      /// also receiving events.
      opaque,
    
      /// Translucent targets both receive events within their bounds and permit
      /// targets visually behind them to also receive events.
      translucent,
    }
    

    相关文章

      网友评论

          本文标题:Flutter 全局收起键盘

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