// 封装一个方便的函数
static Future<T> showBottomSheet<T>(BuildContext context, Widget content) {
return showModalBottomSheet(
context: context,
isScrollControlled: true, //允许bottomSheet高度自定义
backgroundColor: Colors.transparent, //背景透明,保证下面的圆角矩形裁剪有效
builder: (BuildContext c) {
return ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12), topRight: Radius.circular(12)),
child: Container(color: Colors.white, child: content));
});
}
设置最大高度:
content = IntrinsicHeight(
child: Container(
constraints: BoxConstraints(maxHeight: maxHeight),
child: SingleChildScrollView(child: Column(children: list))));
IntrinsicHeight:高度按子控件真实高度计算;
SingleChildScrollView:当超出最大高度后,出现滚动条;
BottomSheet中setState失效:
因为该setState并非BottomSheet的,而是当前页面的。解决方法是,使用StatefulBuilder包装下:
content = StatefulBuilder((c, setter){
return ...onPressed: (){ setter((){ doUpdate(); }); }...
});