Flutter组件————AppBar

AppBar 是 Flutter 中用于创建应用程序顶部栏的组件,它遵循 Material Design 规范。

参数:

参数名称 类型 描述
title Widget 设置 AppBar 中的标题文本或自定义标题小部件。
automaticallyImplyLeading bool 决定是否自动添加返回按钮(如果页面不是首页)。
leading Widget 设置 AppBar 左侧的控件,如菜单图标或自定义小部件。
actions List<Widget> 设置 AppBar 右侧的操作按钮列表。
elevation double 定义 AppBar 下方阴影的高度。
centerTitle bool 决定 title 是否居中显示,默认值根据平台而异。
backgroundColor Color 设置 AppBar 的背景颜色。
foregroundColor Color 设置 AppBar 内部元素的颜色,例如标题和操作按钮的颜色。
shadowColor Color 设置 AppBar 阴影的颜色。
surfaceTintColor Color 设置 AppBar 表面高光的颜色。
toolbarHeight double 设置 AppBar 的总高度。
bottom PreferredSizeWidget 设置 AppBar 底部的小部件,如 TabBar
shape ShapeBorder 定义 AppBar 的形状,例如圆角矩形等。
iconTheme IconThemeData 用于定义 AppBar 内部图标的样式。
primary bool 指示此 AppBar 是否是屏幕的主要工具栏。
titleSpacing double 定义 title 周围的间距。

代码示例

dart 复制代码
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //所有右侧行为按钮
  List<Widget> actionList = const [
    Icon(Icons.social_distance),
    SizedBox(
      width: 30,
    ),
    Icon(Icons.cyclone),
    SizedBox(
      width: 30,
    ),
    Icon(Icons.manage_accounts),
    SizedBox(
      width: 40,
    )
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary, //背景颜色
        foregroundColor: const Color.fromARGB(135, 226, 5, 255),
        leading: const Icon(Icons.accessibility_new_rounded), //左侧按钮
        title: const Text("Flutter 示例"), //标题
        actions: actionList, //右侧按钮
        elevation: 10, //下方阴影
        shadowColor: const Color.fromARGB(136, 0, 225, 255), //阴影颜色
        centerTitle: true, // 标题是否居中(ios默认居中,Android默认居左)
        surfaceTintColor: const Color.fromARGB(172, 249, 128, 0), //表面颜色
        toolbarHeight: 45, //顶部栏高度
        iconTheme: const IconThemeData(
        size: 30, color: Color.fromARGB(207, 255, 251, 0)), //控制内部元素样式
        primary: true, // 是否显示主要按钮
        titleSpacing: 100, //标题内边距
        bottom: const TabBar(tabs: [
          Tab(icon: Icon(Icons.directions_car)),
          Tab(icon: Icon(Icons.directions_transit)),
          Tab(icon: Icon(Icons.directions_bike)),
        ]), //顶部栏底部按钮
        shape:const  RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(
            bottom: Radius.circular(15)
          )  //顶部栏底部按钮样式
        ),
      ), //顶部栏
      body: Center(
        child: ListView(
          children: [],
        ),
      ),
    );
  }
}

效果

相关推荐
EndingCoder8 小时前
跨平台移动开发框架React Native和Flutter性能对比
flutter·react native·react.js
Double Point8 小时前
`RotationTransition` 是 Flutter 中的一个动画组件,用于实现旋转动画效果
flutter
亚洲小炫风8 小时前
flutter 项目工程文件夹组织结构
flutter·flutter工程结构
Double Point8 小时前
Flutter 中 vsync
flutter
Double Point13 小时前
ScaleTransition 是 Flutter 中的一个动画组件,用于实现缩放动画效果。
flutter
saxihuangxing14 小时前
flutter build apk出现的一些奇怪的编译错误
flutter
恋猫de小郭1 天前
Flutter 合并 ‘dot-shorthands‘ 语法糖,Dart 开始支持交叉编译
android·flutter·ios
林家凌宇1 天前
Flutter 3.29.3 花屏问题记录
android·flutter·skia
恋猫de小郭2 天前
React Native 前瞻式重大更新 Skia & WebGPU & ThreeJS,未来可期
android·javascript·flutter·react native·react.js·ios
怀君2 天前
Flutter——数据库Drift开发详细教程(五)
数据库·flutter