rahul  Kushwaha
rahul Kushwaha

Reputation: 2819

How to give exact height value to DraggableScrollableSheet in flutter?

I have simple DraggableScrollableSheet in flutter and it has parameter like initialChieldSize, minChildSize, maxChildSize but they take value in ftaction of parent height.

But I have fix height Elements and want to give exact height in value to initialChildSize.

how to give exact value to the above parameters in DraggableScrollableSheet?

Upvotes: 3

Views: 2157

Answers (1)

rasuru
rasuru

Reputation: 451

Try getting MediaQuery's size or any other custom parent widget's size (use Layout Builder for custom size). Then give the DraggableScrollableSheet initial and min child size of <you_desired_height> / size.height. This way you will get what the fraction for this particular height is. It worked for me :)

Here is an example:

return LayoutBuilder(
  builder: (context, safeAreaSize) {
    return Stack(
      children: [
        Placeholder(),
        Positioned.fill(
          child: DraggableScrollableSheet(
            initialChildSize: 40 / safeAreaSize.maxHeight,
            minChildSize: 40 / safeAreaSize.maxHeight,
            maxChildSize: 1.0,
            expand: false,
            builder: (context, scrollController) {
              return SingleChildScrollView(
                controller: scrollController,
                child: Container(height: 40).backgroundColor(Colors.blue),
              );
            },
          ),
        ),
      ],
    );
  },
);

If you want to see it in action go to: https://dartpad.dev/d89a5411bb9785edfb630f6082b71d00

Edit: I found a package called "sliding_sheet" on pub.dev and it's neat. It can auto-resize the sheet based on content, and it can snap at places you want on the screen. I give the same value as in above code to the first of "snappings" (it's an argument to SlidiingSheet contructor). Hope you will find it useful!

Upvotes: 4

Related Questions