Flutter中有意思的滚动效果 - Sliver系列

2023-11-08 13:30

本文主要是介绍Flutter中有意思的滚动效果 - Sliver系列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

滚动效果:

 

出处原文链接:https://segmentfault.com/a/1190000019902201

由于原文代码对屏幕短的手机存在适配问题,本人进行修改,新代码如下:

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return const MaterialApp(title: 'Welcome to Flutter',home: MyHomePage(),);}
}class MyHomePage extends StatefulWidget {const MyHomePage({super.key});@overrideState<StatefulWidget> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {final double _headerCollapsedHeight = 40;final double _headerExpandedHeight = 300;@overrideWidget build(BuildContext context) {//final double height = MediaQuery.of(context).size.height;final MediaQueryData mqd = MediaQuery.of(context);return Scaffold(body: CustomScrollView(slivers: <Widget>[SliverPersistentHeader(pinned: true,delegate: _MySliverHeaderDelegate(title: '哪吒之魔童降世',collapsedHeight: _headerCollapsedHeight,expandedHeight: _headerExpandedHeight,paddingTop: mqd.padding.top,coverImgUrl:'https://img.zcool.cn/community/01c6615d3ae047a8012187f447cfef.jpg@1280w_1l_2o_100sh.jpg',coverImgName: null,),),SliverFillRemaining(hasScrollBody: false,child: FilmContent(minHeight:(mqd.size.height - mqd.padding.top - _headerCollapsedHeight),),),],),);}
}class _MySliverHeaderDelegate extends SliverPersistentHeaderDelegate {_MySliverHeaderDelegate({required this.collapsedHeight,required this.expandedHeight,required this.paddingTop,this.coverImgUrl,this.coverImgName,required this.title,});final double collapsedHeight;final double expandedHeight;final double paddingTop;final String? coverImgUrl;final String? coverImgName;final String title;String _statusBarMode = 'dark';@overridedouble get minExtent => collapsedHeight + paddingTop;@overridedouble get maxExtent => expandedHeight + paddingTop;@overridebool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {return true;}void updateStatusBarBrightness(shrinkOffset) {if (shrinkOffset > 50 && _statusBarMode == 'dark') {_statusBarMode = 'light';SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(statusBarBrightness: Brightness.light,statusBarIconBrightness: Brightness.light,));} else if (shrinkOffset <= 50 && _statusBarMode == 'light') {_statusBarMode = 'dark';SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(statusBarBrightness: Brightness.dark,statusBarIconBrightness: Brightness.dark,));}}Color makeStickyHeaderBgColor(shrinkOffset) {final int alpha =(shrinkOffset / (maxExtent - minExtent) * 255).clamp(0, 255).toInt();return Color.fromARGB(alpha, 255, 255, 255);}Color makeStickyHeaderTextColor(shrinkOffset, isIcon) {if (shrinkOffset <= 50) {return isIcon ? Colors.white : Colors.transparent;} else {final int alpha =(shrinkOffset / (maxExtent - minExtent) * 255).clamp(0, 255).toInt();return Color.fromARGB(alpha, 0, 0, 0);}}@overrideWidget build(BuildContext context, double shrinkOffset, bool overlapsContent) {updateStatusBarBrightness(shrinkOffset);return SizedBox(height: maxExtent,width: MediaQuery.of(context).size.width,child: Stack(fit: StackFit.expand,children: <Widget>[coverImgUrl != null? Image.network(coverImgUrl!, fit: BoxFit.cover): coverImgName != null? Image.asset(coverImgName!, fit: BoxFit.cover): Container(color: Colors.orangeAccent,),Positioned(left: 0,top: maxExtent / 2,right: 0,bottom: 0,child: Container(decoration: const BoxDecoration(gradient: LinearGradient(begin: Alignment.topCenter,end: Alignment.bottomCenter,colors: [Color(0x00000000),Color(0x90000000),],),),),),Positioned(left: 0,right: 0,top: 0,child: Container(color: makeStickyHeaderBgColor(shrinkOffset),child: SafeArea(bottom: false,child: SizedBox(height: collapsedHeight,child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: <Widget>[IconButton(icon: Icon(Icons.arrow_back_ios,color: makeStickyHeaderTextColor(shrinkOffset, true),),onPressed: () => Navigator.pop(context),),Text(title,style: TextStyle(fontSize: 20,fontWeight: FontWeight.w500,color: makeStickyHeaderTextColor(shrinkOffset, false),),),IconButton(icon: Icon(Icons.share,color: makeStickyHeaderTextColor(shrinkOffset, true),),onPressed: () {},),],),),),),),],),);}
}class FilmContent extends StatelessWidget {const FilmContent({super.key, required this.minHeight});final double minHeight;@overrideWidget build(BuildContext context) {return Container(constraints: BoxConstraints(minHeight: minHeight),padding: const EdgeInsets.all(16),child: Column(crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[Row(crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[Container(width: 130,height: 180,decoration: BoxDecoration(borderRadius: BorderRadius.circular(6),// color: Colors.orangeAccent,),clipBehavior: Clip.hardEdge,child: Image.network('https://img1.gamersky.com/image2019/07/20190725_ll_red_136_2/gamersky_07small_14_201972510258D0.jpg',width: 130,height: 180,fit: BoxFit.cover,),),const SizedBox(width: 10),Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[_buildText('哪吒之魔童降世',fontSize: 25,fontWeight: FontWeight.bold,),const SizedBox(height: 10),_buildText('动画/中国大陆/110分钟',color: const Color(0xFF999999),),const SizedBox(height: 8),_buildText('2019-07-26 08:00 中国大陆上映',color: const Color(0xFF999999),),const SizedBox(height: 8),_buildText('32.1万人想看/大V推荐度95%',color: const Color(0xFF999999),),],),),],),const Divider(height: 32),Column(crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[_buildText('剧情简介',fontSize: 25,fontWeight: FontWeight.bold,),const SizedBox(height: 10),_buildText('天地灵气孕育出一颗能量巨大的混元珠,元始天尊将混元珠提炼成灵珠和魔丸,灵珠投胎为人,助周伐纣时可堪大用;而魔丸则会诞出魔王,为祸人间。元始天尊启动了天劫咒语,3年后天雷将会降临,摧毁魔丸。太乙受命将灵珠托生于陈塘关李靖家的儿子哪吒身上。然而阴差阳错,灵珠和魔丸竟然被掉包。本应是灵珠英雄的哪吒却成了混世大魔王。调皮捣蛋顽劣不堪的哪吒却徒有一颗做英雄的心。然而面对众人对魔丸的误解和即将来临的天雷的降临,哪吒是否命中注定会立地成魔?他将何去何从?',color: const Color(0xFF999999),textAlign: TextAlign.justify,),],),],),);}_buildText(String data, {double fontSize = 15,FontWeight? fontWeight,Color? color = const Color(0xFF333333),TextAlign? textAlign,}) {return Text(data,style: TextStyle(fontSize: fontSize,fontWeight: fontWeight,color: color,height: 1,),textAlign: textAlign,overflow: TextOverflow.clip,);}
}

这篇关于Flutter中有意思的滚动效果 - Sliver系列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/370185

相关文章

Flutter打包APK的几种方式小结

《Flutter打包APK的几种方式小结》Flutter打包不同于RN,Flutter可以在AndroidStudio里编写Flutter代码并最终打包为APK,本篇主要阐述涉及到的几种打包方式,通... 目录前言1. android原生打包APK方式2. Flutter通过原生工程打包方式3. Futte

Flutter监听当前页面可见与隐藏状态的代码详解

《Flutter监听当前页面可见与隐藏状态的代码详解》文章介绍了如何在Flutter中使用路由观察者来监听应用进入前台或后台状态以及页面的显示和隐藏,并通过代码示例讲解的非常详细,需要的朋友可以参考下... flutter 可以监听 app 进入前台还是后台状态,也可以监听当http://www.cppcn

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

禁止HTML页面滚动的操作方法

《禁止HTML页面滚动的操作方法》:本文主要介绍了三种禁止HTML页面滚动的方法:通过CSS的overflow属性、使用JavaScript的滚动事件监听器以及使用CSS的position:fixed属性,每种方法都有其适用场景和优缺点,详细内容请阅读本文,希望能对你有所帮助... 在前端开发中,禁止htm

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

使用Python实现生命之轮Wheel of life效果

《使用Python实现生命之轮Wheeloflife效果》生命之轮Wheeloflife这一概念最初由SuccessMotivation®Institute,Inc.的创始人PaulJ.Meyer... 最近看一个生命之轮的视频,让我们珍惜时间,因为一生是有限的。使用python创建生命倒计时图表,珍惜时间

鸿蒙开发搭建flutter适配的开发环境

《鸿蒙开发搭建flutter适配的开发环境》文章详细介绍了在Windows系统上如何创建和运行鸿蒙Flutter项目,包括使用flutterdoctor检测环境、创建项目、编译HAP包以及在真机上运... 目录环境搭建创建运行项目打包项目总结环境搭建1.安装 DevEco Studio NEXT IDE