如何设置 showModalBottomsheet 为全高?

我使用 showRoundedModalBottomSheet,如何调整这个模态高度直到应用程序栏?

enter image description here

83289 次浏览

[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.

  1. You open class BottomSheet in library of flutter and change maxHeight

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
);}
  1. You can create a new class with other name and copy source code from class BottomSheet and change maxHeight

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,
);

I guess the easiest way is:

showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) => Wrap(children: [YourSheetWidget()]),
);

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
}
);
}
)

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,
);
}

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(),
);
});

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(
)
)
}
}