• 【Flutter】【widget】AppBar 顶部状态栏



    在这里插入图片描述

    前言


    一、AppBar 是什么?

    顶部状态栏的快速实现。默认新建的时候有一个标题。

    二、使用步骤

    1.布局如图

    在这里插入图片描述

    2.参数说明

    • centerTitle: true, 标题是否居中,ios 和android 风格不一样
    • shadowColor: Colors.green, 阴影的颜色
    • // surfaceTintColor: Colors.red,
    • backgroundColor: Colors.red, //背景颜色
    • foregroundColor: Colors.indigo, //应用程序栏中[Text]和[Icon]的默认颜色,如果没有设置就会使用该颜色
    • elevation: 10, //阴影
    • iconTheme: const IconThemeData(
      size: 40, color: Colors.black), //app bar 所有的里面的图标主题设置
    • shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(15))), //设置形状
    • leading: const Icon(Icons.arrow_circle_down_rounded),
    • automaticallyImplyLeading: false,
    • actions: [
      IconButton(onPressed: () {}, icon: const Icon(Icons.downhill_skiing)),
      IconButton(onPressed: () {}, icon: const Icon(Icons.padding)),
      ],
    • // flexibleSpace: Container(
      // height: 20,
      // color: Colors.red,
      // ),
    • title: const Text(“我是标题”), //标题*

    代码如下(示例):

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      // This widget is the root of your application.
      
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
    
      final String title;
    
      
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
                  centerTitle: true,
            shadowColor: Colors.green,
            // surfaceTintColor: Colors.red,
            backgroundColor: Colors.red, //背景颜色
            foregroundColor: Colors.indigo, //应用程序栏中[Text]和[Icon]的默认颜色,如果没有设置就会使用该颜色
            elevation: 10,
            iconTheme: const IconThemeData(
                size: 40, color: Colors.black), //app bar 所有的里面的图标主题设置
            shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(15))), //设置形状
            leading: const Icon(Icons.arrow_circle_down_rounded),
            automaticallyImplyLeading: false,
            actions: [
              IconButton(onPressed: () {}, icon: const Icon(Icons.downhill_skiing)),
              IconButton(onPressed: () {}, icon: const Icon(Icons.padding)),
            ],
            // flexibleSpace: Container(
            //   height: 20,
            //   color: Colors.red,
            // ),
            title: const Text("我是标题"), //标题
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    2.特别的bottom

    1.PreferredSize

    在appbar 下面的botton 可以作为顶部导航栏的功能实现,他看也是一个widget:

          appBar: AppBar(
            centerTitle: true,
    
            title: const Text("我是标题"), //标题
            bottom: const PreferredSize(
                child: Text('PreferredSize'), preferredSize: Size(10, 10)),
          ),
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    2.作为顶部导航栏

    TabBar 和 TabBarView 配合使用,lenth两个要一致不然会报错,然后需要TabController

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      // This widget is the root of your application.
      
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
    
      final String title;
    
      
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage>
        with SingleTickerProviderStateMixin {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      final tabNameList = ["热门", "本地", "新闻", "优惠"];
      late TabController _tabController;
      
      void initState() {
        // TODO: implement initState
        super.initState();
        _tabController = TabController(length: tabNameList.length, vsync: this);
      }
    
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.pink,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(15))),
            centerTitle: true,
            title: const Text("我是标题"), //标题
            bottom: TabBar(
                controller: _tabController,
                isScrollable: true, //可滚动
                indicatorColor: Colors.indigo, //指示器的颜色
                labelColor: Colors.white, //选中文字颜色
                unselectedLabelColor: Colors.black, //没有选中文字颜色
    
                tabs: <Widget>[
                  for (int i = 0; i < tabNameList.length; i++)
                    Tab(text: tabNameList[i])
                ]),
          ),
          body: TabBarView(
              controller: _tabController,
              children: tabNameList
                  .map((e) => Center(
                        child: Text(e),
                      ))
                  .toList()),
    
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    在这里插入图片描述

    3.其他 AppBar 可以设置透明?

    在scaffold下面:设置颜色等
    但是其他事不需要设置透明,直接去掉,然后自己新建widget。完美

    extendBody: true, //底部NavigationBar透明
    extendBodyBehindAppBar: true,//顶部Bar透明
    
    • 1
    • 2

    3.backbutton

    如果构建了appbar,并且不是在主页面的情况,会自动生成backbutton 按键,点击默认靠右返回到上一个页面.

    第二个页面,点击floatbutton 跳转到第二个页面

    class SecondPage extends StatefulWidget {
      const SecondPage({Key? key}) : super(key: key);
    
      
      _SecondPageState createState() => _SecondPageState();
    }
    
    class _SecondPageState extends State<SecondPage> {
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('second page'),
          ),
          body: Container(
            color: Colors.red,
          ),
        );
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    总结

    欢迎关注,留言,咨询,交流!
    在这里插入图片描述

  • 相关阅读:
    Qt之分类导航主界面
    ubuntu 查看5000端口是否开放
    java计算机毕业设计项目任务跟踪系统源码+系统+mysql数据库+lw文档+部署
    2022美亚杯电子数据取证大赛-个人赛
    Nginx配置IP拦截
    docker安装elasticsearch、kibana
    脊髓损伤会引起哪些并发症
    USB3.0:VL817Q7-C0的LAYOUT指南(二)
    numpy(2)
    【校招VIP】产品项目计划之功能分析
  • 原文地址:https://blog.csdn.net/weixin_43444734/article/details/127681974