Flutter--自定义顶部导航栏、(TabBar)TabLayout

appbar属性
属性释义
leading导航按钮显示的图标
title标题
actions相当于menu
bottom通常用来放置tabBar
backgroundColor导航背景颜色
iconTheme图表样式
textTheme文字样式
centerTitle标题是否居中显示
自定义AppBar使用

import 'package:flutter/material.dart';

class AppBarDemoPage extends StatelessWidget {
  const AppBarDemoPage({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return 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');
            },
          )
        ],


      ),
      body: Text('appbardemo'),
    );
  }
}

在这里插入图片描述

TabBar(TabLayout)
属性
属性释义
tabs显示标签内容
controllerTabController对象
isScrollable是否可滚动,即是否在一个屏幕放下所有Tab
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
import 'package:flutter/material.dart';

class AppBarDemoPage extends StatelessWidget {
  const AppBarDemoPage({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4, // tab个数
      child: Scaffold(
        // Tab组件必须放到Scaffold中
        appBar: AppBar(
            title: Text("TabBarDemo"),
            bottom: TabBar(
              tabs: <Widget>[
                Tab(
                  text: "热点",
                ),
                Tab(
                  text: "新闻",
                ),
                Tab(
                  text: "推荐",
                ),
                Tab(
                  text: "同城",
                )
              ],
            )),
        body: TabBarView(
          // 类似ViewPage
          children: <Widget>[
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第1个 tab")),
                ListTile(title: Text("这是第1个 tab")),
                ListTile(title: Text("这是第1个 tab"))
              ],
            ),
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第2个 tab")),
                ListTile(title: Text("这是第2个 tab")),
                ListTile(title: Text("这是第2个 tab"))
              ],
            ),
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第3个 tab")),
                ListTile(title: Text("这是第3个 tab")),
                ListTile(title: Text("这是第3个 tab"))
              ],
            ),
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第4个 tab")),
                ListTile(title: Text("这是第4个 tab")),
                ListTile(title: Text("这是第4个 tab"))
              ],
            ),
          ],
        ),
      ),
    );
  }
}

在这里插入图片描述

存在Bottomnavigation的页面创建TabLayout
import 'package:flutter/material.dart';

class SystemPage extends StatefulWidget {
  SystemPage({Key key}) : super(key: key);
  _SystemPageState createState() => _SystemPageState();
}

class _SystemPageState extends State<SystemPage> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold( // 外部布局已经存在Scaffold,此时内部还可以再嵌套一个Scaffold
        appBar: AppBar( // 此时我们在同一个页面创建了两个appbar,所以直接在title中创建tab即可
            title: TabBar(
          tabs: <Widget>[
            Tab(
              text: "热点",
            ),
            Tab(
              text: "新闻",
            ),
            Tab(
              text: "推荐",
            ),
            Tab(
              text: "同城",
            )
          ],
        )),
        body: TabBarView(
          // 类似ViewPage
          children: <Widget>[
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第1个 tab")),
                ListTile(title: Text("这是第1个 tab")),
                ListTile(title: Text("这是第1个 tab"))
              ],
            ),
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第2个 tab")),
                ListTile(title: Text("这是第2个 tab")),
                ListTile(title: Text("这是第2个 tab"))
              ],
            ),
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第3个 tab")),
                ListTile(title: Text("这是第3个 tab")),
                ListTile(title: Text("这是第3个 tab"))
              ],
            ),
            ListView(
              children: <Widget>[
                ListTile(title: Text("这是第4个 tab")),
                ListTile(title: Text("这是第4个 tab")),
                ListTile(title: Text("这是第4个 tab"))
              ],
            ),
          ],
        ),
      ),
    );
  }
}

在这里插入图片描述

通过TabController实现TabLayout
import 'package:flutter/material.dart';

class TabbarControllerPage extends StatefulWidget {
  TabbarControllerPage({Key key}) : super(key: key);
  _TabbarControllerPageState createState() => _TabbarControllerPageState();
}


class _TabbarControllerPageState extends State<TabbarControllerPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;


   // 生命周期函数,销毁时取消订阅,类似Rx
  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  // 生命周期函数, 加载即触发
  @override
  void initState() {
    super.initState();
    _tabController = new TabController(
        vsync: this,
        length: 3);
    // 接受监听
    _tabController.addListener((){
      print(_tabController.index);
    });
  }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('顶部 tab 切换'),
        bottom: new TabBar(
          controller: _tabController, // 使用TabbarController必须加,相当于设置监听
          tabs: <Widget>[
            Tab(
              text: "热点",
            ),
            Tab(
              text: "新闻",
            ),
            Tab(
              text: "推荐",
            ),
          ],
        ),
      ),
      body: new TabBarView(
        controller: _tabController,
        children: <Widget>[
          new Center(child: new Text('热点')),
          new Center(child: new Text('新闻')),
          new Center(child: new Text('推荐')),
        ],
      ),
    );
  }
}

在这里插入图片描述

  • 以上示例均在前篇路由配置基础上进行配置
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值