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实现一门编程语言-