Flutter实现动画卡片式Tab导航 | 掘金技术征文
生活随笔
收集整理的这篇文章主要介绍了
Flutter实现动画卡片式Tab导航 | 掘金技术征文
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
前言
本人接触Flutter不到一个月,深深感受到了这个平台的威力,于是不断学习,Flutter官方Example中的flutter_gallery,很好的展示了Flutter各个widget的功能
其中的animation内容,展示的是一个非常漂亮的 带有动画和拖拽功能的 可展开的卡片式Tab导航,非常漂亮,但是其实现没有抽象出一个可供第三方使用的Widget出来,而且其页面内容的定制性不够友好,滑动的时候也有bug,我在他的基础上进行了优化
官方展示了一个非常好的开源示例,我改造了一下,也不敢独自享用,现在分享给大家,欢迎大家多多交流
外观
实现
这里是我的代码: GitHub/Realank
想使用这个控件非常简单,首先定义页面数据:
const Color _mariner = const Color(0xFF3B5F8F); const Color _mediumPurple = const Color(0xFF8266D4); const Color _tomato = const Color(0xFFF95B57); const Color _mySin = const Color(0xFFF3A646);List<CardSection> allSections = <CardSection>[new CardSection(title: 'First Page',leftColor: _mediumPurple,rightColor: _mariner,contentWidget: Center(child: new Text('第一页'))),new CardSection(title: 'Second Page',leftColor: _mariner,rightColor: _mySin,contentWidget: Center(child: new Text('第二页'))),new CardSection(title: 'Third Page',leftColor: _mySin,rightColor: _tomato,contentWidget: Center(child: new Text('第三页'))),new CardSection(title: 'Forth Page',leftColor: _tomato,rightColor: Colors.blue,contentWidget: Center(child: new Text('第四页'))),new CardSection(title: 'Fifth Page',leftColor: Colors.blue,rightColor: _mediumPurple,contentWidget: Center(child: new Text('第五页'))), ]; 复制代码然后创建这个控件:
void main() => runApp(new MyApp());class MyApp extends StatelessWidget {// This widget is the root of your application.@overrideWidget build(BuildContext context) {return new MaterialApp(title: 'Flutter Demo',home: new Scaffold(body: Center(child: new AnimateTabNavigation(sectionList: allSections,),),),);} } 复制代码大功告成
原理
知其然还要知其所以然,下面来说说这个控件的实现原理
首先,在sections.dart里定义了数据结构:
class CardSection {CardSection({this.title, this.leftColor, this.rightColor, this.contentWidget});final String title;final Color leftColor;final Color rightColor;final Widget contentWidget;bool operator ==(Object other) {if (other is! CardSection) return false;final CardSection otherSection = other;return title == otherSection.title;}int get hashCode => title.hashCode; }复制代码它定义了其中一个卡片的标题,左边颜色和右边颜色(为了显示过渡颜色效果),以及子控件(这个是我改进的,这样可以别人使用的时候随意添加控件)
然后在widgets.dart中定义了几个widget:
- SectionCard : 标题卡片
- SectionTitle : 标题
- SectionIndicator : 标题下的装饰线
最后在cardNavigation.dart中就是布局这些内容啦,这里面代码很复杂,其思路倒是不难:
从 0 到 1:我的 Flutter 技术实践 | 掘金技术征文,征文活动正在进行中
总结
以上是生活随笔为你收集整理的Flutter实现动画卡片式Tab导航 | 掘金技术征文的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: [Python]数据类型、常量、变量和运
- 下一篇: 用javascript实现一门编程语言-