颤振小部件内的表单 - 传递表单键

问题描述

我的 Flutter 小部件定义:

class LoginFormWidget extends StatefulWidget {
  const LoginFormWidget({
    Key key,@required this.loginFormKey,@required this.isEnabled,@required this.citiesListControl,@required this.onPasswordLoginpressed,@required this.onGuestLoginpressed,@required this.onGoogleLoginpressed,@required this.onFacebookLoginpressed,@required this.onEmailSaved,@required this.onPasswordSaved,this.statusText,}) : super(key: key);

  //final GlobalKey<FormState> loginFormKey;
  final Widget citiesListControl;
  final bool isEnabled;
  final String statusText;
  final GlobalKey<FormState> loginFormKey;

  final Function() onPasswordLoginpressed;
  final Function() onGuestLoginpressed;
  final Function() onGoogleLoginpressed;
  final Function() onFacebookLoginpressed;
  final Function(String) onEmailSaved;
  final Function(String) onPasswordSaved;

  @override
  _LoginFormWidgetState createState() => _LoginFormWidgetState();
}

我定义了一些回调来处理登录按钮点击。对于登录电子邮件/密码,有一个登录按钮:

final loginButton = RaisedButton(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(24.0),),onpressed: widget.onPasswordLoginpressed,padding: EdgeInsets.all(12.0),color: Colors.lightBlueAccent,child: Text(Strings.btn_login,style: TextStyle(color: Colors.white),key: Key(Strings.btn_login)),);

要使用我的小部件:

LoginFormWidget(
      isEnabled: _isCitySelected,onEmailSaved: (value) => _email = value,onPasswordSaved: (value) => _password = value,citiesListControl: citiesListControl,statusText: _status,loginFormKey: _loginFormKey,onPasswordLoginpressed: _isCitySelected
          ? () {
              if (_loginFormKey.currentState.validate()) {
                _loginFormKey.currentState.save();
                _loginEmailPassword();
              }
            }
          : null,

因此,如果没有选择城市(_isCitySelected 为 false) - 按钮的 onpressed 应为空(按钮已禁用)。否则,应执行验证和表单保存。

如何在我的情况下执行表单验证?如果我将表单密钥传递给我的小部件(密钥在我的根小部件中定义)并按上述方式执行验证和保存,我将收到异常:

I/Flutter (22322): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/Flutter (22322): The following NoSuchMethodError was thrown while handling a gesture:
I/Flutter (22322): The method 'validate' was called on null.
I/Flutter (22322): Receiver: null
I/Flutter (22322): Tried calling: validate()
I/Flutter (22322): 
I/Flutter (22322): When the exception was thrown,this was the stack:
I/Flutter (22322): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
I/Flutter (22322): #1      _LoginPageState._buildLoginForm.<anonymous closure> (package:lupe2/pages/login_page.dart:361:40)
I/Flutter (22322): #2      _InkResponseState._handleTap (package:Flutter/src/material/ink_well.dart:991:20)
I/Flutter (22322): #3      GestureRecognizer.invokeCallback (package:Flutter/src/gestures/recognizer.dart:182:24)
I/Flutter (22322): #4      TapGestureRecognizer.handleTapUp (package:Flutter/src/gestures/tap.dart:607:11)
I/Flutter (22322): #5      BaseTapGestureRecognizer._checkUp (package:Flutter/src/gestures/tap.dart:296:5)

onEmailSaved: (value) => _email = value,

是能够将表单值保存在我的小部件之外的回调函数

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)