在不包含导航器statefulwidget的上下文中请求的导航器操作

时间:2018-12-14 23:20:40

标签: button flutter statefulwidget statelesswidget

我正在研究下面的Flutter代码。我遇到第二个按钮“ Regitrese”的问题。我看过的每个地方都与无状态小工具一起使用,所以我不确定如何解决它。我尝试更改将其放置到主页上的空白: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(''),
      ),
    ],
  ),
);
}
}

1 个答案:

答案 0 :(得分:0)

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

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

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

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