Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update state of a ModalBottomSheet in Flutter?

Tags:

flutter

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;               });              }),       );     }); 
like image 220
Daniel Oliveira Avatar asked Sep 19 '18 21:09

Daniel Oliveira


People also ask

How do you update the bottomSheet in flutter?

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 do you show the bottom sheet flutter?

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(), );

What is a modal bottom sheet in flutter?

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.

Does modalbottomsheet update its size when it is swiped?

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?

What is expanding bottom sheet in flutter?

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.

How do I display a modal bottom sheet in an app?

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.


1 Answers

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; }); 
like image 145
mmahgoub Avatar answered Sep 30 '22 10:09

mmahgoub