0

when the bottom sheet appears it does not has that much height. but when a checkbox clicked in the bottom sheet it is expanding to the top regardless the screen size. what I have been trying to do is to set max height for bottom sheet. can't find proper solution for that so far. any helps are welcome!

ps: what I have been trying do is open bottom sheet dialog as wrap content initially. after the checkbox is checked its height should have max value. in other words I want to expand it regarding the max height of bottom sheet that will be given.

senario:

  1. bottom model should be opened as wrap_content
  2. when an item in bottom sheet is clicked the height of the bottom sheet should change to specific a value.
aligur
  • 3,387
  • 3
  • 34
  • 51
  • Can you provide sample snippet that will reproduce the same issue ? – Md. Yeasin Sheikh Nov 27 '22 at 14:55
  • @YeasinSheikh I couldn't paste the code as it is complicated but basically what I have been trying do is open bottom sheet dialog as wrap content initially. after the checkbox is checked its height should have max value. in other words I want to expand it regarding the max height of bottom sheet that will be given. – aligur Nov 28 '22 at 06:42

2 Answers2

1

For the bottom sheet, you need to set the height of the sheet.

and before you set the height set to enable this option

isScrollControlled: true,


height: MediaQuery.of(context).size.height - 100,
Chirag Sondagar
  • 427
  • 1
  • 4
  • 15
  • what I have been trying do is open bottom sheet dialog as wrap content initially. after the checkbox is checked its height should have max value. – aligur Nov 28 '22 at 06:40
0

Updated:

What you want is to update the state of the bottomsheet using StatefulBuilder

Here's the sample code

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool setToMaxHeight = false;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: const Text('showModalBottomSheet'),
        onPressed: () {
          showModalBottomSheet<void>(
            isScrollControlled: true,
            context: context,
            builder: (BuildContext context) {
              return StatefulBuilder(builder: (BuildContext context,
                  StateSetter setState) { //this
                return Container(
                  height: setToMaxHeight ? 600 : 300, //this
                  color: Colors.amber,
                  child: Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Text('Max Height: ${setToMaxHeight ? 600 : 300}'),
                        const SizedBox(height: 12),
                        ElevatedButton(
                          child: const Text('Toggle BottomSheet Size'),
                          onPressed: () {
                            setState(() {
                              setToMaxHeight = !setToMaxHeight;
                            }); //this

                          },
                        ),
                        const SizedBox(height: 12),
                        ElevatedButton(
                          child: const Text('Close BottomSheet'),
                          onPressed: () => Navigator.pop(context),
                        ),
                      ],
                    ),
                  ),
                );
              });
            },
          );
        },
      ),
    );
  }
}

Take note on isScrollControlled property and set it to true

Paulo
  • 594
  • 3
  • 10
  • it wouldn't work. what I have been trying do is open bottom sheet dialog as wrap content initially. after the checkbox is checked its height should have max value. – aligur Nov 28 '22 at 06:40
  • Can you share here the design of the output you wan to achieve? – Paulo Nov 28 '22 at 06:58
  • added into question – aligur Nov 28 '22 at 07:06
  • 1
    What you want is to update the state of the bottomsheet. Check this https://stackoverflow.com/a/56972160/15649348 this may answer your question – Paulo Nov 28 '22 at 15:06
  • ahh got it. it was pretty easy lol. how coudnt I think it :) anyway, thank you! if you edit your answer I will approve it. – aligur Nov 28 '22 at 15:24
  • Updated the answer. Would also love to see the updated version of the question so every one with the same scenario can see it. Btw, glad to help! – Paulo Nov 28 '22 at 16:07
  • approved and question was updated accordingly. – aligur Nov 29 '22 at 12:44