flutter-解决AppBar背景色为白色带滚动时不生效的bug

作为初学flutter的小白来说,今天在模仿小米商城app的首页appbar的时候,遇到一个奇怪的问题,就是修改AppBar的backgroundColor,在首页上拉滚动时,设置的白色背景色不生效,背景色变成不是淡灰色,其他深色不影响,唯独白色不行,flutter版本:3.16.8。

appbar背景色白色的bug

Dart 复制代码
AppBar(
...
//barOpacity是一个0~1.0的透明度的变量,视频中确实到了1的,不用怀疑
backgroundColor: Colors.white.withOpacity(controller.barOpacity.value), 
elevation: 0,
...)

仔细观察,AppBar的背景色一旦设置为白色且无透明度,下拉的时候就会把白色变成浅灰色,即使不加入渐变透明度的逻辑,固定写死也没用,但是改成其他颜色如绿色红色什么的,又好像没问题,到底是什么问题呢?

这让我不得不怀疑这个是不是受到主题风格的影响,于是我搞老搞去,修改了ThemeData,一脸懵逼就把问题解决了,如下代码:

Dart 复制代码
  theme: ThemeData(
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
            //加入colorScheme既可以解决上面的问题
            colorScheme: const ColorScheme.light(
                primary: Colors.white,
                onPrimary: Colors.white,
                onBackground: Colors.white,
                secondary: Colors.white),
          ),
          debugShowCheckedModeBanner: false,
          title: "shop+demo",

如果对你有用,点个赞支持下吧

如果你还有更深的理解,也请回复下你的理解。

相关推荐
wuxianda103012 小时前
uniapp项目上架苹果商店4.3a被拒,3天极速解决方案2026.5.8
前端·人工智能·flutter·uni-app·ios上架·苹果上架·苹果4.3a
段子子16 小时前
【在flutter项目中使用get_cli初始化项目】
flutter
W蘭17 小时前
Flutter从入门到实战-03-综合案例实战
flutter
jiejiejiejie_18 小时前
Flutter For OpenHarmony:共享元素转场的 OpenHarmony 平台实战
flutter
Zender Han18 小时前
Flutter Edge-to-Edge 介绍及适配使用指南
android·flutter·ios
xmdy586618 小时前
Flutter + 开源鸿蒙实战|城市智慧停车管理系统 Day4 停车订单生成+多状态管理+在线缴费+我的订单+会员中心+个人中心完善
flutter·开源·harmonyos
xmdy586618 小时前
Flutter + 开源鸿蒙实战|城市智慧停车管理系统 Day8 进阶美化与真机调优篇
flutter·华为·harmonyos
Zender Han18 小时前
Flutter 高斯模糊介绍与具体实现
android·flutter·ios