项目中有需求,当输入框键盘放下是,刷新一下当前页面某个数据,也使用过keyboard_visibility这个框架,并没有达到理想的效果,最后发现通过设置FocusNode完美的解决了问题
1.创建FocusNode
2.添加监听
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_focusNote.addListener(focusChangeEvent);
});
}
void focusChangeEvent() {
//键盘释放
if (!_focusNote.hasFocus) {
//刷新页面数据
setState(() {});
}
}
3.给textfield添加focuNode
TextField(focusNode:_focusNode)
4.释放监听
@override
void dispose() {
_focusNote?.removeListener(focusChangeEvent);
_focusNote?.dispose();
super.dispose();
}