flutter - 无法检测到 Textfield unfocus lister
问题描述
如何检测 textField 何时失去焦点?我尝试用谷歌搜索它,但只找到了检测文本字段何时聚焦的方法。
TextField(
keyboardType: TextInputType.text,
minLines: 2,
maxLines: null,
controller: fieldController,
),
解决方案
只需使用FocusNode
和addListener
方法,如下所示:
//define _node and _focused virables in header class
FocusNode _node = FocusNode();
bool _focused = false;
//handleFocusChange in initState method
@override
void initState() {
super.initState();
_node.addListener(_handleFocusChange);
}
void _handleFocusChange() {
if (_node.hasFocus != _focused) {
setState(() {
_focused = _node.hasFocus;
});
}
}
//use _node in TextField
TextField(
keyboardType: TextInputType.text,
minLines: 2,
maxLines: null,
focusNode:_node,
controller: fieldController,
),
推荐阅读
- codenameone - 从图库加载视频
- python-3.x - 将比例 z 检验应用于数据框中的每条记录
- reactjs - 在 React with Enzyme 中测试 Socket.io 监听器
- c# - 将多个过滤器与 Or 条件组合并将其发送 Simple Odata Client - C#
- r - head() 或 top_n() 函数更改数据集中数值的格式
- mysql - 当我尝试在 sequel pro 中运行此查询时出现错误
- powershell - 使用 foreach 循环执行 powershell 脚本后结果太多
- javascript - 通过 ASIN 获取亚马逊产品图片
- javascript - “var data = jQuery(msg), script;”是什么意思 在javascript中?
- laravel - 如何使用 Laravel 将查询字符串附加到分页链接