[Flutter]使用頂部切換導(dǎo)航TabBar

目前移動(dòng)開發(fā)tab切換是一個(gè)很通用的功能,F(xiàn)lutter 通過Material 庫提供了很方便的API來使用tab切換。

效果圖

image

創(chuàng)建TabController

TabBarView和TabBar都有一個(gè)tabController的參數(shù)
TabBarView和Tab是就由TabController來控制同步的,點(diǎn)擊了某個(gè)tab后,要同步的顯示對(duì)應(yīng)的TabBarView。TabController的創(chuàng)建有兩種形式,一種是使用系統(tǒng)的DefaultTabController,這種方式很簡單,只要在Scaffold上面再套一層DefaultTabController就可以了。這種方式下,TabBarView會(huì)自動(dòng)去查找這個(gè)tabController,如果找不到就會(huì)報(bào)錯(cuò)。

第二種是自己定義一個(gè)TabController.實(shí)現(xiàn)SingleTickerProviderStateMixin

創(chuàng)建TabBar

TabBar哪里都可以創(chuàng)建,但是在AppBar里面有一個(gè)bottom參數(shù)可以接收TabBar,會(huì)放在導(dǎo)航欄的下面。

new TabBar(
          tabs: <Widget>[
            new Tab(
              icon: new Icon(Icons.directions_bike),
            ),
            new Tab(
              icon: new Icon(Icons.directions_boat),
            ),
            new Tab(
              icon: new Icon(Icons.directions_bus),
            ),
          ],
        ),

創(chuàng)建Tab對(duì)應(yīng)的內(nèi)容Widget

tab對(duì)應(yīng)的內(nèi)容view要放在TabBarView里面。如下:

new TabBarView(
        controller: _tabController,
        children: <Widget>[
          new Center(child: new Text('自行車')),
          new Center(child: new Text('船')),
          new Center(child: new Text('巴士')),
        ],
      ),

兩種方式的完整代碼

第一種實(shí)現(xiàn)代碼如下:

@override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: 3,
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text('頂部tab切換'),
          bottom: new TabBar(
            tabs: <Widget>[
              new Tab(icon: new Icon(Icons.directions_bike),),
              new Tab(icon: new Icon(Icons.directions_boat),),
              new Tab(icon: new Icon(Icons.directions_bus),),
            ],
            controller: _tabController,
          ),
        ),
        body: new TabBarView(
          children: <Widget>[
            new Center(child: new Text('自行車')),
            new Center(child: new Text('船')),
            new Center(child: new Text('巴士')),
          ],
        ),
      ),
    );
  }

第二種代碼如下:

class TabBarDemoState extends State<TabBarDemo>
    with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 3);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('頂部tab切換'),
        bottom: new TabBar(
          tabs: <Widget>[
            new Tab(
              icon: new Icon(Icons.directions_bike),
            ),
            new Tab(
              icon: new Icon(Icons.directions_boat),
            ),
            new Tab(
              icon: new Icon(Icons.directions_bus),
            ),
          ],
          controller: _tabController,
        ),
      ),
      body: new TabBarView(
        controller: _tabController,
        children: <Widget>[
          new Center(child: new Text('自行車')),
          new Center(child: new Text('船')),
          new Center(child: new Text('巴士')),
        ],
      ),
    );
  }

關(guān)于學(xué)習(xí)

flutter的學(xué)習(xí)文章及代碼都整理在這個(gè)github倉庫

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

相關(guān)閱讀更多精彩內(nèi)容

  • 緊接上一篇的有側(cè)邊欄APP,這次我們向APP中加入上下Tab頁,使之跟趨近主流大部分APP的信息布局套路,等不及看...
    燃燒的魚丸閱讀 18,660評(píng)論 19 31
  • 點(diǎn)擊上方藍(lán)字關(guān)注我,好文章送上門! 入夏天以后,衣櫥里的T恤、襯衣等基礎(chǔ)款逐漸成為上班族的懶人首選,一件簡單的...
    我是吳曦閱讀 313評(píng)論 0 0
  • 首先聲明:昨天寫的計(jì)劃寫的是1.22,其實(shí)應(yīng)該寫1.23。因?yàn)檫@是對(duì)今天的計(jì)劃。 嗯先對(duì)昨天的計(jì)劃進(jìn)行反饋~具體請(qǐng)...
    譚銀河閱讀 179評(píng)論 0 0
  • 1那是穿過環(huán)海路的風(fēng)你不知道海東沒有岸一片花海只是繁星的倒影 2一朵格?;ㄑ刂0堕_我一邊走,一邊采路的盡頭我們不...
    一言爾閱讀 491評(píng)論 1 1
  • (說明:此系列隱去專車app的名稱,以免老是有人說我打軟廣。) “我在宛平南路,中山南二路,儂拉阿里搭?” 接到這...
    Graceland閱讀 959評(píng)論 8 5

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