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

标签 flutter dart flutter-layout flutter-appbar

我想使用 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/

相关文章:

dart - 我可以将 MediaQuery.of(context) 固定为常数值吗?

forms - flutter 错误 “A build function returned null”

user-interface - flutter 如何在小部件周围/容器内部绘制自定义边框

dart - 为什么 toList() 在 Dart 中创建一个 List<dynamic> ?

flutter - 如何在 Flutter 中将颜色作为 UI 文本的一部分?

dart - 如何以编程方式为整个应用程序更改脚手架小部件的背景颜色

dart - 使用 fluro 导航时更改值

dart - 使用 shift+r flutter 时,Vscode 不支持全局评估

flutter - 通过 BuildContext 获取 Route 参数需要将它放在 build() 中吗?

flutter - 停止点击时停止GIF动画