Flutter实现Tab切换页面

本文介绍了如何在Flutter中实现Tab切换页面,通过自定义TabBarWidget,将标签与页面同步变化。内容包括抽屉页面的实现,并提供了关键代码段和Demo源码链接。
摘要由CSDN通过智能技术生成
  • 先看效果图
    在这里插入图片描述

  • 实现过程分析
    首先自定义一个TabBarWidget

      class TabBarWidget extends StatefulWidget{
       //底部模式
       static const int BOTTOM_TAB = 1;
       //顶部模式
       static const int TOP_TAB = 2;
     
       final int type;
       //标题集合
       final List<Widget> tabItems;
       //页面集合
       final List<Widget> tabViews;
       final Color backgroundColor;
       //指示器颜色
       final Color indicatorColor;
       final Widget title;
       //抽屉widget
       final Widget drawer;
       final ValueChanged<int> onPageChanged;
     
       TabBarWidget({
         Key key,
         this.type,
         this.tabItems,
         this.tabViews,
         this.backgroundColor,
         this.indicatorColor,
         this.title,
         this.drawer,
         this.onPageChanged
       }):super(key:key);
     
       @override
       State<StatefulWidget> createState() {
         // TODO: implement createState
         return new _TabBarState(this.type, this.tabViews, this.indicatorColor, this.title, this.drawer,this.onPageChanged);
       }
     
     }
     
     //创建State对象,存储TabBarWidget的内部逻辑和变化状态
     //with表示扩展,SingleTickerProviderStateMixin是一个扩展(混合)类,它没有构造函数,只能继承自Object。
     //一个类可以有多个扩展类,扩展类可以实现方法,接口不能实现方法,只能在实现类里面实现,继承只能是单继承,这就是扩展的好处。
     //当有继承,扩展,以及类本身实现同样的功能时,方法调用的优先级是扩展类
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值