1.键盘弹出
final FocusNode _focusNode = new FocusNode();
new TextField(
focusNode: _focusNode,
),
2.请求收起键盘
FocusScope.of(context).requestFocus(new FocusNode());
3.监听键盘是否显示
WidgetsBinding.instance.addPostFrameCallback(() {
setState(() {
键盘高度:大于零,键盘弹出,否则,键盘隐藏
MediaQuery.of(context).viewInsets.bottom > 0;
});
});
class _KeyboardDetectorState extends State<KeyboardDetector>
with WidgetsBindingObserver {
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void didChangeMetrics() {
super.didChangeMetrics();
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
widget.keyboardShowCallback
?.call(MediaQuery.of(context).viewInsets.bottom > 0);
});
});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}