尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
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(); } }
RELATED

相关推荐

MediaInfo 快速上手:看懂视频音频文件的完整参数指南

MediaInfo 快速上手:看懂视频音频文件的完整参数指南

MediaInfo 快速上手:看懂视频音频文件的完整参数指南 【免费下载链接】MediaInfo Convenient unified display of the most relevant technical and tag data for video and audio files. 项目地址: https://gitcode.com/gh_mirrors/me/MediaInfo MediaInfo …

📅 2026/11/7 4:27:20
GetQzonehistory完整指南:3步保存你的QQ空间历史说说

GetQzonehistory完整指南:3步保存你的QQ空间历史说说

GetQzonehistory完整指南:3步保存你的QQ空间历史说说 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory 你有没有翻过QQ空间,却发现某些明明写了很久的说说已经不见…

📅 2026/10/19 0:01:42
一键导出QQ空间十年老说说:GetQzonehistory使用全流程

一键导出QQ空间十年老说说:GetQzonehistory使用全流程

一键导出QQ空间十年老说说:GetQzonehistory使用全流程 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory GetQzonehistory 是一款开源的 QQ 空间历史说说导出工具,它…

📅 2026/11/6 0:49:54
MORE NEWS

更多资讯

📰

培训电工证江阴报考条件与考试时间

电工证过期了不知道怎么复审?江阴官方回应来了 证书过期了不知道怎么复审延期,这个问题我每年至少要接到五六个电话。特别是江阴这边,很多电工朋友因为工作变动或者忙于生计,一不小心就把证给放过了。最近官方回应, 特种作业操作证…

📰

呈贡学电工证去哪里考前押题怎么选培训机构

呈贡学电工证去哪里考前押题怎么选培训机构 实操考试心里没底怕挂科,这是很多想在呈贡学电工证的朋友最头疼的问题。尤其是像洛阳涧西区那些从事公路工程的朋友,电工证几乎是进入施工现场的“门槛证”。考前如果没准备好,实操一出错,轻则挂科重则影响职业…

📰

电工证湖南实操考试心里没底怕挂科?官方回应帮你拿捏关键点

电工证湖南实操考试心里没底怕挂科?官方回应帮你拿捏关键点 你是不是也担心电工证湖南实操考试时手忙脚乱,心里没底怕挂科?尤其是在郑州的建筑工人群体里,很多人因为不了解考试流程、题型和评分标准,导致复习不到位、考试失误,最终耽误了工作和项目进度…

📰

龙江县电工证培训机构值不值得考?别被中介坑了

龙江县电工证培训机构值不值得考?别被中介坑了 你是不是也这样,想考电工证却不知道去哪报名,怕被中介骗钱?别急,我这有实操经验,今天就给你讲讲 龙江县电工证培训机构 到底值不值得考,怎么避开那些坑,顺利拿证。…

📰

晋城电工证去哪里报名千万别踩坑,应届生求职必备

晋城电工证去哪里报名千万别踩坑,应届生求职必备 急需特种作业证才能上岗找工作,偏偏你还没拿到电工证?在晋城,想考电工证的人不少,但真正知道怎么报名、怎么备考、怎么避坑的人却不多。很多人为了省事,随便找了个培训机构报名,结果一考试就被淘汰,钱…

📰

电工证学时可以累计吗?多久拿证才够用?

电工证学时可以累计吗?多久拿证才够用? 你现在急需特种作业证才能上岗找工作,可你又担心电工证学时能不能累计?多久拿证才靠谱?别急,这篇文章就给你讲明白。 电工证学时真的能累计吗? 很多人在报考电工证前,最纠结的一个问题就是:…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬