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,
}