您当前的位置: 首页 > 

大前端之旅

暂无认证

  • 1浏览

    0关注

    403博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

大前端之旅 发布时间:2021-08-09 14:53:40 ,浏览量:1

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

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

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

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

import 'package:flutter/material.dart';

class EmailPasswordSignInForm extends StatefulWidget {
  @override
  _EmailPasswordSignInFormState createState() =>
      _EmailPasswordSignInFormState();
}

class _EmailPasswordSignInFormState extends State {
  final FocusScopeNode _node = FocusScopeNode();
  final GlobalKey _formKey = GlobalKey();

  @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: [
                // 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 */},
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

关注
打赏
1660524863
查看更多评论
立即登录/注册

微信扫码登录

0.0438s