Как передать данные из диалогового окна оповещения на ту же страницу во флаттере

Это идеальный вариант использования для DesignGridLayout :

DesignGridLayout layout = new DesignGridLayout(contentPane);
layout.labelAlignment(LabelAlignment.RIGHT);
layout.row().grid(label1).add(field1);
layout.row().grid(label2).add(field2);
...

1
задан mohammed nabil 1 March 2019 в 14:18
поделиться

2 ответа

В текстовом поле есть параметр onChanged: вы можете использовать его для передачи функции

TextField(
            keyboardType: TextInputType.number,
            onChange: onChange
            decoration: InputDecoration(
                hintText: 'Enter the number'
            ),
          )

на вашем главном экране, используйте это:

 void onChange(String text) {
 //do stuff here with text like maybe setState
 }
0
ответ дан Hussein Abdallah 1 March 2019 в 14:18
поделиться

Отредактируйте новое решение:

// write this in your main page
String onMainPageText;

вы можете отобразить это в на своей главной странице! после нажатия кнопки okey в вашем методе _showdialog Text(onMainPageText)

измените ваш метод _showDialog с помощью следующего кода.

  void _showDialog() {
    String dialogText;
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Alert Dialog title"),
          content: TextField(
            onChanged: (String textTyped) {
              setState(() {
                dialogText = textTyped;
              });
            },
            keyboardType: TextInputType.number,
            decoration: InputDecoration(hintText: 'Enter the number'),
          ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            Row(
              children: <Widget>[
                new FlatButton(
                  child: new Text("Cancel"),
                  onPressed: () {
                    setState(() {
                      onMainPageText = '';
                    });
                    Navigator.of(context).pop();
                  },
                ),
                new FlatButton(
                    onPressed: () {
                      setState(() {
                        onMainPageText = dialogText;
                      });
                      Navigator.of(context).pop();
                    },
                    child: new Text("OK"))
              ],
            ),
          ],
        );
      },
    );
  }

Старый ответ:

создайте глобальный TextEditingController, который решит вашу проблему. Вы можете получить доступ к тексту текстового поля с помощью textEditingConroller.text

, не забудьте определить textEditingController внутри вашего класса

class YourMainPageState extends State<YourMainPage>{
  TextEditingController textEditingController = new TextEditingController();

}
  void _showDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Alert Dialog title"),
          content: TextField(
            controller: textEditingController,
            keyboardType: TextInputType.number,
            decoration: InputDecoration(hintText: 'Enter the number'),
          ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            Row(
              children: <Widget>[
                new FlatButton(
                  child: new Text("Cancel"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                new FlatButton(onPressed: () {print(textEditingController.text);}, child: new Text("OK"))
              ],
            ),
          ],
        );
      },
    );
  }

Вы можете отобразить напечатанный текст с этим кодом:

Padding(
                padding: const EdgeInsets.only(top: 50.0),
                  child: new Text(texEditingController.text););
0
ответ дан cipli onat 1 March 2019 в 14:18
поделиться
Другие вопросы по тегам:

Похожие вопросы: