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

 注意: 
       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
    评论
Flutter中设置顶部导航栏可以使用AppBar组件。AppBar组件是Material Design中的一个标准组件,通常用于顶部导航栏或者应用栏。 以下是如何在Flutter中设置顶部导航栏的步骤: 1. 导入Material库:在Flutter中使用AppBar组件需要导入Material库,可以在文件开头添加以下代码: ``` import 'package:flutter/material.dart'; ``` 2. 创建AppBar:使用AppBar组件来创建顶部导航栏,可以在Scaffold组件中添加AppBar,如下所示: ``` Scaffold( appBar: AppBar( title: Text("My App"), ), body: Container(), ); ``` 在这个例子中,我们创建了一个标题为“My App”的AppBar。 3. 自定义AppBar:AppBar组件支持许多自定义属性,比如颜色、图标、菜单等。下面是一些常用的自定义属性: - backgroundColor:设置导航栏背景颜色 - actions:设置导航栏右侧的操作按钮 - leading:设置导航栏左侧的返回按钮 - elevation:设置导航栏的阴影大小 - title:设置导航栏标题 以下是一个自定义AppBar的例子: ``` Scaffold( appBar: AppBar( title: Text("My App"), backgroundColor: Colors.blue, actions: [ IconButton( icon: Icon(Icons.search), onPressed: (){}, ), IconButton( icon: Icon(Icons.more_vert), onPressed: (){}, ), ], leading: IconButton( icon: Icon(Icons.menu), onPressed: (){}, ), elevation: 4.0, ), body: Container(), ); ``` 在这个例子中,我们设置了导航栏的背景颜色为蓝色,右侧添加了两个操作按钮,左侧添加了一个菜单按钮,并且设置了导航栏的阴影大小为4.0。 通过以上步骤,你已经成功设置了顶部导航栏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值