- 最外层使用GestureDetector
- GestureDetector添加 点击 和 滑动 事件监听
- 判断键盘是否是弹起状态
- 使用系统方法FocusManager收起键盘
/// ListView Widget
Widget _buildListView() {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () { /// 页面点击响应
searchModule.closeKeyboard(context);
},
onPanDown: (_) { /// 列表滑动响应
searchModule.closeKeyboard(context);
},
child: Container(
color: themeModule.themeData.backgroundColor,
child: ListView.builder(
itemCount: searchModule.results.length,
itemBuilder: (context, index) {
return Material(
child: Ink(
color: Colors.white,
child: InkWell(
onTap: () {
_itemOnClick(index, searchModule.results[index]);
},
child: Container(
child: _buildListItem(
searchModule.results[index], index),
),
),
),
);
}
),
)
);
}
void closeKeyboard(BuildContext context) {
FocusScopeNode currentFocus = FocusScope.of(context);
/// 键盘是否是弹起状态
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus.unfocus();
}
}