Flutter中Scaffold的bottomSheet能否设置透明以透出下层评论内容?
问题原因
Scaffold 自带的 bottomSheet 默认绑定了主题的画布背景色,同时自带阴影 elevation,所以即使你给子组件设置透明,上层的默认背景也会挡住下层内容。
解决方案
方案1:直接修改Scaffold bottomSheet默认属性(改动最小)
直接关闭Scaffold bottomSheet的自带背景和阴影即可,修改后的代码如下:
class PostDetail extends StatelessWidget { final int maxRenderImgCnt = 4; final Post post; PostDetail(this.post); @override Widget build(BuildContext context) { return Scaffold( appBar: CustomAppBar( ... ), body: SingleChildScrollView( ... ), // 关闭bottomSheet默认背景色 bottomSheetBackgroundColor: Colors.transparent, // 关闭bottomSheet默认阴影 bottomSheetElevation: 0, bottomSheet: CommonTextField(onTap: null, editTarget: null), ); } }
如果你的Flutter版本低于3.0,没有bottomSheetBackgroundColor属性,可以给子组件外层套一层透明的Material:
bottomSheet: Material( color: Colors.transparent, elevation: 0, child: CommonTextField(onTap: null, editTarget: null), ),
方案2:改用Stack布局实现(灵活度最高)
如果后续还要做更多交互动效、或者需要自定义悬浮位置,推荐直接弃用Scaffold的bottomSheet参数,用Stack把输入层叠在页面内容上方,默认完全透明:
@override Widget build(BuildContext context) { return Scaffold( appBar: CustomAppBar( ... ), body: Stack( children: [ // 下层原页面内容,会直接透过上层透明区域显示 SingleChildScrollView( ... ), // 上层输入/评论视图,不设置背景就是完全透明 Positioned( left: 0, right: 0, bottom: 0, child: CommonTextField(onTap: null, editTarget: null), ) ], ), ); }
额外注意
如果需要让透明区域的点击事件透传到下层,只需要给不需要响应点击的透明区域包一层IgnorePointer即可,如果是部分区域响应的话可以单独配置GestureDetector的行为。
效果示意图:
内容的提问来源于stack exchange,提问作者Yeonghyeon Ko
相关产品推荐
相关产品推荐

