How to update state of a ModalBottomSheet in Flutter?

前端 未结 4 2215
一个人的身影
一个人的身影 2020-12-03 02:40

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,

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 03:33

    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;
    });
    

提交回复
热议问题