最终实现效果如图片所示,分布演示了基础的,全屏的和自定义的底部弹窗形式。
在消息页面 message.dart 中,使用 Column 组件构建了三个按钮,点击每个按钮调用不同的底部弹窗显示。这部分代码不展示,核心注意的方式是按钮的 onPressed 响应方法,需要使用 async 修饰,这是因为 ModalBottomSheet 的返回结果是一个 Future 对象,需要通过 await 来获取返回结果。
onPressed: () async { int selectedIndex = await _showCustomModalBottomSheet(context, _options); print("自定义底部弹层:选中了第$selectedIndex个选项"); }, //...
基本使用对于全屏和默认只差一个参数,演示代码中,我们使用了一组模拟的数据构建选项数据,然后再传给显示底部弹窗的方法,实际这组数据大部分是从后台获取的。当 isScrollControlled 是 true 时,则是全屏弹窗,默认是 false。
Future<int> _showBasicModalBottomSheet(context, List<String> options) async { return showModalBottomSheet<int>( 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, ); }, ); }
需要注意的有四点:
在自定义底部弹窗中,我们做了如下自定义项:
自定义弹窗的代码如下所示:
Future<int> _showCustomModalBottomSheet(context, List<String> options) async { return showModalBottomSheet<int>( backgroundColor: Colors.transparent, isScrollControlled: true, context: context, builder: (BuildContext context) { return Container( clipBehavior: Clip.antiAlias, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only( topLeft: const Radius.circular(20.0), topRight: const Radius.circular(20.0), ), ), height: MediaQuery.of(context).size.height / 2.0, child: Column(children: [ SizedBox( height: 50, child: Stack( textDirection: TextDirection.rtl, children: [ Center( child: Text( '底部弹窗', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 16.0), ), ), IconButton( icon: Icon(Icons.close), onPressed: () { Navigator.of(context).pop(); }), ], ), ), Divider(height: 1.0), Expanded( child: ListView.builder( itemBuilder: (BuildContext context, int index) { return ListTile( title: Text(options[index]), onTap: () { Navigator.of(context).pop(index); }); }, itemCount: options.length, ), ), ]), ); }, ); }
这里有几个额外需要注意的点:
本篇介绍了三种 ModalBottomSheet 的方式, 可以看到 ModalBottomSheet 非常灵活。实际开发过程中,还可以根据需要,利用 ModalBottomSheet的 builder 方法返回不同的组件进而定制自己的底部弹层组件,能够满足绝大多数场景。同时,借 ModalBottomSheet 的启发,我们自己也可以使用 Navigator方法来实现其他形式的弹层,例如从底部弹出登录页,登录后再返回原页面。