Используйте Navigator.push (MaterialPageRoute) вместо AlertDialog - PullRequest
1 голос
/ 26 июня 2019

Я бы хотел использовать 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();
                      },
                    ),
                  )
                ],
              ),
              ),
            );
          },
          );
            }
            ),
      ),
    ],
  ),

1 Ответ

0 голосов
/ 26 июня 2019

Создайте 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()));
}

Итак, когда кнопка нажата, вы перейдете на новую страницу, которая в данный момент является вашей формой.

...