我正在使用Flutter模态底板来显示一些选项供用户选择。我有一个Column带有ListTiles 的列表作为底部工作表的内容。
我的问题是,如果我有6 ListTiles 以上的时间,某些将被切断而不显示。
有没有办法使底部的工作表可滚动?
Dar*_*ish 15
可以使用showModalBottomSheet的isScrollControlled属性来实现效果。
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('DraggableScrollableSheet'),
),
body: SizedBox.expand(
child: DraggableScrollableSheet(
builder: (BuildContext context, ScrollController scrollController) {
return Container(
color: Colors.blue[100],
child: ListView.builder(
controller: scrollController,
itemCount: 25,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text('Item $index'));
},
),
);
},
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是 Flutter 团队的官方视频。
可以在此处找到 DartPad 的现场演示。
小智 15
与此几乎相同的解决方案,但没有不必要的手势检测器层等。
重要的部分是expand: false,in DraggableScrollableSheet,因为它默认为 true 。这会导致底部工作表在默认配置中扩展至完整高度。将其设置为 false 后,无需用两个手势检测器包裹底部纸张来检测外部轻击。
同样在这种情况下只需要一种形状。
showModalBottomSheet(
context: context,
isScrollControlled: true,
isDismissible: true,
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.vertical(top: Radius.circular(16))),
builder: (context) => DraggableScrollableSheet(
initialChildSize: 0.4,
minChildSize: 0.2,
maxChildSize: 0.75,
expand: false,
builder: (_, controller) => Column(
children: [
Icon(
Icons.remove,
color: Colors.grey[600],
),
Expanded(
child: ListView.builder(
controller: controller,
itemCount: 100,
itemBuilder: (_, index) {
return Card(
child: Padding(
padding: EdgeInsets.all(8),
child: Text("Element at index($index)"),
),
);
},
),
),
],
),
),
);
Run Code Online (Sandbox Code Playgroud)
Eli*_*les 10
我找到了一个实现以下代码的解决方案
void _showBottomSheet(BuildContext context) {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) {
return GestureDetector(
onTap: () => Navigator.of(context).pop(),
child: Container(
color: Color.fromRGBO(0, 0, 0, 0.001),
child: GestureDetector(
onTap: () {},
child: DraggableScrollableSheet(
initialChildSize: 0.4,
minChildSize: 0.2,
maxChildSize: 0.75,
builder: (_, controller) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(25.0),
topRight: const Radius.circular(25.0),
),
),
child: Column(
children: [
Icon(
Icons.remove,
color: Colors.grey[600],
),
Expanded(
child: ListView.builder(
controller: controller,
itemCount: 100,
itemBuilder: (_, index) {
return Card(
child: Padding(
padding: EdgeInsets.all(8),
child: Text("Element at index($index)"),
),
);
},
),
),
],
),
);
},
),
),
),
);
},
);
}
Run Code Online (Sandbox Code Playgroud)
只需将您更改Column为ListView,如下所示:
return ListView(
children: <Widget>[
...
]
);
Run Code Online (Sandbox Code Playgroud)
如果我不希望滚动工作表的内容,而是滚动工作表本身,该怎么办?
如果您希望用户能够向上滑动底部面板以填充屏幕,恐怕在模块化底部面板的当前实现中这是不可能的。
| 归档时间: |
|
| 查看次数: |
1888 次 |
| 最近记录: |