Flutter开发之appBar简介

appBar的简介

AppBar在APP上的上方(如果有)显示工具栏小部件、前导、标题和操作。
而APP的底部通常用于TabBar。

appBar的位置图说明

下图显示了当书写语言从左到右(例如英语)时,工具栏中每个插槽的位置:
image.png

appBar的属性

actions → List?
要显示在标题小部件后面一行中的小部件列表。 这个最常用
actionsIconTheme → IconThemeData?
应用程序栏中显示的图标的颜色、不透明度和大小。
automaticallyImplyLeading → bool
控制如果为空,是否应尝试暗示前导小部件。
backgroundColor → Color?
用于应用程序栏的“材质”的填充颜色
bottom → PreferredSizeWidget?
此小部件显示在应用程序栏的底部。
bottomOpacity → double
How opaque the bottom part of the app bar is.
centerTitle → bool?
Whether the title should be centered.
elevation → double?
The z-coordinate at which to place this app bar relative to its parent.
excludeHeaderSemantics → bool
Whether the title should be wrapped with header Semantics.
flexibleSpace → Widget?
This widget is stacked behind the toolbar and the tab bar. Its height will be the same as the app bar’s overall height.
foregroundColor → Color?
The default color for Text and Icons within the app bar.
hashCode → int
The hash code for this object.read-onlyinherited
iconTheme → IconThemeData?
The color, opacity, and size to use for toolbar icons.
key → Key?
Controls how one widget replaces another widget in the tree.finalinherited
leading → Widget?
A widget to display before the toolbar’s title.
leadingWidth → double?
Defines the width of leading widget.
notificationPredicate → ScrollNotificationPredicate
A check that specifies which child’s ScrollNotifications should be listened to.
preferredSize → Size
A size whose height is the sum of toolbarHeight and the bottom widget’s preferred height.
primary → bool
Whether this app bar is being displayed at the top of the screen.
runtimeType → Type
A representation of the runtime type of the object.read-onlyinherited
scrolledUnderElevation → double?
The elevation that will be used if this app bar has something scrolled underneath it.
shadowColor → Color?
The color of the shadow below the app bar.
shape → ShapeBorder?
The shape of the app bar’s Material as well as its shadow.
surfaceTintColor → Color?
The color of the surface tint overlay applied to the app bar’s background color to indicate elevation.
systemOverlayStyle → SystemUiOverlayStyle?
Specifies the style to use for the system overlays that overlap the AppBar.
title → Widget?
The primary widget displayed in the app bar.
titleSpacing → double?
The spacing around title content on the horizontal axis. This spacing is applied even if there is no leading content or actions. If you want title to take all the space available, set this value to 0.0.
titleTextStyle → TextStyle?
The default text style for the AppBar’s title widget.
toolbarHeight → double?
Defines the height of the toolbar component of an AppBar.
toolbarOpacity → double
How opaque the toolbar part of the app bar is.
toolbarTextStyle → TextStyle?
The default text style for the AppBar’s leading, and actions widgets, but not its title.

重要属性

  1. title: title属性用于设置AppBar的标题文本。您可以通过Text小部件或其他类似的小部件来指定标题文本的样式和内容。

  2. leading: leading属性指定在标题之前显示的小部件,通常用于放置导航返回按钮或应用程序的Logo图标。

  3. actions: actions属性用于指定位于标题之后的一系列小部件,这些小部件可以是操作按钮、图标按钮或其他自定义小部件。

  4. backgroundColor: backgroundColor属性用于设置AppBar的背景颜色。您可以使用Color类或Color常量来指定颜色值。

  5. elevation: elevation属性控制AppBar的阴影效果。通过调整该值,您可以改变AppBar的高程感觉。

  6. brightness: brightness属性用于设置状态栏和导航栏的亮度。可以选择Brightness.dark或Brightness.light,以使状态栏和导航栏的文本和图标颜色与背景适配。

  7. centerTitle: centerTitle属性控制标题是否居中显示。设置为true时,标题将居中显示;设置为false时,标题将左对齐显示。

  8. bottom: bottom属性允许您在AppBar底部添加额外的小部件,如选项卡栏或其他自定义内容。

appBar的实例

先看个例子:我们创建一个leading为一个IconButton,title 为“appBar Demo”,actiions添加两个IconButton的 appBar。运行的效果图如下:
image.png

源码如下:

import 'package:flutter/material.dart';

void main() => runApp(const AppBarApp());

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

  
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: AppBarExample(),
    );
  }
}

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

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('AppBar Demo'),
        leading: IconButton(
            icon: const Icon(Icons.add_alarm_rounded),
            tooltip: 'Show Snackbar',
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('This is a snackbar')));
            },
          ),

        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.add_alarm_rounded),
            tooltip: 'Show Snackbar',
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('This is a snackbar')));
            },
          ),
          IconButton(
            icon: const Icon(Icons.navigate_next),
            tooltip: 'Go to the next page',
            onPressed: () {
              Navigator.push(context, MaterialPageRoute<void>(
                builder: (BuildContext context) {
                  return Scaffold(
                    appBar: AppBar(
                      title: const Text('Next page'),
                      // automaticallyImplyLeading: false,
                    ),
                    body: const Center(
                      child: Text(
                        'This is the next page',
                        style: TextStyle(fontSize: 24),
                      ),
                    ),
                  );
                },
              ));
            },
          ),
        ],
      ),
      body: const Center(
        child: Text(
          'This is the home page',
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}

没有太多要说明的,对着运行的效果图
先看leading,这个例子中我们添加了一个IconButton。
title添加的是一个 Text(‘AppBar Demo’),
actions区域也是添加了2个IconButton。
flexibleSpace 和 bottom后面总结。

用的比较多的情景

1、在actions中添加PopupMenuButton

  actions: [
          PopupMenuButton<PopupAction>(
            onSelected: _handleAction,
            itemBuilder: (BuildContext context) {
              return PopupAction.values.map((PopupAction choice) {
                return PopupMenuItem<PopupAction>(
                  value: choice,
                  child: Text(
                    choice == PopupAction.add
                        ? 'Add cfg'
                        : 'Remove cfg',
                  ),
                );
              }).toList();
            },
          ),
        ],
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flutter 开发中,"DragScrollSheet" 是一个用于实现可拖动滚动的小部件。它允许用户在屏幕上拖动内容并进行滚动,类似于滚动视图(ScrollView),但具有更灵活的交互方式。 你可以使用 `DraggableScrollableSheet` 小部件来实现这样的效果。它通常用作页面的底部或侧边面板,用户可以通过拖动该面板来展示更多内容。 以下是一个简单的示例代码,演示了如何使用 `DraggableScrollableSheet`: ```dart @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('DraggableScrollableSheet'), ), body: Stack( children: <Widget>[ // 页面主体内容 ListView.builder( itemCount: 100, itemBuilder: (context, index) => ListTile( title: Text('Item $index'), ), ), // 可拖动的滚动面板 DraggableScrollableSheet( builder: (context, scrollController) => Container( color: Colors.grey[300], child: ListView.builder( controller: scrollController, itemCount: 20, itemBuilder: (context, index) => ListTile( title: Text('Sheet Item $index'), ), ), ), ), ], ), ); } ``` 在这个示例中,页面主体内容由一个 `ListView.builder` 构成,其中包含了一些简单的列表项。而 `DraggableScrollableSheet` 是一个具有固定高度的容器,里面包含了另一个 `ListView.builder`,用于展示可拖动面板中的内容。 你可以根据需要自定义 `DraggableScrollableSheet` 的样式和行为,例如通过设置高度、背景颜色、滚动控制器等。希望这个示例能帮到你!如果有更多问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值