Flutter-TextField重点重建了窗口小部件

时间:2019-06-02 05:57:05

标签: flutter textfield

结果是您无法打开键盘。我在其他地方对此问题做了一些研究,大多数问题是当小部件是有状态的(而我的不是)时。以下是LoginWidget。我正在使用提供程序包,我怀疑这可能是在幕后做的。谁能看到我看不到的东西? :

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))
                        ],
                      )),
                )));
  }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

但是,如果您有“ LoginPage”列表,它将触发小部件使用相同的键。 我更喜欢将父窗口小部件与消费者小部件包装在一起。