AppBar中自定義頂部導航

在上一篇裏總結AppBar的一些簡單用法,可是AppBar除了有前面那些樣式屬性外,還能實現相似底部的Tab切換。app

首先下載並運行前面的項目:less

而後在此基礎上實現Tab切換。ide

常見屬性

TabBar有一下常見的屬性:ui

  • tabs :顯示的標籤內容,通常使用 Tab 對象,也能夠是其餘的 Widget 
  • controller :TabController 對象 
  • isScrollable :是否可滾動 
  • indicatorColor :指示器顏色 
  • indicatorWeight :指示器高度 
  • indicatorPadding :底部指示器的 Padding 
  • indicator :指示器 decoration,例如邊框等 
  • indicatorSize :指示器大小計算方式,TabBarIndicatorSize.label 跟文字等寬,TabBarIndicatorSize.tab 跟每一個 tab 等寬 
  • labelColor :選中 label 顏色 
  • labelStyle :選中 label 的 Style 
  • labelPadding :每一個 label 的 padding 值 
  • unselectedLabelColor :未選中 label 顏色 
  • unselectedLabelStyle :未選中 label 的 Style 

基本實現

爲了實現頂部的Tabs切換,首先須要在Scaffold的外層定義一個DefaultTabController組件,而後組件裏面定義tab的個數,最後將TabBar定義在AppBar裏面的bottom屬性中。根據這些,咱們來修改前面的spa

AppBarDemo.dart頁面。
import 'package:flutter/material.dart';

class AppBarDemoPage extends StatelessWidget {
  const AppBarDemoPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length:2 ,
      child: Scaffold(
        appBar: AppBar(
          title:Text("AppBarDemoPage"), 
          // backgroundColor: Colors.red, 
          centerTitle:true,
          leading: IconButton(
            icon: Icon(Icons.menu),
            onPressed: (){
              print('menu');
            },
          ), 
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: (){
                print('search');
              },
            ),
            IconButton(
              icon: Icon(Icons.settings),
              onPressed: (){
                print('settings');
              },
            )
          ],
          bottom: TabBar(
            tabs: <Widget>[
              Tab(text: "熱門"),
              Tab(text: "推薦")
            ],
          ),
        ),
        body: Text('1111'),
      ),
    );
  }
}

爲了簡化代碼,刪掉前面關於AppBar的屬性設置:3d

AppBarDemo.dartcode

import 'package:flutter/material.dart';

class AppBarDemoPage extends StatelessWidget {
  const AppBarDemoPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length:2 ,
      child: Scaffold(
        appBar: AppBar(
          title:Text("AppBarDemoPage"), 
          centerTitle:true,
          bottom: TabBar(
            tabs: <Widget>[
              Tab(text: "熱門"),
              Tab(text: "推薦")
            ],
          ),
        ),
        body: Text('1111'),
      ),
    );
  }
}

如今,只有跳轉的按鈕,卻沒有對應的頁面組件,因此,還須要在body裏面添加tabs切換的頁面。對象

 

目前,是在一個新的頁面添加了頂部Tabs切換,那麼,若是須要在底部TabBar頁面基礎上添加Tabs切換,又該如何操做呢?blog

TabBar中添加頂部Tab切換

 按照前面說的,在Scaffold的外層定義一個DefaultTabController組件,先這樣修改Category.dart頁面:get

import 'package:flutter/material.dart';

class CategoryPage extends StatefulWidget {
  CategoryPage({Key key}) : super(key: key);
  _CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          bottom:TabBar(
            tabs: <Widget>[
              Tab(text: "熱銷"),
              Tab(text: "推薦"),
              Tab(text: "推薦"),
              Tab(text: "推薦")
            ],
          ) ,
        ),
        body:TabBarView(
          children: <Widget>[
            ListView(
                children: <Widget>[
                  ListTile(title:Text("第一個tab")),
                ],
              ),
            ListView(
                children: <Widget>[
                  ListTile(title:Text("第二個tab")),
                ],
              ),
              ListView(
                children: <Widget>[
                  ListTile(title:Text("第三個tab")),
                ],
              ),
            ListView(
                children: <Widget>[
                  ListTile(title:Text("第四個tab")),
                ],
              )
          ],
        )
      ),
    );
  }
}

 由於Category.dart是掛載到Tabs.dart中的,而在Tabs.dart中,已經有一個Scaffold組件和AppBar組件了,因此,繼續添加頂部Tabs之後,就會有兩個Scaffold組件和AppBar組件。

 

 爲了解決上面的問題,只須要將Tabs切換換個位置,移動到title所在的位置就能夠了:

import 'package:flutter/material.dart';

class CategoryPage extends StatefulWidget {
  CategoryPage({Key key}) : super(key: key);

  _CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black26,
          title: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child:TabBar(
            isScrollable: true, //多個Tab的時候,能夠實現滑動和聯動
                  indicatorColor:Colors.blue,
                  labelColor:Colors.blue,
                  unselectedLabelColor: Colors.white,
                  indicatorSize:TabBarIndicatorSize.label ,
                  tabs: <Widget>[
                    Tab(text: "熱銷"),
                    Tab(text: "推薦"),
                    Tab(text: "推薦"),
                    Tab(text: "推薦")
                  ],
                ) ,
              )
            ],
          ),
          
        ),
        body:TabBarView(
          children: <Widget>[
            ListView(
                children: <Widget>[
                  ListTile(title:Text("第一個tab")),
                ],
              ),
            ListView(
                children: <Widget>[
                  ListTile(title:Text("第二個tab")),
                ],
              ),
              ListView(
                children: <Widget>[
                  ListTile(title:Text("第三個tab")),
                ],
              ),
            ListView(
                children: <Widget>[
                  ListTile(title:Text("第四個tab")),
                ],
              )
          ],
        )
      ),
    );
  }
}

 

相關文章
相關標籤/搜索