【问题标题】:Flutter Web submit form on enter keyFlutter Web 在输入键上提交表单
【发布时间】:2020-10-06 15:19:55
【问题描述】:

当用户在填写表单时点击回车按钮时,有没有办法调用提交按钮。这是我的表单代码:

@override
  Widget build(BuildContext context) {
    String _email;
    return AlertDialog(
      title: Text('Password Reset'),
      content: Form(
        key: _formKey,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextFormField(
              decoration: InputDecoration(
                hintText: 'Email',
                labelText: 'Email',
              ),
              autofocus: true,
              maxLength: 30,
              validator: (value) {
                if (value.isEmpty) {
                  return 'Email is required';
                }
                return null;
              },
              onSaved: (input) => _email = input,
            ),
          ],
        ),
      ),
      actions: [
        RaisedButton(
          onPressed: () async {
            if (_formKey.currentState.validate()) {
              _formKey.currentState.save();
              var result = await auth.sendPasswordResetEmail(_email);
              print(result);
              Navigator.of(context).pop();
            }
          },
          child: Text('Reset'),
        )
      ],
    );
  }

【问题讨论】:

    标签: flutter flutter-web


    【解决方案1】:

    对于TextFormField,处理它的属性是onFieldSubmitted。您可以将onPressedRaiseButton 中的代码复制到此。例如

    onFieldSubmitted: (value) {
                    if (_formKey.currentState.validate()) {
                      _formKey.currentState.save();
    //               var result = await auth.sendPasswordResetEmail(_email);
    //               print(result);
                      print(_email);
                      Navigator.of(context).pop();
                    }
                  },
    

    完整示例可作为代码笔here

    您可能也对RawKeyboardListener 感兴趣,但是它 无法识别 enter 键。但可以听其他键如ShiftCapsLock等。

    【讨论】:

    • 对于TextField(相对于TextFormField),属性为onSubmitted,即void Function(String)
    • RawKeyboardListener 如何无法识别键盘上最重要的键之一...是真的吗?
    【解决方案2】:

    根据您的需要,使用TextFormField 构造函数的onEditingCompleteonSubmitted 参数。

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 2015-05-16
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      相关资源
      最近更新 更多