本人接触Flutter不到一个月,深深感觉到了这个平台的威力,因而不断学习,Flutter官方Example中的flutter_gallery,很好的展现了Flutter各个widget的功能git
其中的animation内容,展现的是一个很是漂亮的
带有动画和拖拽功能的
可展开的
卡片式Tab导航,很是漂亮,可是其实现没有抽象出一个可供第三方使用的Widget出来,并且其页面内容的定制性不够友好,滑动的时候也有bug,我在他的基础上进行了优化github
官方展现了一个很是好的开源示例,我改造了一下,也不敢独自享用,如今分享给你们,欢迎你们多多交流bash
这里是个人代码: GitHub/Realank数据结构
想使用这个控件很是简单,首先定义页面数据:app
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('第五页'))),
];
复制代码
而后建立这个控件:less
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new Scaffold(
body: Center(
child: new AnimateTabNavigation(
sectionList: allSections,
),
),
),
);
}
}
复制代码
大功告成ide
知其然还要知其因此然,下面来讲说这个控件的实现原理布局
首先,在sections.dart里定义了数据结构:post
class CardSection {
CardSection({this.title, this.leftColor, this.rightColor, this.contentWidget});
final String title;
final Color leftColor;
final Color rightColor;
final Widget contentWidget;
@override
bool operator ==(Object other) {
if (other is! CardSection) return false;
final CardSection otherSection = other;
return title == otherSection.title;
}
@override
int get hashCode => title.hashCode;
}
复制代码
它定义了其中一个卡片的标题,左边颜色和右边颜色(为了显示过渡颜色效果),以及子控件(这个是我改进的,这样能够别人使用的时候随意添加控件)学习
而后在widgets.dart中定义了几个widget:
最后在cardNavigation.dart中就是布局这些内容啦,这里面代码很复杂,其思路却是不难: