Reputation: 3627
I use showRoundedModalBottomSheet
, how can I adjust this modal height till the appbar?
Upvotes: 136
Views: 161257
Reputation: 379
In the widget build your bottom sheet like the one below,
@override
Widget build(BuildContext context) {
return Scaffold(
body: Builder(builder: (context) {
return Center(
child: TextButton(
child: Text("Show bottom sheet"),
onPressed: () => _showBottomSheet(context),
),
);
}),
);
}
Now in the _showBottomSheet(context)
add the below lines to set the height that you need (a percentage value from device height)
showModalBottomSheet(
context: context,
isScrollControlled: true,
constraints: BoxConstraints.loose(Size.fromHeight(mediaQueryData.size.height * 0.75)),
builder: (context) {
return Flex(
direction: Axis.vertical,
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Flexible(
child: ListView(
shrinkWrap: true,
children: [
//list items
]
);],
);
},
);
}
}
This will help you set the bottom sheet height as a percentage of the screen height.
Upvotes: 0
Reputation: 1
showModalBottomSheet(
context: context,
builder: (ctx) => Container(),
isScrollControlled: true,
**useSafeArea**: true);
Upvotes: 0
Reputation: 2205
A way to do it without isScrollControlled: true
(which adds a scrollable field and can mess with layout)
return showModalBottomSheet(
context: context,
useSafeArea: true,
constraints: BoxConstraints(
maxHeight: double.infinity,
),
scrollControlDisabledMaxHeightRatio: 1,
builder: (context) {
// your widget
},
);
you need to both allow the constraints to take up any space, and set the scrollControlDisabledMaxHeightRatio to 1 to allow it to go full screen useSafeArea stops it from expanding over the status bar
Upvotes: 6
Reputation: 359
You can also use DraggableScrollableSheet
to adjust the height of the bottom sheet and handle scrolling :
showModalBottomSheet(
context: context,
useRootNavigator: true,
isScrollControlled: true,
enableDrag: true,
builder: (_) {
return DraggableScrollableSheet(
minChildSize: minSize,
maxChildSize: maxSize,
expand: false,
initialChildSize: initSize,
builder: (context, scrollController) {
return YourWidget();
},
);
},
);
Upvotes: 1
Reputation: 509
Using constraints and isScrollControlled fields like this:
showModalBottomSheet(
context: context,
constraints: BoxConstraints(minHeight: context.mediaQuerySize.height * .9, maxHeight: context.mediaQuerySize.height * .9),
isScrollControlled: true,
builder: (context) => YourWidget(),
)
but we should implement both minHeight and maxHeight.
Upvotes: 3
Reputation: 817
You can control the height by using FractionallySizedBox
and set useSafeArea
to true
to get the accurate useable height without notch.
showModalBottomSheet(
context: context,
isScrollControlled: true,
useSafeArea: true,
builder: (_) {
return FractionallySizedBox(
heightFactor: 0.85,
child: Container(),
);
},
);
Upvotes: 5
Reputation: 44
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) {
return DraggableScrollableSheet(
builder: (
BuildContext context,
// Use scrollController for listView inside content
ScrollController scrollController,
) {
// Your content here <---
return Scaffold();
},
initialChildSize: 0.95,
maxChildSize: 0.95,
minChildSize: 0.90,
);
},
);
Upvotes: 0
Reputation: 1059
The default height for BottomSheet is half the screen size. If you want your BottomSheet to EXPAND according to your content DYNAMICALLY.
showModalBottomSheet<dynamic>(
isScrollControlled: true,
context: context,
builder: (BuildContext bc) {
return Wrap(
children: <Widget>[...]
)
}
)
This will automatically expand the BottomSheet according to the content inside.
Upvotes: 6
Reputation: 257
You can wrap the contents in a Container and provide height to full height
await showModalBottomSheet(
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30), topRight: Radius.circular(30))),
backgroundColor: Colors.white,
builder: (BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height,
child: ListView(
)
)
}
}
Upvotes: 0
Reputation: 2867
What worked for me was returning the modal's content wrapped in a DraggableScrollableSheet
:
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
isScrollControlled: true,
isDismissible: true,
builder: (BuildContext context) {
return DraggableScrollableSheet(
initialChildSize: 0.75, //set this as you want
maxChildSize: 0.75, //set this as you want
minChildSize: 0.75, //set this as you want
expand: true,
builder: (context, scrollController) {
return Container(...); //whatever you're returning, does not have to be a Container
}
);
}
)
Upvotes: 31
Reputation: 2846
You can control the height by using FractionallySizedBox and setting the isScrollControlled to true.
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (context) {
return FractionallySizedBox(
heightFactor: 0.9,
child: Container(),
);
});
Upvotes: 239
Reputation: 301
You can modify this method in the definition of the bottom sheet. Normally, it is 9.0, but as you can see here I changed it to 13.0. 16.0 is full screen.
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return BoxConstraints(
minWidth: constraints.maxWidth,
maxWidth: constraints.maxWidth,
minHeight: 0.0,
maxHeight: isScrollControlled
? constraints.maxHeight
: constraints.maxHeight * 13.0 / 16.0,
);
}
Upvotes: 0
Reputation: 10101
[Update]
In showModalBottomSheet(...)
set the property isScrollControlled:true
.
It will make bottomSheet to full height.
[Original answer]
You can Implement the FullScreenDialog instead.
Flutter Gallery app has an example of FullScreenDialog
You can open your Dialog using below code:
Navigator.of(context).push(new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return Dialog();
},
fullscreenDialog: true
));
Check this blog post too for more:
Hope it will help you.
Upvotes: 243
Reputation: 37969
I guess the easiest way is:
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) => Wrap(children: [YourSheetWidget()]),
);
Upvotes: 27
Reputation: 2247
If you call showModalBottomSheet()
with isScrollControlled: true
, then the dialog will be allowed to occupy the whole height.
To adjust the height to the content, you can proceed as usually, for example, using Container
and Wrap
widgets.
Example:
final items = <Widget>[
ListTile(
leading: Icon(Icons.photo_camera),
title: Text('Camera'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.photo_library),
title: Text('Select'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.delete),
title: Text('Delete'),
onTap: () {},
),
Divider(),
if (true)
ListTile(
title: Text('Cancel'),
onTap: () {},
),
];
showModalBottomSheet(
context: context,
builder: (BuildContext _) {
return Container(
child: Wrap(
children: items,
),
);
},
isScrollControlled: true,
);
Upvotes: 45
Reputation: 427
from
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return BoxConstraints(
minWidth: constraints.maxWidth,
maxWidth: constraints.maxWidth,
minHeight: 0.0,
maxHeight: constraints.maxHeight * 9.0 / 16.0
);}
to
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return BoxConstraints(
minWidth: constraints.maxWidth,
maxWidth: constraints.maxWidth,
minHeight: 0.0,
maxHeight: constraints.maxHeight
);}
Upvotes: 8