Flutter 无法将焦点设置为ExpansionFile programmaticaly内的文本字段
我有一个Flutter 无法将焦点设置为ExpansionFile programmaticaly内的文本字段,flutter,Flutter,我有一个expansionfile,里面有一个TextField,当用户展开互动程序时,我试图将焦点设置到这个文本字段。但由于某些原因,我无法让它工作。 代码如下: ExpansionTile( key: PageStorageKey<String>("notes_key"), title: Text( "Notes", style: TextStyle(color: Colors.indigo), ), onExpansionChanged: (ex
expansionfile
,里面有一个TextField
,当用户展开互动程序时,我试图将焦点设置到这个文本字段。但由于某些原因,我无法让它工作。
代码如下:
ExpansionTile(
key: PageStorageKey<String>("notes_key"),
title: Text(
"Notes",
style: TextStyle(color: Colors.indigo),
),
onExpansionChanged: (expanded){
if(expanded){
FocusScope.of(context).requestFocus(_notesFocusNode);
_notesController.value = _notesController.value.copyWith(text: _notes);
} else {
_notesFocusNode.unfocus();
}
},
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
),
textInputAction: TextInputAction.newline,
style: TextStyle(color: Colors.black),
keyboardType: TextInputType.multiline,
focusNode: _notesFocusNode,
controller: _notesController,
key: PageStorageKey("notes_field_key"),
minLines: 1,
maxLines: null,
onChanged: (value) => _notes = value,
),
),
IconButton(
icon: Icon(Icons.done),
color: Colors.teal,
onPressed: () => _notesFocusNode.unfocus(),
),
],
),
],
)
扩展文件(
key:PageStorageKey(“notes_key”),
标题:正文(
“附注”,
样式:TextStyle(颜色:Colors.indigo),
),
OneExpansionChanged:(已展开){
如果(扩展){
FocusScope.of(context).requestFocus(_notesFocusNode);
_notesController.value=\u notesController.value.copyWith(文本:\u notes);
}否则{
_notesFocusNode.unfocus();
}
},
儿童:[
划船(
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
扩大(
孩子:TextField(
装饰:输入装饰(
边框:InputBorder.none,
),
textInputAction:textInputAction.newline,
样式:TextStyle(颜色:Colors.black),
键盘类型:TextInputType.multiline,
focusNode:_notesFocusNode,
控制器:\注意控制器,
键:PageStorageKey(“注释\字段\键”),
小姑娘:1,
maxLines:null,
一旦更改:(值)=>\u notes=value,
),
),
图标按钮(
图标:图标(Icons.done),
颜色:Colors.teal,
按下时:()=>\u notesFocusNode.unfocus(),
),
],
),
],
)
谢谢你的帮助
对于更大的上下文,文本字段不在任何表单中。提供的代码块位于较大的代码块中,它被包装在
列表视图中。可能我需要在视图中设置一些父焦点范围?展开磁贴时,TextFormField
尚不可用,无法获取focus
。它仅在动画启动几毫秒后才可用。由于无法知道动画何时完成,因此可以通过添加延迟来进行变通。我建议至少使用200
毫秒,这样动画就不会交错,但TextFormField
会获得焦点:
Future.delayed(Duration(milliseconds: 200), (){
FocusScope.of(context).requestFocus(_notesFocusNode);
});
不知道是谁否决了你,但实际上你的想法是正确的。但是,对于开发人员来说,在flatter中实现expansionfile
中的onExpansionChanged
属性是毫无用处的。。。谢谢你的帮助。