Flutter入門(51):Flutter 組件之 CupertinoTabBar 詳解

1. 基本介紹

CupertinoTabBar 是一個 iOS 風(fēng)格的底部選項卡,等同于 UITabBar,他的子控件 BottomNavigationBarItem 也就相當(dāng)于 UITabBarItem。

CupertinoTabBar.png
CupertinoTabBar Tapped.gif

2. 示例代碼

代碼下載地址。如果對你有幫助的話記得給個關(guān)注,代碼會根據(jù)我的 Flutter 專題不斷更新。

3. 屬性介紹

BottomNavigationBarItem 屬性 介紹
icon @required 圖標(biāo)
title 標(biāo)題
activeIcon Widget 高亮?xí)r圖標(biāo)
backgroundColor 背景色,注意它不適用于 CupertinoTabBar,它只能在 BottomNavigationBar 中展示效果,Flutter入門(15):Flutter 組件之 BottomNavigationBar 詳解
CupertinoTabBar 屬性 介紹
items @required 子選項卡
onTap 點擊選項卡回調(diào)
currentIndex 當(dāng)前選中選項卡下標(biāo)
backgroundColor 背景色
activeColor 選項卡高亮?xí)r顏色
inactiveColor 選項卡未選中時顏色,默認(rèn)為 _kDefaultTabBarInactiveColor
iconSize 選項卡圖標(biāo)大小,默認(rèn)為 30.0
border 邊框

4. CupertinoTabBar 詳解

CupertinoTabBar 值得注意的是,它只能配合 CupertinoTabScaffold 使用。屬性不多,代碼中已經(jīng)備注好了。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class FMCupertinoTabBarVC extends StatefulWidget{
  @override
  FMCupertinoTabBarState createState() => FMCupertinoTabBarState();
}

class FMCupertinoTabBarState extends State <FMCupertinoTabBarVC> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return CupertinoTabScaffold(
      tabBar: _cupertinoTabBar(),
      tabBuilder: (context, index){
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text("CupertinoTabBar"),
          ),
          child: ListView(
            children: [

            ],
          ),
        );
      },
    );
  }

  CupertinoTabBar _cupertinoTabBar(){
    return CupertinoTabBar(
      // 點擊回調(diào)
      onTap: (index){
        print(index);
      },
      currentIndex: 2, // 設(shè)置默認(rèn)選中位置
      backgroundColor: Colors.lightBlueAccent, // tabbar 背景色
      activeColor: Colors.white, // 圖標(biāo)高亮顏色
      inactiveColor: Colors.grey, // 圖標(biāo)未選中顏色
      iconSize: 25, // 圖標(biāo)大小
      // 邊框
      border: Border(
        top: BorderSide(
          width: 3,
          color: Colors.red
        ),
      ),
      items: [
        _bottomNavigationBarItem(Icons.add, "第一個"),
        _bottomNavigationBarItem(Icons.add, "第二個"),
        _bottomNavigationBarItem(Icons.add, "第三個"),
        _bottomNavigationBarItem(Icons.add, "第四個"),
      ],
    );
  }

  BottomNavigationBarItem _bottomNavigationBarItem(IconData activeIcon, String title){
    return BottomNavigationBarItem(
      icon: Icon(Icons.ac_unit), // 圖標(biāo)
      activeIcon: Icon(activeIcon), // 高亮圖標(biāo)
      title: Text("$title"), // 標(biāo)題
      backgroundColor: Colors.yellow, // 背景色,僅在 BottomNavigatinBar 中生效,在 iOS 風(fēng)格組件中無效
    );
  }
}
CupertinoTabBar Tapped.gif

5. 技術(shù)小結(jié)

CupertinoTabBar 屬性不多,了解一下即可。

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

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