int selectedIndex = await _showCustomModalBottomSheet(context, _options);
print(“自定义底部弹层:选中了第$selectedIndex个选项”);
},
//…
基本使用
基本使用对于全屏和默认只差一个参数,演示代码中,我们使用了一组模拟的数据构建选项数据,然后再传给显示底部弹窗的方法,实际这组数据大部分是从后台获取的。当 isScrollControlled
是 true
时,则是全屏弹窗,默认是 false
。
Future _showBasicModalBottomSheet(context, List options) async {
return showModalBottomSheet(
isScrollControlled: false,
context: context,
builder: (BuildContext context) {
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(options[index]),
onTap: () {
Navigator.of(context).pop(index);
});
},
itemCount: options.length,
);
},
);
}
需要注意的有四点:
- 弹窗需要上下文的
context
,这是因为