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

Use Navigator.push (MaterialPageRoute) instead of AlertDialog

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

 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()));
}

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