简体   繁体   中英

Flutter - TextField rebuilds widget on focus

The effect is that you can't open the keyboard. I did some digging around this issue elsewhere, and most issues are when the widget is stateful (but mine is not).The following is the LoginWidget . I'm using the provider package, which I suspect might be doing something underneath the covers. Can anyone spot something I don't see? :

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String email, password;
    final GlobalKey<FormState> formKey = GlobalKey<FormState>();
    return ChangeNotifierProvider<LoginModel>(
        builder: (context) => LoginModel(ViewState.Idle),
        child: Consumer<LoginModel>(
            builder: (context, model, child) => Scaffold(
                  appBar: AppBar(
                    title: Text("Sign in"),
                  ),
                  body: Form(
                      key: formKey,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          TextFormField(
                            validator: (input) {
                              if (input.isEmpty) {
                                return "Email required";
                              }
                            },
                            onSaved: (input) => email = input,
                            decoration: InputDecoration(labelText: 'Email'),
                          ),
                          TextFormField(
                            validator: (input) {
                              if (input.isEmpty) {
                                return "Password required";
                              }
                            },
                            onSaved: (input) => password = input,
                            decoration: InputDecoration(labelText: 'Password'),
                            obscureText: true,
                          ),
                          model.state == ViewState.Loading
                              ? CircularProgressIndicator()
                              : RaisedButton(
                                  onPressed: () async {
                                    if (formKey.currentState.validate()) {
                                      formKey.currentState.save();
                                      bool success =
                                          await model.login(email, password);
                                      if (success) {
//                                  Navigator.pushNamed(context, '/');
                                        Navigator.pushReplacement(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) => HomePage()),
                                        );
                                      }
                                    }
                                  },
                                  child: Text('Sign in'),
                                ),
                          if (model.state == ViewState.Error)
                            Center(child: Text(model.apiErrorMessage))
                        ],
                      )),
                )));
  }
}

解决方案是将final GlobalKey<FormState> formKey = GlobalKey<FormState>()移出构建器并使其成为static

But if you have a list of "LoginPage" it will trigged widget use the same key. I prefer wrap the parent widget with a Consumer widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM