Flutter3开启Material3时透明AppBar设透明shadowColor仍显阴影
问题场景
实现带透明AppBar的页面布局,要求滚动内容显示在AppBar下方,已完成以下配置:
- 设置AppBar
shadowColor为透明色 - 设置AppBar
elevation为0.0 - 设置AppBar
bottomOpacity为0.0 - 设置AppBar背景为透明
- 开启Scaffold的
extendBodyBehindAppBar: true
实际运行时内容滚动过程中AppBar仍会显示阴影,经排查触发条件为应用主题设置useMaterial3: true,当前使用Flutter版本为3.0.2。
滚动时的异常阴影效果参考:滚动时AppBar阴影截图
复现代码
Stack( fit: StackFit.expand, children: [ //AuthBackground(), Container(color: Colors.brown,), Theme( data: AppStyles.mainDarkTheme.copyWith( textTheme: AppStyles.mainDarkTheme.textTheme.apply( bodyColor: Colors.blue, displayColor: Colors.blue, ) ), child: Scaffold( backgroundColor: Colors.transparent, extendBodyBehindAppBar: true, appBar: AppBar( backgroundColor: Colors.transparent, shadowColor: Colors.transparent, elevation: 0.0, bottomOpacity: 0.0, ), body: _content(), ), ), ], )
解决方案
问题根源是Material3设计规范下,AppBar滚动时的高度提示效果不再仅通过elevation投影实现,新增了表面色调(surfaceTint)叠加层,之前配置的shadowColor、elevation参数仅控制传统投影效果,无法关闭这个色调叠加层。
只需要在AppBar配置中新增surfaceTintColor: Colors.transparent即可完全关闭滚动时的阴影/色调效果,修改后的AppBar代码如下:
appBar: AppBar( backgroundColor: Colors.transparent, shadowColor: Colors.transparent, surfaceTintColor: Colors.transparent, // 关闭M3表面色调叠加 elevation: 0.0, bottomOpacity: 0.0, ),
如果项目中所有透明AppBar都需要该效果,可以直接在全局主题的AppBarTheme中统一配置surfaceTintColor: Colors.transparent,无需逐个页面单独设置。
内容的提问来源于stack exchange,提问作者testerino
相关产品推荐
相关产品推荐

