flutter - tab keypress/TextInputAction.next 导航到下一个输入表单
问题描述
我有一个非常标准的登录表单。在 Web 上,标签导航是非常标准的——按电子邮件文本字段上的 tab 键导航到密码文本字段(反之亦然)。在移动设备上,TextInputAction.next 应该以相同的方式工作。但是我如何在颤动中做到这一点?
这是我的简单登录表单:
class LoginFormState extends State<LoginForm> {
// Create a global key that uniquely identifies the Form widget
// and allows validation of the form.
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Scaffold(
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter your email'),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
autovalidate: true,
style: TextStyle(
fontSize: 28,
),
),
TextFormField(
obscureText: true,
autocorrect: false,
decoration: InputDecoration(labelText: 'Enter your API key'),
style: TextStyle(
fontSize: 28,
),
),
],
)),
));
解决方案
你需要为每个创建一个FocusNodeTextFormField
,然后你需要将它分配给每个TextFormField
,然后在onEditingComplete
你需要调用requestFocus
的参数FocusNode
中的下一个TextFormField
。
查看此官方教程以获取更多信息。
并检查具有简化 API 的flutter_form_bloc来执行此操作。
class LoginForm extends StatefulWidget {
LoginForm({Key key}) : super(key: key);
_LoginFormState createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final _formKey = GlobalKey<FormState>();
FocusNode _apiKeyFocusNode;
FocusNode _otherFieldFocusNode;
@override
void initState() {
super.initState();
_apiKeyFocusNode = FocusNode();
_otherFieldFocusNode = FocusNode();
}
@override
void dispose() {
// Clean up the focus node when the Form is disposed.
_apiKeyFocusNode.dispose();
_otherFieldFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'Enter your email'),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
autovalidate: true,
style: TextStyle(
fontSize: 28,
),
onEditingComplete: () => _apiKeyFocusNode.requestFocus(),
),
TextFormField(
focusNode: _apiKeyFocusNode,
obscureText: true,
autocorrect: false,
decoration: InputDecoration(labelText: 'Enter your API key'),
style: TextStyle(
fontSize: 28,
),
onEditingComplete: () => _otherFieldFocusNode.requestFocus(),
),
TextFormField(
focusNode: _otherFieldFocusNode,
obscureText: true,
autocorrect: false,
decoration: InputDecoration(labelText: 'Other field'),
style: TextStyle(
fontSize: 28,
),
),
],
)),
));
}
}
推荐阅读
- ssh - Build Docker image to install/run openssh-server
- python - 如何从多个段落中找到常用的单词或句子或段落
- python-3.x - tee command change behavior with py3
- javascript - How to add new columns to an Excel file in my local system using Vanilla Javascript?
- wso2 - 如何更改 WSO2 APIM-3.2.0 和 IS-5.10.0 的管理员密码
- javascript - 树中始终只有一个节点
- kotlin - 有没有可以在 Dart 中编写像 Future&then 这样的挂起函数的函数?
- java - JMH Benchmark 性能怪异,因为 @Param 注释
- postman - 如何在邮递员中为 2 路 TLS 添加我信任的服务器证书?
- typescript - TypeScript [需要,导入] 错误 | 外部节点模块 | 吞咽