0

I want to set the height for my cards based on the height of the widgets in it. I use globalKey and set it in my widget but return null.

can anyone help me how can I do that?

 final GlobalKey _Key = GlobalKey();


 showModalBottomSheet(
                        context: context,
                        useRootNavigator: true,
                        isScrollControlled: true,
                        isDismissible: true,
                        enableDrag: true,
                        builder: (context) {
                          return Container(
                            key : _key,
                            child: ListView(
                              shrinkWrap: true,
                              padding: EdgeInsets.zero,
                              children: [
                                PressWidget(),
                                PaddingButtonCard(),
                              ],
                            ),
                          );
                        },
                      );

when I print _Key.currentContext?.findRenderObject().size , return null.

user17838882
  • 93
  • 2
  • 14
  • You would need to do this call after the widget has been built, not inside the build function. Do you already do this? (If not, you can try it out by simply making a button in your interface print this value). I'm not entirely sure how the `size` works, unfortunately. – fravolt Jul 06 '22 at 07:29

1 Answers1

0

A Widget like a card should automatically wrap it's contents, so unless the contents have an unconstrained height (like a ListView or Expanded widget), you shouldn't need measuring or such techniques to achieve this.

Try, as a test, putting a Container with a set height inside your Card, and observe how the card automatically sizes itself:

Card(
  child: Container(
    height: 40,
    width: double.maxFinite,
    color: Colors.purple,
    child: Text('Purple Card'),
  ),
),
fravolt
  • 2,565
  • 1
  • 4
  • 19