亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換

 更新時(shí)間:2021年07月11日 11:45:59   作者:早起的年輕人  
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換的具體代碼,供大家參考,具體內(nèi)容如下

TabBar 、Tab、TabBarView 結(jié)合實(shí)現(xiàn)

這里實(shí)現(xiàn)的是appbar下的選項(xiàng)卡

import 'package:flutter/material.dart';

/**
 * 有狀態(tài)StatefulWidget
 *  繼承于 StatefulWidget,通過 State 的 build 方法去構(gòu)建控件
 */
class TabBarAndTopTab extends StatefulWidget {
  通過構(gòu)造方法傳值
  TabBarAndTopTab();

  //主要是負(fù)責(zé)創(chuàng)建state
  @override
  _DemoStateWidgetState createState() => _DemoStateWidgetState();
}

/**
 * 在 State 中,可以動(dòng)態(tài)改變數(shù)據(jù)
 * 在 setState 之后,改變的數(shù)據(jù)會(huì)觸發(fā) Widget 重新構(gòu)建刷新
 */
class _DemoStateWidgetState extends State<TabBarAndTopTab>
    with SingleTickerProviderStateMixin {
  _DemoStateWidgetState();

  List tabs = ["首頁", "發(fā)現(xiàn)", "我的", "設(shè)置"];

  //用于控制/監(jiān)聽Tab菜單切換
  //TabBar和TabBarView正是通過同一個(gè)controller來實(shí)現(xiàn)菜單切換和滑動(dòng)狀態(tài)同步的。
  TabController tabController;

  @override
  void initState() {
    ///初始化,這個(gè)函數(shù)在生命周期中只調(diào)用一次
    super.initState();
    tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  void didChangeDependencies() {
    ///在initState之后調(diào) Called when a dependency of this [State] object changes.
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return buildTabScaffold();
  }

  //通過“bottom”屬性來添加一個(gè)導(dǎo)航欄底部tab按鈕組,將要實(shí)現(xiàn)的效果如下:
  Widget buildTabScaffold() {
    return Scaffold(
      appBar: AppBar(
        title: Text('標(biāo)題'),
        //設(shè)置選項(xiàng)卡
        bottom: buildTabBar(),
        //設(shè)置標(biāo)題居中
        centerTitle: true,
      ),
      //設(shè)置選項(xiàng)卡對(duì)應(yīng)的page
      body: buildBodyView(),
    );
  }

  //當(dāng)整個(gè)頁面dispose時(shí),記得把控制器也dispose掉,釋放內(nèi)存
  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }

  buildBodyView() {
    //構(gòu)造 TabBarView
    Widget tabBarBodyView = TabBarView(
      controller: tabController,
      //創(chuàng)建Tab頁
      children: tabs.map((e) {
        return Container(
          alignment: Alignment.center,
          child: Text(e, textScaleFactor: 1),
        );
      }).toList(),
    );
    return tabBarBodyView;
  }

  buildTabBar() {
    //構(gòu)造 TabBar
    Widget tabBar = TabBar(
        //tabs 的長(zhǎng)度超出屏幕寬度后,TabBar,是否可滾動(dòng)
        //設(shè)置為false tab 將平分寬度,為true tab 將會(huì)自適應(yīng)寬度
        isScrollable: false,
        //設(shè)置tab文字得類型
        labelStyle: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
        //設(shè)置tab選中得顏色
        labelColor: Colors.white,
        //設(shè)置tab未選中得顏色
        unselectedLabelColor: Colors.white70,
        //設(shè)置自定義tab的指示器,CustomUnderlineTabIndicator
        //若不需要自定義,可直接通過
        //indicatorColor 設(shè)置指示器顏色
        //indicatorWight 設(shè)置指示器厚度
        //indicatorPadding
        //indicatorSize  設(shè)置指示器大小計(jì)算方式
        ///指示器大小計(jì)算方式,TabBarIndicatorSize.label跟文字等寬,TabBarIndicatorSize.tab跟每個(gè)tab等寬
        indicatorSize: TabBarIndicatorSize.tab,
        //生成Tab菜單
        controller: tabController,
        //構(gòu)造Tab集合
        tabs: tabs.map((e) => Tab(text: e)).toList());

    return tabBar;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論