How to give exact height value to DraggableScrollableSheet in flutter?

492

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!

Share:
492
rahul  Kushwaha
Author by

rahul Kushwaha

Updated on December 16, 2022

Comments

  • rahul  Kushwaha
    rahul Kushwaha over 1 year

    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?