dart - TextField 的自动对焦属性导致小部件进入“脏”状态
问题描述
AppBar 有一个搜索图标,单击它会在“标题”属性中显示一个 TextField。单击搜索图标时,我需要 TextField 自动对焦。问题在于“autofocus”属性,如果设置为 true,而不是仅更改 title 属性的状态,某些东西会导致小部件变成“脏”状态。这会导致调用主构建函数,从而重建整个事物。
试图复制它并提供一个示例应用程序,但奇怪的是它在演示中似乎工作得很好。
有什么调试建议吗?
AppBar(
centerTitle: true,
title: StreamBuilder(
stream: false,
initialData: symbolBloc.isSearching,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot){
if(snapshot.data) {
return TextField(
// autofocus: true, <--- here
controller: searchQuery,
style: new TextStyle(
color: Colors.white,
),
);
}
return new Text("", style: new TextStyle(color: Colors.white)
);
}),
backgroundColor: Colors.blueGrey,
elevation: 0.0,
actions: <Widget>[
StreamBuilder(
stream: bloc.isSearchActive,
initialData: false,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot){
if(snapshot.data)
return activeSearchIconButton(symbolBloc);
return searchIconButton(symbolBloc);
},
),
],
),
searchIconButton(SymbolBloc bloc){
return new IconButton(
icon: new Icon(
Icons.search,
color: Colors.white,
),
tooltip: 'Search',
onPressed: (){
bloc.displaySearchField(true);
},
);
}
activeSearchIconButton(SymbolBloc bloc){
return new IconButton(
icon: new Icon(
Icons.close,
color: Colors.white,
),
tooltip: 'Search',
onPressed: (){
bloc.displaySearchField(false);
},
);
}
解决方案
您可以设置FocusNode
属性TextField
和Button
点击调用FocusScope.of(context).requestFocus(_focusNode);
推荐阅读
- gradle - Gradle - 复制任务完成操作后如何执行某些操作?
- python - Python:Tkinter TclError:无法调用“图像”命令
- ios - 使用 Ionic 3 和 Amazon Pinpoint 推送通知
- ios - UINavigationController 如何在子视图中设置标题等
- angular - 在Angular4 / Ionic中使用*ngFor时如何从列表中获取单击的项目详细信息
- opennms - OpenNMS 实现脚本来获取任何服务的状态
- java - 我的服务每两分钟运行一次,我不知道为什么
- java - Bootstrap 固定导航栏在滚动时不会保持页面宽度?
- reactjs - Redux 在单个页面中形成多个表单
- php - 在 centos 中启用 libmongossl