前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 FocusScopeNode 在 TextFormFields 之间轻松移动焦点

使用 FocusScopeNode 在 TextFormFields 之间轻松移动焦点

原创
作者头像
徐建国
修改2021-08-09 18:01:21
1.2K0
修改2021-08-09 18:01:21
举报
文章被收录于专栏:个人路线个人路线

FormTextFormField是在 Flutter 中输入文本时非常有用的小部件。

我们可以提供一种在键盘上按“下一步”时移动输入焦点的便捷方法吗?

使用FocusScopeNode,这是非常容易做到的。

假设您有一个电子邮件和密码输入表单,如下所示:

代码语言:javascript
复制
import 'package:flutter/material.dart';
​
class EmailPasswordSignInForm extends StatefulWidget {
  @override
  _EmailPasswordSignInFormState createState() =>
      _EmailPasswordSignInFormState();
}
​
class _EmailPasswordSignInFormState extends State<EmailPasswordSignInForm> {
  final FocusScopeNode _node = FocusScopeNode();
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
​
  @override
  void dispose() {
    _node.dispose();
    super.dispose();
  }
​
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FocusScopeNode "),
      ),
      body: Container(
        child: Form(
          key: _formKey,
          child: FocusScope(
            node: _node,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                // email
                TextFormField(
                  decoration: InputDecoration(
                    labelText: 'Email',
                    hintText: 'https://luckly.work/',
                  ),
                  textInputAction: TextInputAction.next,
                  keyboardType: TextInputType.emailAddress,
                  // move to the next field
                  onEditingComplete: _node.nextFocus,
                ),
                // password
                TextFormField(
                  decoration: InputDecoration(
                    labelText: 'Password',
                  ),
                  obscureText: true,
                  textInputAction: TextInputAction.done,
                  // move to the next field
                  onEditingComplete: _node.nextFocus,
                ),
                // submit
                RaisedButton(
                  child: Text('Sign In'),
                  onPressed: () {/* submit code here */},
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
​

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档