使用不包含导航器 statefulwidget 的上下文请求的导航器操作

navigator operation requested with a context that does not include a navigator statefulwidget

我正在处理后面的 Flutter 代码。我对名为 "Regitrese" 的第二个按钮有疑问。我看过的每个地方都使用 statelesswidgets,所以我不确定如何修复它。我尝试更改 void 以将其放在 home: MyHomePage() 并将 MyHomePage 设置为 statefull 而不是从 MyApp 总线获取 statefull 它显示错误缺少 StatefulWidget.createState 的具体实现。我不确定它应该如何进行。你能让一个按钮在 StatefulWidget 中工作吗?有没有我没看到的技巧?

void main()=> runApp(new MyApp());

class MyApp extends StatefulWidget{
 @override
   State<StatefulWidget> createState(){
     return new MyHomePage();
   }
}

class MyHomePage extends State<MyApp>{
 final TextEditingController rutController = TextEditingController();
 final TextEditingController passwordController = TextEditingController();
 var _rut, _password;

 @override
 Widget build(BuildContext context) {
   return new MaterialApp(
   home: new Scaffold(
    body: new Container(
      padding: const EdgeInsets.all(50.0),
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          TextFormField(
            controller: this.rutController,
            decoration: InputDecoration(
              labelText: 'Rut',
              hintText: 'eg. 154683265',
                suffixIcon: IconButton(
                  icon: Icon(Icons.clear),
                  onPressed: () {
                    rutController.clear();
                  }
                )
            ),
          ),
          TextFormField(
            controller: this.passwordController,
            decoration: InputDecoration(
              labelText: 'Contraseña',
              hintText: 'Contraseña',
                suffixIcon: IconButton(
                    icon: Icon(Icons.clear),
                    onPressed: () {
                      passwordController.clear();
                    },
                )
            ),
            obscureText: true,
          ),
          RaisedButton(
            onPressed: (){
              loginButton(rut: this.rutController.text, password: this.passwordController.text);
            },
            child: Text('Login'),
          ),
          RaisedButton(
            onPressed: (){
              Navigator.push(
                 context,
                 MaterialPageRoute(builder(context)=>SelectUserPage())
              )
            },
            child: Text('Registrese'),
          ),
        ],
      ),
    ),
  ),
 );
}
}

class SelectUserType extends StatelessWidget{
 @override
 Widget build(BuildContext context){
  return new Container(
   child: new Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      RaisedButton(
        onPressed: (){
          //Do something
        },
        child: Text(''),
      ),
      RaisedButton(
        onPressed: (){
          //Do something
        },
        child: Text(''),
      ),
    ],
  ),
);
}
}

问题是您需要访问 MaterialApp 小部件下方的上下文,该小部件将 Navigator 添加到树中。如果您查看您的代码,context 在其上方。

解决它的一种方法是将树的一部分移动到另一个小部件。

或者您可以在按钮周围或 Column 周围使用 Builder,如下面的代码所示:

      Builder(
        builder: (context) => RaisedButton(
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(
                        builder: (context) => SelectUserType()));
              },
              child: Text('Registrese'),
            ),
      ),