Flutter 全局收起键盘

2019-11-14  本文已影响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,
}
上一篇 下一篇

猜你喜欢

热点阅读