flutter实现滚动导航栏发生透明变化 采用的是flutter的getx作为的状态管理product_content_view.dartimport package:flutter/material.dart; import package:flutter_app/app/components/AppBarTop.dart; import package:flutter_app/app/services/screenAdapter.dart; import package:get/get.dart; import ../controllers/product_content_controller.dart; class ProductContentView extends GetViewProductContentController { const ProductContentView({super.key}); override Widget build(BuildContext context) { return Obx(() { // ✅ Obx 包裹后opacity.value 变化会触发整个 Scaffold 重绘 final appBarBgOpacity controller.opacity.value; return Scaffold( extendBodyBehindAppBar: true, // 实现透明导航栏 appBar: AppBarTop( showBackButton: true, titleWidget: SizedBox( width: ScreenAdapter.width(400), height: ScreenAdapter.height(96), child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Padding( padding: const EdgeInsets.all(0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 商品, style: TextStyle( fontSize: ScreenAdapter.fontSize(36), color: Colors.black, ), ), Container( margin: EdgeInsets.only(top: ScreenAdapter.height(10)), width: ScreenAdapter.width(100), height: ScreenAdapter.height(2), color: Colors.red, ) ], ), ), Padding( padding: const EdgeInsets.all(0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 详情, style: TextStyle( fontSize: ScreenAdapter.fontSize(36), color: Colors.black, ), ), Container( margin: EdgeInsets.only(top: ScreenAdapter.height(10)), width: ScreenAdapter.width(100), height: ScreenAdapter.height(2), color: Colors.red, ) ], ), ), Padding( padding: const EdgeInsets.all(0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 推荐, style: TextStyle( fontSize: ScreenAdapter.fontSize(36), color: Colors.black, ), ), Container( margin: EdgeInsets.only(top: ScreenAdapter.height(10)), width: ScreenAdapter.width(100), height: ScreenAdapter.height(2), color: Colors.red, ) ], ), ), ], ), ), backgroundColor: Colors.white.withOpacity(appBarBgOpacity), actions: [ Container( margin: EdgeInsets.only(right: ScreenAdapter.width(20)), width: ScreenAdapter.width(88), height: ScreenAdapter.width(88), child: ElevatedButton( onPressed: () {}, style: ButtonStyle( //注意:去掉button默认的padding padding: MaterialStateProperty.all(const EdgeInsets.all(0)), alignment: Alignment.center, backgroundColor: MaterialStateProperty.all( Colors.black12.withOpacity(0.1 appBarBgOpacity * 0.02), ), foregroundColor: MaterialStateProperty.all(Colors.white), shape: MaterialStateProperty.all( const CircleBorder(), ), ), child: Icon( Icons.file_upload_outlined, color: appBarBgOpacity 0.5 ? Colors.black : Colors.white, )), ), Container( margin: EdgeInsets.only(right: ScreenAdapter.width(20)), width: ScreenAdapter.width(88), height: ScreenAdapter.width(88), child: ElevatedButton( onPressed: () {}, style: ButtonStyle( //注意 padding: MaterialStateProperty.all(const EdgeInsets.all(0)), alignment: Alignment.center, backgroundColor: MaterialStateProperty.all( Colors.black12 .withOpacity(0.1 appBarBgOpacity * 0.02), ), foregroundColor: MaterialStateProperty.all(Colors.white), shape: MaterialStateProperty.all(const CircleBorder())), child: Icon( Icons.more_horiz_rounded, color: appBarBgOpacity 0.5 ? Colors.black : Colors.white, )), ), ], ), body: ListView( // ✅ 必须把 controller 的 scrollController 绑定到 ListView否则 listener 永远不触发 controller: controller.scrollController, children: [ Container( width: ScreenAdapter.width(1080), height: ScreenAdapter.height(780), color: Colors.orange, ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ListTile( title: Text(我是一个内容列表), ), ], ), ); }); } }product_content_controllerimport package:flutter/material.dart; import package:get/get.dart; class ProductContentController extends GetxController { final ScrollController scrollController ScrollController(); /// 导航栏透明度0~1 final RxDouble opacity 0.0.obs; /// 触发完全不透明的滚动距离阈值 final double opacityThreshold 200; override void onInit() { super.onInit(); scrollController.addListener(_onScroll); } void _onScroll() { // 还没 attach 到 viewport 时 position 可能不存在防止报错 if (!scrollController.hasClients) return; final pixels scrollController.position.pixels; if (pixels 0) { opacity.value 0; } else if (pixels opacityThreshold) { opacity.value 1; } else { // 0 ~ threshold 之间做线性插值范围始终在 0~1 opacity.value (pixels / opacityThreshold).clamp(0.0, 1.0); } } override void onReady() { super.onReady(); } override void onClose() { scrollController.removeListener(_onScroll); scrollController.dispose(); super.onClose(); } }

相关新闻

最新新闻

系统架构设计师考试精华知识点汇总

系统架构设计师考试精华知识点汇总

系统架构设计师考试精华知识点汇总 考前最后一搏!这是一份精简的精华知识点汇总,涵盖考试最核心的内容。 一、计算机组成 CPU组成: - 运算器:ALU、累加器、暂存寄存器 - 控制器:PC、IR、指令译码器 - 寄存器:通用寄存器、专用寄存器存储层次: 寄存器 → Cache(L1/L2/…

2026/8/22 23:35:25
架构师的技术视野:从本土到全球的成长之路

架构师的技术视野:从本土到全球的成长之路

架构师的技术视野:从本土到全球的成长之路 想象一下:你的煎饼摊生意不错,但如果你只知道这一家煎饼摊的玩法,你就永远做不大。 架构师也一样,需要开阔的技术视野。 为什么需要技术视野 技术视野 = 知道行业内有哪些技术趋势和最佳实践没有技术视野的问题: ❌ 重复造轮…

2026/8/22 23:35:25
智能制造数字化工厂规划建设方案,企业转型必读!

智能制造数字化工厂规划建设方案,企业转型必读!

很多工厂还停留在“设备堆满车间、流程全靠人盯”的阶段。生产计划拍脑袋定,物料经常缺或压库存,设备一坏就停产半天,质检靠人工抽检漏掉一堆问题。不同系统数据不通,车间主任每天被电话追着问进度,老板想查实时成本得…

2026/8/22 23:35:25
未来十年的企业竞争,是智能体竞争

未来十年的企业竞争,是智能体竞争

引言 多数管理者对 AI 的定位还停留在降本工具:省几个人、快几个流程,算的是成本的账。向量空间JBoltAI 在与企业决策层的交流里观察到另一个趋势:竞争的资产形态正在换轨。设备、产能、人才的比较优势仍然有效,但一层新的竞争资产…

2026/8/22 23:35:25
显卡驱动残留怎么清?Display Driver Uninstaller(DDU)使用指南

显卡驱动残留怎么清?Display Driver Uninstaller(DDU)使用指南

显卡驱动残留怎么清?Display Driver Uninstaller(DDU)使用指南 【免费下载链接】display-drivers-uninstaller Display Driver Uninstaller (DDU) a driver removal utility / cleaner utility 项目地址: https://gitcode.com/gh_mirrors/d…

2026/8/22 23:35:25
如何在手机上流畅玩舰娘HTML5:GotoBrowser安卓手游网页浏览器完整指南

如何在手机上流畅玩舰娘HTML5:GotoBrowser安卓手游网页浏览器完整指南

如何在手机上流畅玩舰娘HTML5:GotoBrowser安卓手游网页浏览器完整指南 【免费下载链接】GotoBrowser Android Browser for KanColle 2nd Phase (HTML5) 项目地址: https://gitcode.com/gh_mirrors/go/GotoBrowser GotoBrowser是一款免费开源的舰娘HTML5版专用…

2026/8/22 23:30:25