Flutter(13):Material組件之TabBar

Flutter教學目錄持續(xù)更新中

github源代碼持續(xù)更新中

1.TabBar簡介

水平選項卡,配合TabBarView可以實現(xiàn)選項卡跟頁面的切換

2.TabBar屬性

  • tabs:顯示的標簽內(nèi)容,一般使用Tab對象,也可以自定義
  • controller:TabController對象
  • isScrollable:是否可滾動(默認false),當false時不可滑動會讓每個tab平均分配寬度空間;如果true可滑動,每個tab會自適應寬度,不超過父節(jié)點寬度會居中顯示
  • indicatorColor:指示器顏色
  • indicatorWeight:指示器高度
  • indicatorPadding:底部指示器的Padding
  • indicator:指示器decoration,例如邊框等
  • indicatorSize:指示器大小計算方式,TabBarIndicatorSize.label跟文字等寬,TabBarIndicatorSize.tab跟每個tab等寬
  • labelColor:選中l(wèi)abel顏色
  • labelStyle:選中l(wèi)abel的Style
  • labelPadding:每個label的padding值
  • unselectedLabelColor:未選中l(wèi)abel顏色
  • unselectedLabelStyle:未選中l(wèi)abel的Style

3.Tab屬性

  • text:文本
  • icon:圖標
  • iconMargin:圖標的Margin
  • child:子節(jié)點

4.TabController屬性

  • length:Tab的數(shù)量
  • vsync:TickerProvider,需要混入(with)SingleTickerProviderStateMixin
    例如:
class _TabBarPageState extends State<TabBarPage>
    with SingleTickerProviderStateMixin 

(這邊可能有dart語法不了解的同學不了解混入(with)是什么意思可以看一下這個文章Flutter(Dart)中extends 、 implements 、 with的用法與區(qū)別

4.常規(guī)使用

一般情況可以直接使用Tab去創(chuàng)建TabBar的子節(jié)點,其中最主要的是需要設置TabController,在頁面銷毀的時候記得銷毀TabController


1600779758(1).png
final _tabList = [
    Tab(
      text: '首頁',
      icon: Icon(Icons.home),
      iconMargin: EdgeInsets.all(5),
    ),
    Tab(
      text: '郵件',
      icon: Icon(Icons.mail),
      iconMargin: EdgeInsets.all(5),
    ),
    Tab(
      text: '消息',
      icon: Icon(Icons.message),
      iconMargin: EdgeInsets.all(5),
    ),
    Tab(
      text: '我的',
      icon: Icon(Icons.people),
      iconMargin: EdgeInsets.all(5),
    ),
  ];
 appBar: AppBar(
        title: Text('TabBarPage'),
        bottom: TabBar(
          tabs: widget._tabList,
          controller: _tabController,
          isScrollable: false,
          indicatorWeight: 2,
          indicatorColor: Colors.amberAccent,
          indicatorPadding: EdgeInsets.only(bottom: 2),
          indicatorSize: TabBarIndicatorSize.tab,
          labelColor: Colors.amberAccent,
          labelPadding: EdgeInsets.only(bottom: 2),
          labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
          unselectedLabelColor: Colors.black,
          unselectedLabelStyle:
              TextStyle(fontSize: 16, fontWeight: FontWeight.normal),
        ),
      ),
 @override
  void initState() {
    _tabController = TabController(length: widget._tabList.length, vsync: this);
    super.initState();
  }
@override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

5.自定義的widget做Tab

tabBar是支持放入自定義的widget做子節(jié)點的,這樣給個性化定制提供了更多可能性


1600779869(1).png
 final _tabDateList = [
    TabBean(title: '首頁', icon: Icons.home),
    TabBean(title: '郵件', icon: Icons.mail),
    TabBean(title: '消息', icon: Icons.message),
    TabBean(title: '我的', icon: Icons.people),
  ];
 _customTabs() {
    return widget._tabDateList
        .map(
          (e) => Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [Icon(e.icon), Text(e.title)],
            ),
          ),
        )
        .toList();
  }
appBar: AppBar(
        title: Text('TabBarPage'),
        bottom: TabBar(
          tabs: _customTabs(),
          controller: _tabController,
          isScrollable: false,
          indicatorWeight: 2,
          indicatorColor: Colors.amberAccent,
          indicatorPadding: EdgeInsets.only(bottom: 2),
          indicatorSize: TabBarIndicatorSize.tab,
          labelColor: Colors.amberAccent,
          labelPadding: EdgeInsets.only(bottom: 2),
          labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
          unselectedLabelColor: Colors.black,
          unselectedLabelStyle:
              TextStyle(fontSize: 16, fontWeight: FontWeight.normal),
        ),
      ),

下一節(jié)Material組件之TabBarView

Flutter(14):Material組件之TabBarView

Flutter教學目錄持續(xù)更新中

github源代碼持續(xù)更新中

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容