当前位置:   article > 正文

Flutter创建TabBar和可滑动的顶部分页_flutter 可滑动tab

flutter 可滑动tab

Flutter搭建一个基本的app架构,tabBar + navgationBar + views

1.首先理清思路,创建tabBar -> 选中第一个标签 -> 进入第一个子页面 -> 创建nagationBar -> 创建顶部分页tab

2. 创建tabBar,找到main函数入口,创建一个类命名为自定义的tabBarController,将它作为函数的返回值。

  1. void main () => runApp(LiveApp());
  2. class LiveApp extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return CYTabBarController();
  6. }
  7. }

大概就是这样,这样我们就创建好了一个入口。

2.来到这个新建的文件,实现一个Widget MaterialApp里面的属性主要使用home:Widget。

  1. class CYTabBarController extends StatefulWidget {
  2. @override
  3. _CYTabBarControllerState createState() => _CYTabBarControllerState();
  4. }
  5. class _CYTabBarControllerState extends State<CYTabBarController> {
  6. @override
  7. @override
  8. Widget build(BuildContext context) {
  9. return MaterialApp(
  10. home: ,
  11. );
  12. }
  13. }

接下来就是要给这个home一个函数返回值为Widget或者直接赋值Widget。

这里给一个Scaffold Widget,Scaffold 的主要用到的有appBar,body,title,bottomNavigationBar,

appBar 就是导航栏,title等价于导航栏上的居中的view,你可以自定一个Widget赋值给它,body是你需要显示的内容,bottomNavigationBar就是底部的tabBar了。

3.实现tabBar

一种做法是用tabBarView,这样效果就是可以左右滚动,需要创建一个TabController,它是负责管理tabView的。

另一种做法就是用bottomNavigationBar,这个是不可以滚动的,也不需要TabController去管理。

这里我选择用bottomNavigationBar去做,具体做法看个人的喜好。

BottomNavigationBar 需要实现一个items,就是BottomNavigationBarItem的数组,有多少了tab标签就创建多少个。

  1. items: [BottomNavigationBarItem(icon: Icon(Icons.home),title: Text('首页')),
  2. BottomNavigationBarItem(icon: Icon(Icons.live_tv),title: Text('直播')),
  3. BottomNavigationBarItem(icon: Icon(Icons.find_in_page),title: Text('发现')),
  4. BottomNavigationBarItem(icon: Icon(Icons.my_location),title: Text('我的')),],

这里我创建了4个item。

看一下源码,需要哪些参数和可以设置哪些参数

  1. BottomNavigationBar({
  2. Key key,
  3. @required this.items,
  4. this.onTap,
  5. this.currentIndex = 0,
  6. this.elevation = 8.0,
  7. BottomNavigationBarType type,
  8. Color fixedColor,
  9. this.backgroundColor,
  10. this.iconSize = 24.0,
  11. Color selectedItemColor,
  12. this.unselectedItemColor,
  13. this.selectedIconTheme = const IconThemeData(),
  14. this.unselectedIconTheme = const IconThemeData(),
  15. this.selectedFontSize = 14.0,
  16. this.unselectedFontSize = 12.0,
  17. this.selectedLabelStyle,
  18. this.unselectedLabelStyle,
  19. this.showSelectedLabels = true,
  20. bool showUnselectedLabels,
  21. })

这些都是常用的可设置,@required 是必须赋值的,具体用法看字面意思就知道了,这里不去介绍。

来看看具体的实现

  1. bottomNavigationBar: BottomNavigationBar(
  2. type: BottomNavigationBarType.fixed,
  3. items: [BottomNavigationBarItem(icon: Icon(Icons.home),title: Text('首页')),
  4. BottomNavigationBarItem(icon: Icon(Icons.live_tv),title: Text('直播')),
  5. BottomNavigationBarItem(icon: Icon(Icons.find_in_page),title: Text('发现')),
  6. BottomNavigationBarItem(icon: Icon(Icons.my_location),title: Text('我的')),],
  7. currentIndex: currentIndex,
  8. selectedFontSize: 13,
  9. unselectedFontSize: 13,
  10. selectedItemColor: Colors.green,
  11. onTap: (index) {
  12. setState(() {
  13. currentIndex = index;
  14. });
  15. },
  16. )

有一个index很重要,就是你点击返回给你的按钮下标,通过StatefulWidget 的setState 可以监听它的改变然后做出相应的调整。

4.创建一个IndexedStack的Widget,它有一个children和index属性,我们将需要显示的界面先创建好导入,然后放入children里面,index赋值当前选中的下标,这样做的好处是不用每次都去渲染。

  1. Widget build(BuildContext context) {
  2. return MaterialApp(
  3. home: Scaffold(
  4. bottomNavigationBar: BottomNavigationBar(
  5. type: BottomNavigationBarType.fixed,
  6. items: [BottomNavigationBarItem(icon: Icon(Icons.home),title: Text('首页')),
  7. BottomNavigationBarItem(icon: Icon(Icons.live_tv),title: Text('直播')),
  8. BottomNavigationBarItem(icon: Icon(Icons.find_in_page),title: Text('发现')),
  9. BottomNavigationBarItem(icon: Icon(Icons.my_location),title: Text('我的')),],
  10. currentIndex: currentIndex,
  11. selectedFontSize: 13,
  12. unselectedFontSize: 13,
  13. selectedItemColor: Colors.green,
  14. onTap: (index) {
  15. setState(() {
  16. currentIndex = index;
  17. });
  18. },
  19. ),
  20. body: IndexedStack(
  21. children: <Widget>[
  22. HomeView(),
  23. LiveView(),
  24. DiscoverView(),
  25. MyZoneView(),
  26. ],
  27. index: currentIndex,
  28. ),
  29. ),
  30. );
  31. }

看看具体的效果

 

5.创建一个滑动的tab分页。

来到首页你创建的类里面,大概是这样

  1. class HomeView extends StatefulWidget {
  2. @override
  3. _HomeViewState createState() => _HomeViewState();
  4. }
  5. class _HomeViewState extends State<HomeView> {
  6. @override
  7. Widget build(BuildContext context) {
  8. return Container(
  9. child: Center(
  10. child: Text('首页',style: TextStyle(fontSize: 18)),
  11. ),
  12. );
  13. }
  14. }

这里需要用到的Widget主要有Scaffold,TabBar,TabBarView,用到了tabBar和TabBarView首先就要创建一个TabController,

用来管理它们。

这里要用到一个initState()和dispose()函数,一个用来创建,一个用来销毁。

在initState()函数里面将TabController 创建好。

  1. void initState() {
  2. // TODO: implement initState
  3. tabController = TabController(
  4. length: 3,
  5. vsync: this,
  6. );
  7. super.initState();
  8. }

length是需要管理的tab数组长度,vsync就简单理解为发送一个同步信号,这里要渲染了。

对应的dispose()里面

  1. @override
  2. void dispose() {
  3. // TODO: implement dispose
  4. tabController.dispose();
  5. super.dispose();
  6. }

创建的步骤和上面类似,如下

  1. @override
  2. Widget build(BuildContext context) {
  3. super.build(context);
  4. return Scaffold(
  5. appBar: AppBar(
  6. backgroundColor: Colors.green,
  7. title: Text('首页',style: TextStyle(fontSize: 22),),
  8. bottom: TabBar(
  9. indicatorColor: Colors.white,
  10. indicatorSize: TabBarIndicatorSize.label,
  11. unselectedLabelStyle: TextStyle(
  12. fontSize: 15,
  13. fontWeight: FontWeight.w500,
  14. ),
  15. unselectedLabelColor: Colors.white54,
  16. labelColor: Colors.white,
  17. labelStyle: TextStyle(
  18. fontSize: 20,
  19. fontWeight: FontWeight.w500
  20. ),
  21. controller: tabController,
  22. tabs: <Widget>[
  23. Tab(text: '电影',),
  24. Tab(text: '读书',),
  25. Tab(text: '新闻',),
  26. ],
  27. ),
  28. ),
  29. body: TabBarView(
  30. controller: tabController,
  31. children: <Widget>[
  32. MoviesView(),
  33. BooksView(),
  34. NewsView(),
  35. ],
  36. ),
  37. );
  38. }

这样就创建好了一个分页滑动的tab。

demo下载地址:Flutter创建tabbar

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号