///移除键盘核心代码
FocusScope.of(context).requestFocus(new FocusNode());
创建dart文件,并添加如下类。在需要点击或者拖拽的view外面嵌入如下类,触发相关方法的时候就可以收回键盘
//点击收回键盘
import 'package:flutter/material.dart';
class TouchHideKeyBoard extends StatelessWidget {
final Widget child;
TouchHideKeyBoard({this.child});
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
onHorizontalDragEnd: (_) {
FocusScope.of(context).requestFocus(new FocusNode());
},
onVerticalDragEnd: (_) {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: child,
);
}
}
//拖拽收回键盘
NotificationListener(
onNotification: (_) {
FocusScope.of(context).requestFocus(new FocusNode());
return false;
},
child: child, //child是ListView之类的
);