1

我正在研究下面的 Flutter 代码。我对名为“Regitrese”的第二个按钮有疑问。我看过的每个地方都使用无状态小部件,所以我不知道如何修复它。我尝试更改 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(''),
      ),
    ],
  ),
);
}
}
4

1 回答 1

7

问题是您需要访问添加到树的MaterialApp小部件下方的上下文。Navigator如果你看你的代码,它就在context它上面。

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

或者,您可以Builder在按钮周围使用 a 或在Column以下代码中使用 a:

      Builder(
        builder: (context) => RaisedButton(
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(
                        builder: (context) => SelectUserType()));
              },
              child: Text('Registrese'),
            ),
      ),
于 2018-12-14T23:57:11.830 回答