gpt4 book ai didi

flutter - 使用 Navigator.push (MaterialPageRoute) 而不是 AlertDialog

转载 作者:IT王子 更新时间:2023-10-29 07:02:44 25 4
gpt4 key购买 nike

我想使用 Navigator.push (MaterialPageRoute) 而不是 AlertDialog,因为现在我认为让我的用户拥有一个完整的页面来发布内容而不是一个对话框更好,我将如何编辑我的代码来做这个?提前致谢

 appBar: AppBar(
centerTitle: true,
title: Text('hehe',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0),),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 10.0),
child: IconButton(icon: Icon(Icons.comment),
onPressed: () {
showDialog(context: context,
builder: (BuildContext context){
return AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
content: Form(key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
initialValue: '',
onSaved: (val) => board.subject = val,
validator: (val) => val == "" ? val: null,
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: RaisedButton(
color: Colors.indigo,
child: Text(
'Post',
style: TextStyle(color: Colors.white),),
onPressed: () {
handleSubmit();
Navigator.of(context).pop();
},
),
)
],
),
),
);
},
);
}
),
),
],
),

最佳答案

创建一个 StatefulWidget 子类说 MyForm

class MyForm extends StatefulWidget {
@override
_MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("My form")),
body: Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
initialValue: '',
onSaved: (val) => board.subject = val,
validator: (val) => val == "" ? val : null,
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: RaisedButton(
color: Colors.indigo,
child: Text(
'Post',
style: TextStyle(color: Colors.white),
),
onPressed: () {
handleSubmit();
Navigator.of(context).pop();
},
),
)
],
),
),
);
}
}

然后在您的 onPressed 方法中像这样使用它。

onPressed: () {
Navigator.push(context, MaterialPagerRoute(builder: (context) => MyForm()));
}

因此,当单击该按钮时,您将被导航到当前是您的表单的新页面。

关于flutter - 使用 Navigator.push (MaterialPageRoute) 而不是 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56774919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com