我遇到一种情况,我需要以编程方式专注于InputField(例如响应按钮按下)。
我正在使用Focus.moveTo函数;但是,即使InputField被聚焦(出现闪烁的光标),键盘也不会被抬起。
似乎最好的解决方案是在_InputFieldState中调用RequestKeyboard()函数,但这是私有的。
做到这一点的最佳方法是什么?
这是显示工作流程的代码示例:
class InputFieldWrapper extends StatefulWidget {
@override
_InputFieldWrapperState createState() => new _InputFieldWrapperState();
}
class _InputFieldWrapperState extends State<InputFieldWrapper> {
InputValue _currentInput = new InputValue(text: 'hello');
// GlobalKey for the InputField so we can focus on it
GlobalKey<EditableTextState> _inputKey = new GlobalKey<EditableTextState>();
@override
Widget build(BuildContext context) {
return new Column(
children: [
// Button that should focus on the InputField when pressed
new IconButton(
icon: new Icon(Icons.message),
onPressed: () {
Focus.moveTo(_inputKey);
},
),
// InputField that should be focused when pressing the Button
new InputField(
value: _currentInput,
key: _inputKey,
onChanged: (InputValue input) {
setState(() {
_currentInput = input;
});
}
),
],
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
562 次 |
| 最近记录: |