当前位置:   article > 正文

六、Flutter自定义Tabbar_flutter 自定义tabbar

flutter 自定义tabbar

转自于:https://www.jianshu.com/p/22034838ae45

目录
一、效果展示
二、底部Tabbar
三、顶部Tabbar

一、效果展示

底部Tabbar切换和顶部Tabbar切换在工作中使用频率都比较高,Flutter很人性化,这些组件都提供好了,我们只需要了解一下如何使用就好了。下面是我用宝贵的周末时间做的一个Demo,请看效果。有什么好的建议,可以在下面留言交流一下,共同学习共同进步。

 

Tabbar.jpg

二、底部Tabbar

重新build方法会返回一个Scaffold组件,在这个组件下面添加底部导航属性bottomNavigationBar,onTap事件实现点击底部导航栏页面之间的切换和状态改变。因为界面有变化,所以这里使用的是动态组件StatefulWidget。

 

  1. @override
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
  5. bottomNavigationBar: BottomNavigationBar(
  6. type:BottomNavigationBarType.fixed,
  7. currentIndex: currentIndex,
  8. items:_bottomTabs,
  9. onTap: (index){
  10. setState(() {
  11. currentIndex = index;
  12. currentPage = _tabs[currentIndex];
  13. });
  14. },
  15. fixedColor: Colors.green,
  16. ),
  17. body:currentPage
  18. );
  19. }

_bottomTabs是定义的底部Tabbar的List<BottomNavigationBarItem>,包含了Tabbar的每个item。item有默认icon和选择的activeIcon,这里使用的都是加载的本地图片,加载本地图片需要以下配置。
1、以当前Demo为例,建立一个assets文件夹images,下面建2.0x和3.0x两个文件夹,用来放2倍图和3倍图,另外直接在images文件夹下放一倍图。

 

本地图片配置1.jpg

 

2、需要在pubspec.yaml文件中声明每张图片。

 

本地图片配置2.jpg


3、Tabbar每个item的icon和activeIcon加载本地图。

  1. final List<BottomNavigationBarItem> _bottomTabs = [
  2. BottomNavigationBarItem(
  3. icon:Image.asset("images/ic_tab_home_normal.png",fit: BoxFit.cover,width: 40,height: 40,),
  4. activeIcon:Image.asset("images/ic_tab_home_active.png",fit: BoxFit.cover,width: 40,height: 40,),
  5. title:Text('首页')
  6. ),
  7. BottomNavigationBarItem(
  8. icon:Image.asset("images/ic_tab_subject_normal.png",fit: BoxFit.cover,width: 40,height: 40,),
  9. activeIcon:Image.asset("images/ic_tab_subject_active.png",fit: BoxFit.cover,width: 40,height: 40,),
  10. title:Text('书影音')
  11. ),
  12. BottomNavigationBarItem(
  13. icon:Image.asset("images/ic_tab_group_normal.png",fit: BoxFit.cover,width: 40,height: 40,),
  14. activeIcon:Image.asset("images/ic_tab_group_active.png",fit: BoxFit.cover,width: 40,height: 40,),
  15. title:Text('小组')
  16. ),
  17. BottomNavigationBarItem(
  18. icon:Image.asset("images/ic_tab_shiji_normal.png",fit: BoxFit.cover,width: 40,height: 40,),
  19. activeIcon:Image.asset("images/ic_tab_shiji_active.png",fit: BoxFit.cover,width: 40,height: 40,),
  20. title:Text('市集')
  21. ),
  22. BottomNavigationBarItem(
  23. icon:Image.asset("images/ic_tab_profile_normal.png",fit: BoxFit.cover,width: 40,height: 40,),
  24. activeIcon: Image.asset("images/ic_tab_profile_active.png",fit: BoxFit.cover,width: 40,height: 40,),
  25. title:Text('我的')
  26. )
  27. ];

_tabs是Tabbar所以页面的集合。

 

  1. final List _tabs = [
  2. TopBarPage(),
  3. AudioBook(),
  4. Team(),
  5. Fair(),
  6. Mine()
  7. ];

初次加载页面重新initState初始化currentPage完成整个页面的渲染。

 

  1. @override
  2. void initState() {
  3. super.initState();
  4. currentPage=_tabs[currentIndex];
  5. }

切换Tabbar实现onTap刷新状态并更改currentPage渲染页面,就这样一个漂亮的底部Tabbar就完成了,很简单吧!

三、顶部Tabbar

顶部Tabbar相对于底部Tabbar简单一些,顶部Tabbar切换时不需要手动改变状态,所以这里使用StatelessWidget。顶部Tabbar使用DefaultTabController组件,声明切换item的个数length属性,并保证与tabs和TabBarView的个数一致,否则会报错。
AppBar组件下的Title也是一个组件,这里添加一个自定义的搜索组件SearchTextFieldWidget,这里就不过多介绍了。

 

  1. @override
  2. Widget build(BuildContext context) {
  3. return DefaultTabController(
  4. length: 6,
  5. child: Scaffold(
  6. appBar: AppBar(
  7. title: SearchTextFieldWidget(
  8. hintText: "用一部电影来形容你的2019",
  9. onSubmitted: (searchContent) {
  10. },
  11. ),
  12. backgroundColor: Colors.green,
  13. bottom: TabBar(
  14. indicatorColor: Colors.white,
  15. tabs: <Widget>[
  16. Tab(
  17. text: "电影",
  18. ),
  19. Tab(
  20. text: "电视",
  21. ),
  22. Tab(
  23. text: "综艺",
  24. ),
  25. Tab(
  26. text: "读书",
  27. ),
  28. Tab(
  29. text: "音乐",
  30. ),
  31. Tab(
  32. text: "同城",
  33. )
  34. ],
  35. ),
  36. ),
  37. body: TabBarView(
  38. children: <Widget>[Home(), Home(), Home(), Home(), Home(), Home()],
  39. ),
  40. ),
  41. );
  42. }



作者:星星编程
链接:https://www.jianshu.com/p/22034838ae45
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/431730
推荐阅读
相关标签
  

闽ICP备14008679号