Dart 语言 Dart 中的 AppBar 如何自定义

Dart阿木 发布于 2025-06-19 10 次阅读


Dart 中 AppBar 的自定义实现

在 Dart 语言中,`AppBar` 是 Flutter 应用中常见的组件之一,它通常位于屏幕的顶部,用于显示应用标题、导航按钮等。自定义 `AppBar` 可以让应用的外观更加独特和符合设计要求。本文将围绕 Dart 中 `AppBar` 的自定义展开,包括其基本结构、常用属性以及一些高级技巧。

`AppBar` 是 Flutter 中一个非常重要的组件,它不仅提供了应用的基本导航功能,还允许开发者通过自定义来增强用户体验。本文将详细介绍如何自定义 `AppBar`,包括样式、布局和交互等方面。

AppBar 基本结构

在 Dart 中,`AppBar` 通常包含以下部分:

- 标题(Title):显示在 AppBar 中的文本。

- 导航按钮(Leading):通常用于返回上一级页面。

- 操作按钮(Actions):用于显示额外的操作按钮。

- 主体内容(FlexibleSpaceBar):当 `AppBar` 在滚动视图中使用时,可以添加一个背景图片或渐变效果。

以下是一个简单的 `AppBar` 示例:

dart

import 'package:flutter/material.dart';

void main() {


runApp(MyApp());


}

class MyApp extends StatelessWidget {


@override


Widget build(BuildContext context) {


return MaterialApp(


title: 'AppBar Demo',


home: Scaffold(


appBar: AppBar(


title: Text('Custom AppBar'),


leading: BackButton(),


actions: <Widget>[


IconButton(icon: Icon(Icons.settings), onPressed: () {}),


],


),


body: Center(


child: Text('This is a custom AppBar example.'),


),


),


);


}


}


AppBar 常用属性

`AppBar` 提供了丰富的属性,以下是一些常用的属性:

- `title`:设置 AppBar 的标题。

- `leading`:设置 AppBar 的导航按钮,通常用于返回上一级页面。

- `actions`:设置 AppBar 的操作按钮。

- `bottom`:设置 AppBar 底部的导航栏。

- `flexibleSpace`:当 `AppBar` 在滚动视图中使用时,设置背景图片或渐变效果。

- `elevation`:设置 AppBar 的阴影效果。

- `centerTitle`:设置标题是否居中显示。

自定义 AppBar 样式

要自定义 `AppBar` 的样式,可以通过以下几种方式:

1. 使用主题(Theme)

通过设置主题(Theme)可以轻松地改变 `AppBar` 的颜色和字体样式。

dart

ThemeData(


appBarTheme: AppBarTheme(


color: Colors.blue,


textTheme: TextTheme(


title: TextStyle(


fontSize: 20,


fontWeight: FontWeight.bold,


color: Colors.white,


),


),


),


)


2. 直接设置样式

可以直接在 `AppBar` 组件中设置样式属性。

dart

AppBar(


title: Text('Custom AppBar'),


backgroundColor: Colors.blue,


elevation: 0,


titleTextStyle: TextStyle(


fontSize: 20,


fontWeight: FontWeight.bold,


color: Colors.white,


),


)


3. 使用自定义 Widget

可以通过创建自定义 Widget 来完全控制 `AppBar` 的布局和样式。

dart

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {


final Widget child;

CustomAppBar({Key key, this.child}) : super(key: key);

@override


Widget build(BuildContext context) {


return Container(


padding: EdgeInsets.symmetric(horizontal: 16),


decoration: BoxDecoration(


color: Colors.blue,


boxShadow: [


BoxShadow(


color: Colors.black12,


spreadRadius: 1,


blurRadius: 5,


),


],


),


child: child,


);


}

@override


Size get preferredSize => Size.fromHeight(kToolbarHeight);


}


高级技巧

1. 动态改变 AppBar 标题

在滚动视图(如 `ListView` 或 `PageView`)中,可以根据滚动位置动态改变 `AppBar` 的标题。

dart

class MyHomePage extends StatefulWidget {


@override


_MyHomePageState createState() => _MyHomePageState();


}

class _MyHomePageState extends State<MyHomePage> {


ScrollController _scrollController = ScrollController();

@override


Widget build(BuildContext context) {


return Scaffold(


appBar: AppBar(


title: Text('Dynamic AppBar Title'),


flexibleSpace: FlexibleSpaceBar(


title: Text('Scroll to change title'),


),


),


body: ListView.builder(


controller: _scrollController,


itemCount: 100,


itemBuilder: (context, index) {


return ListTile(


title: Text('Item $index'),


);


},


),


);


}


}


2. 使用 `BottomNavigationBar` 与 `AppBar`

在多页面应用中,`BottomNavigationBar` 和 `AppBar` 通常一起使用。可以通过 `BottomNavigationBar.onTap` 事件来动态改变 `AppBar` 的标题。

dart

class MyHomePage extends StatefulWidget {


@override


_MyHomePageState createState() => _MyHomePageState();


}

class _MyHomePageState extends State<MyHomePage> {


int _selectedIndex = 0;

void _onItemTapped(int index) {


setState(() {


_selectedIndex = index;


});


}

@override


Widget build(BuildContext context) {


return Scaffold(


appBar: AppBar(


title: Text('BottomNavigationBar with AppBar'),


),


body: Center(


child: Text('Selected index: $_selectedIndex'),


),


bottomNavigationBar: BottomNavigationBar(


items: <BottomNavigationBarItem>[


BottomNavigationBarItem(


icon: Icon(Icons.home),


label: 'Home',


),


BottomNavigationBarItem(


icon: Icon(Icons.business),


label: 'Business',


),


],


currentIndex: _selectedIndex,


onTap: _onItemTapped,


),


);


}


}


总结

自定义 `AppBar` 是 Flutter 开发中的一项重要技能,它可以帮助开发者创建出独特且符合设计要求的应用界面。本文介绍了 `AppBar` 的基本结构、常用属性、自定义样式以及一些高级技巧。通过学习和实践,开发者可以更好地利用 `AppBar` 来提升应用的用户体验。