flutter->appBar配置和顶部导航栏

本文详细介绍了如何在Flutter中使用`MaterialApp`、`TabController`、`TabBar`和`TabBarView`构建一个多页应用程序,包括设置顶部导航栏、底部标签页和内容区域的实现方法。
摘要由CSDN通过智能技术生成

 注意: 
       appBar:PreferredSize(child: ,preferredSize: Size.fromHeight(50),)//设置appBar高度
       bottom内的controller 和 body内的controller必须配置
       bottom内的tabs数量 和 body内的TabBarView:children 数量必须一致
       


       @override //生命周期函数:当组件初始化的时候执行
       void initState() {
            super.initState();
            _tabController = TabController(length: 4, vsync: this);
        }
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
        title: 'contaniner',
        home: HomePage(),
        debugShowCheckedModeBanner: false);
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

//with 混入mixins
class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
 
  TabController? _tabController;

  @override //生命周期函数:当组件初始化的时候执行
  void initState() {
    super.initState();
    setState(() {
      // 将参数赋予存储点击数的变量
      _tabController = TabController(length: 4, vsync: this);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Center(child: Text('flutter bar')),
        leading: IconButton(
          iconSize: 22,
          onPressed: () => print("我是图标按钮1"),
          icon: Icon(Icons.ac_unit), //设置图标
          color: Colors.red, //设置按钮颜色
          splashColor: Colors.yellow, //设置水波纹
          highlightColor: Colors.purple, //设置高亮的颜色
          tooltip: '我是提示信息', //提示信息
        ),
        actions: const [
          Icon(Icons.search, color: Colors.white),
          Icon(Icons.more_horiz, color: Colors.white),
        ],
        //配置顶部导航栏
        bottom: TabBar(
          // isScrollable: true,//是否可以滑动
          controller: _tabController, //配置controller
          indicatorWeight: 5,//配置指示器高度
          indicatorPadding: const EdgeInsets.all(5), //配置指示器底部padding
          indicatorColor: Colors.green,//配置底部指示器颜色 和indicator 冲突
          indicator: const BoxDecoration( 
            color: Colors.red //该颜色配置后indicatorColor不生效
          ),
          indicatorSize: TabBarIndicatorSize.tab, //TabBarIndicatorSize.tab:指示器和tab等宽,  TabBarIndicatorSize.label:指示器和文字等宽(默认)
          //如果配置已选中,未选中没有配置则样式通用
          labelColor: Colors.yellow,  //选中文字颜色 该颜色配置优先于labelStyle内的颜色配置
          labelStyle: const TextStyle(color: Colors.green,fontSize: 22), //配置选中tab样式
          labelPadding: EdgeInsets.all(10), //配置选中tabpadding值
          unselectedLabelColor: Colors.white, //配置未选中tab的颜色
          unselectedLabelStyle: const TextStyle(color: Colors.green,fontSize: 12), //配置未选中tab的样式
          tabs: const [
            Tab(child: Text("测试1")),
            Tab(child: Text("测试2")),
            Tab(child: Text("测试3")),
            Tab(child: Text("测试4")),
          ],
        ),
        backgroundColor: Colors.cyan[800],
        elevation: 0.0,
        centerTitle: true,
      ),
      //带索引的集合循环
      body: TabBarView(
        controller: _tabController,
        children: const [
          HomeWidgetOne(),
          HomeWidgetTwo(),
          HomeWidgetThree(),
          HomeWidgetFour(),
        ],
      ),
    );
  }
}

class HomeWidgetOne extends StatelessWidget {
  const HomeWidgetOne({super.key});

  @override
  Widget build(BuildContext context) {
    // return const Text('我是首页数据', style: TextStyle(color: Colors.red));
    return ListView(
        children: List.generate(
      20,
      (index) => ListTile(
        leading: Icon(Icons.access_alarm, size: 30),
        title: Text('第一页$index'),
        subtitle: Text('第一页子标题$index'),
        selected: index == 1,
        selectedColor: Colors.green,
        trailing: Icon(Icons.keyboard_arrow_right),
      ),
    ));
  }
}

class HomeWidgetTwo extends StatelessWidget {
  const HomeWidgetTwo({super.key});

  @override
  Widget build(BuildContext context) {
    // return const Text('我是首页数据', style: TextStyle(color: Colors.red));
    return ListView(
        children: List.generate(
      20,
      (index) => ListTile(
        leading: Icon(Icons.access_alarm, size: 30),
        title: Text('第二页$index'),
        subtitle: Text('第二页面子标题$index'),
        selected: index == 1,
        selectedColor: Colors.green,
        trailing: Icon(Icons.keyboard_arrow_right),
      ),
    ));
  }
}

class HomeWidgetThree extends StatelessWidget {
  const HomeWidgetThree({super.key});

  @override
  Widget build(BuildContext context) {
    // return const Text('我是首页数据', style: TextStyle(color: Colors.red));
    return ListView(
        children: List.generate(
      20,
      (index) => ListTile(
        leading: Icon(Icons.access_alarm, size: 30),
        title: Text('第三页测试$index'),
        subtitle: Text('第三页子标题$index'),
        selected: index == 1,
        selectedColor: Colors.green,
        trailing: Icon(Icons.keyboard_arrow_right),
      ),
    ));
  }
}

class HomeWidgetFour extends StatelessWidget {
  const HomeWidgetFour({super.key});

  @override
  Widget build(BuildContext context) {
    // return const Text('我是首页数据', style: TextStyle(color: Colors.red));
    return ListView(
        children: List.generate(
      20,
      (index) => ListTile(
        leading: Icon(Icons.access_alarm, size: 30),
        title: Text('第四页测试$index'),
        subtitle: Text('第四页子标题$index'),
        selected: index == 1,
        selectedColor: Colors.green,
        trailing: Icon(Icons.keyboard_arrow_right),
      ),
    ));
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值