Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dialog that is able to accept text input and show result in flutter?

Tags:

flutter

dart

I wanted a dialog that is able to allow user to input their information and show it in a list view inside the dialog.

This is the code i used for listview.

new ListView.builder(
            itemBuilder: (context, index) {
              return _infos.isNotEmpty
                  ? new ListTile(
                      title: new Text(_infos[index].toMap().toString()),
                    )
                  : null;
            },
            shrinkWrap: true,
          )

and this is the code i use in onchange in textfield

setState(() {_infos= _result;});

like image 284
Daniel Mana Avatar asked Apr 11 '18 14:04

Daniel Mana


People also ask

How do I show text on button click in Flutter?

This can be simply achieved through using TextEditingController and setState() . Simply define a controller for your TextField and then call setState() when your button is pressed. Note that you have to be on a StatefulWidget since calling setState() rebuilds the UI.


1 Answers

Here is the customized function to create a default AlertDialog with a textField.

TextEditingController _textFieldController = TextEditingController();

Future<void> _displayTextInputDialog(BuildContext context) async {
    return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('TextField in Dialog'),
          content: TextField(
            controller: _textFieldController,
            decoration: InputDecoration(hintText: "Text Field in Dialog"),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text('CANCEL'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            FlatButton(
              child: Text('OK'),
              onPressed: () {
                print(_textFieldController.text);
                Navigator.pop(context);
              },
            ),
          ],
        );
      },
    );
  }

Use:

_displayTextInputDialog(context);

enter image description here

like image 185
Harsh Pipaliya Avatar answered Nov 11 '22 02:11

Harsh Pipaliya