This code is very simple: shows a modal bottom sheet and when the uses clicks the button, it increases the height of the sheet by 10.
But nothing happens. Actually, it only updates its size if the user "slides" the bottom sheet with it's finger (I belive that swipe causes a internal setState on the sheet).
My question is: how do I call the update state of a ModalBottomSheet?
showModalBottomSheet( context: context, builder: (context) { return Container( height: heightOfModalBottomSheet, child: RaisedButton( onPressed: () { setState(() { heightOfModalBottomSheet += 10; }); }), ); });
When we close the bottom bar and open it again, then we pass the updated context so we can find the updated icon. Here, if you want, what you can do is, when the user clicks on the icon button inside the bottom sheet, after setting the state, you can immediately pop the bottom sheet by using Navigator. pop(context).
How To Display Bottom Sheet In Flutter ? To display a general bottom sheet we have to use bottomSheet property of scaffold widget. return Scaffold( appBar: AppBar( title: Text("Flutter Bottom Sheet"), ), body:Container(), bottomSheet: BottomSheet(), );
A modal bottom sheet is a widget, which is a core building block of Flutter apps, in Material Design. Modal bottom sheets in Flutter are used to display supplementary content while restricting the user from interacting with the app’s main content. As the name suggests, a bottom sheet is positioned at the bottom of the screen.
Actually, it only updates its size if the user "slides" the bottom sheet with it's finger (I belive that swipe causes a internal setState on the sheet). My question is: how do I call the update state of a ModalBottomSheet?
Expanding bottom sheets are like a hybrid of standard and modal bottom sheets. An expanding bottom sheet enables the user to access both the standard sheet and information presented in the modal bottom sheet For this tutorial, we’ll focus on creating a modal bottom sheet in Flutter.
Now it’s time to create the body widget, which will contain a button, ElevatedButton, with the text, “Show Modal Bottom Sheet.” The button will be placed at the center of the app using the Center widget. As stated earlier, we’ll use the showModalBottomSheet widget to display a modal bottom sheet, which takes two properties: context and the builder.
You can use Flutter's StatefulBuilder
to wrap your ModalBottomSheet as follows:
showModalBottomSheet( context: context, builder: (context) { return StatefulBuilder( builder: (BuildContext context, StateSetter setState /*You can rename this!*/) { return Container( height: heightOfModalBottomSheet, child: RaisedButton(onPressed: () { setState(() { heightOfModalBottomSheet += 10; }); }), ); }); });
Please note that the new setState
will override your main widget setState
but sure you can just rename it so you would be able to set state of your parent widget and the modal's
//This sets modal state setModalState(() { heightOfModalBottomSheet += 10; }); //This sets parent widget state setState(() { heightOfModalBottomSheet += 10; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With