I am currently trying to make a bottom sheet as below:
Container(
child: TextButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double maxHeight = (2 / 3) * MediaQuery.of(context).size.height;
return Container(
height: constraints.maxHeight > maxHeight
? maxHeight
: constraints.maxHeight,
child: _BottomSheetWidget(),
);
},
);
},
isScrollControlled: true, // Ensures the sheet is fully visible on smaller screens
);
},
child: Text('Open Bottom Sheet'),
),
)
Here is my bottom sheet widget:
class _BottomSheetWidget extends StatelessWidget {
const _BottomSheetWidget();
@override
Widget build(BuildContext context) {
return SafeArea(
child: SingleChildScrollView(
child: Column(
[
// ...Some dynamic content
]
)
)
)
}
}
What I currently do is make the bottom sheet always have 2 / 3 screen width (even if the content height is less than 2 / 3 of the screen size, and therefore always make an empty section in the bottom sheet).
The column content is dynamic (based on the count data fetched). I want the bottom sheet height to fit with the content if the content height is less than 2 / 3 of the screen size. But if the content height is more than 2 / 3 of the screen size, I want the bottom sheet height is 2 / 3 of the screen size, and therefore the bottom sheet to be scrollable. How to do it then?
3
Answers
Instead of using LayoutBuilder, you can wrap your Modal with SizedBox
Don’t use SingleChildScrollView as your parent in BottomSheetWidget, cause the SingleChildScrollView’s height will adjust it child’s height. You can move it inside the Column like this
You are close to your desired output. Just add
width: double.infinity