how to get data from modal bottom sheet in flutter?

Viewed 4249

I've making form screen.

process is

  1. button click
  2. bottom sheet shown
  3. user type their name => ok button clicked
  4. TEXT will be changed by user name

i wanna get data from modal bottom sheet widget but don't know how

this is my 2nd process code

InkWell(
  onTap: () {
    showCupertinoModalBottomSheet(
        context: context,
        builder: (context, scrollController) =>
            RequestNameScreen());
    
  },
...

how can i get 'name' from bottom sheet ??

1 Answers

What showModalBottomSheet() does internally is - it pushes a new route onto the stack. So you can get data from bottom sheet in the same way as you'd get data from a screen (route).

String username = "TEXT";

InkWell(
  onTap: () {
    showCupertinoModalBottomSheet(
        context: context,
        builder: (context, scrollController) =>
            RequestNameScreen()
    ).then((value){
      setState((){
        username = value;
      });
    });
    
  },
...

And when you pop the sheet (RequestNameScreen()) on ok button, you can pass the text provided by user like this.

Navigator.pop(context, "text from user");
Related