Home > Mobile >  Pass value when dialog closed
Pass value when dialog closed

Time:08-18

I'm trying to create an app in Flutter.

When a particular button is pressed a Dialog shows up. In the dialog, the user can write to TextField. I want to use this text in the previous screen when the dialog is closed with pop().

Is there any way to do it?

CodePudding user response:

try this:

showDialog(
  context: context,
  builder: (context) => Dialog(),
).then((result){
  // use the result here
});

and in dialog pop like this:

Navigator.pop(context, result);

CodePudding user response:

You can await to get data from button. also You can pass data .pop(YourValue)

onPressed: () async {
  final data = await showDialog(
    context: context,
    builder: (context) {
      final TextEditingController controller =
          TextEditingController(); // this can be outside to get direct text from it
      return AlertDialog(
        content: TextField(
          controller: controller,
        ),
        actions: [
          TextButton(
              onPressed: () {
                Navigator.of(context).pop(controller.text);
              },
              child: Text("Close"))
        ],
      );
    },
  );
  if (data != null) {
    //your operation
  }
  print(data);
},
  • Related