Flutter:No MediaQuery ancestor could be found starting from the context that was passed to MediaQuer

报这个错的原因:不能直接从runApp直接运行一个StatefulWidget类,而我。。。是这么做的

看一下,先前的代码 

main() {
  runApp(new FormTestRoute());
}

修改后的看一下 

 正确的代码

///---------Form----------
class FormTestPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      title: 'Form Test',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new FormTestRoute(),
    );
  }
}

class FormTestRoute extends StatefulWidget {
  @override
  _FormTestRouteState createState() => new _FormTestRouteState();
}

class _FormTestRouteState extends State {
  TextEditingController _unameController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();
  GlobalKey _formKey = new GlobalKey();
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Form Text"),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
        child: Form(
          key: _formKey, //设置gloablekey 用于后面获取FormState
          autovalidate: true, //开启自动校验
          child: new Column(
            children: [
              TextFormField(
                autofocus: true,
                controller: _unameController,
                decoration: InputDecoration(
                    labelText: "用户名",
                    hintText: "用户名或邮箱",
                    icon: new Icon(Icons.person)),
                //校验用户名
                validator: (v) {
                  return v.trim().length > 0 ? null : "用户名不能为空";
                },
              ),
              TextFormField(
                controller: _pwdController,
                decoration: InputDecoration(
                    labelText: "密码",
                    hintText: "您的登录密码",
                    icon: new Icon(Icons.lock)),
                obscureText: true,
                validator: (v) {
                  return v.trim().length > 5 ? null : "密码不能少于6位";
                },
              ),
              //登录按钮
              Padding(
                padding: const EdgeInsets.only(top: 28.0),
                child: new Row(
                  children: [
                    Expanded(child: Builder(
                      builder: (context) {
                        return RaisedButton(
                          padding: EdgeInsets.all(15.0),
                          child: new Text("登录"),
                          color: Theme.of(context).primaryColor,
                          textColor: Colors.white,
                          onPressed: () {
                            if ((_formKey.currentState as FormState)
                                .validate()) {
                              //验证通过提交数据
                            }
                          },
                        );
                      },
                    ))
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

main() {
  runApp(new FormTestPage());
}

 

 

你可能感兴趣的:(Flutter学习错误集锦)