2分钟
04 Tabbar控件实现-2
外部调用代码如下:每个 Tabbar 点击时,通过pageController.jumpTo
跳转页面,每个页面需要跳转坐标为:当前屏幕大小乘以索引 index 。
class _TabBarBottomPageWidgetState extends State<TabBarBottomPageWidget> {
final PageController pageController = new PageController();
final List<String> tab = ["动态", "趋势", "我的"];
///渲染底部Tab
_renderTab() {
List<Widget> list = new List();
for (int i = 0; i < tab.length; i++) {
list.add(new FlatButton(onPressed: () {
///每个 Tabbar 点击时,通过jumpTo 跳转页面
///每个页面需要跳转坐标为:当前屏幕大小 * 索引index。
topPageControl.jumpTo(MediaQuery
.of(context)
.size
.width * i);
}, child: new Text(
tab[i],
maxLines: 1,
)));
}
return list;
}
///渲染Tab 对应页面
_renderPage() {
return [
new TabBarPageFirst(),
new TabBarPageSecond(),
new TabBarPageThree(),
];
}
@override
Widget build(BuildContext context) {
///带 Scaffold 的Tabbar页面
return new GSYTabBarWidget(
type: GSYTabBarWidget.BOTTOM_TAB,
///渲染tab
tabItems: _renderTab(),
///渲染页面
tabViews: _renderPage(),
topPageControl: pageController,
backgroundColor: Colors.black45,
indicatorColor: Colors.white,
title: new Text("GSYGithubFlutter"));
}
}
如果到此结束,你会发现页面点击切换时,StatefulWidget
的子页面每次都会重新调用initState
。这肯定不是我们想要的,所以这时你就需要AutomaticKeepAliveClientMixin
。
学员评价