Ric*_*zal 1 button flutter statefulwidget statelesswidget
我正在处理后面的 Flutter 代码。我对名为“Regitrese”的第二个按钮有疑问。我看过的每个地方都使用 statelesswidgets,所以我不知道如何解决它。我尝试更改 void 以将其放在 home: MyHomePage() 并将 MyHomePage 置于 statefull 而不是从 MyApp bus 中获取 statefull 它显示了一个错误 Missing specific implementation of 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(''),
),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是您需要访问MaterialApp添加Navigator到树的小部件下方的上下文。如果你看看你的代码context,它就在上面。
解决它的一种方法是将树的一部分移动到另一个小部件。
或者您可以Builder在按钮周围使用 a或Column在以下代码中使用 a :
Builder(
builder: (context) => RaisedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(
builder: (context) => SelectUserType()));
},
child: Text('Registrese'),
),
),
Run Code Online (Sandbox Code Playgroud)