AppBar常见属性
- title:用于设置AppBar中间显示的标题内容,通常是一个
Text
组件,用于表明当前页面的主题或功能。例如:
AppBar(
title: Text('首页'),
)
- leading:通常放置在AppBar的左侧,一般用于返回按钮、菜单按钮等。例如设置一个返回按钮:
AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
),
)
- actions:放置在AppBar的右侧,一般用于一些操作按钮,比如搜索、分享等。例如添加一个分享按钮:
AppBar(
actions: [
IconButton(
icon: Icon(Icons.share),
onPressed: () {
// 分享逻辑
},
)
],
)
自定义AppBar背景颜色和文字颜色
- 自定义背景颜色:通过
backgroundColor
属性来设置。例如设置为蓝色:
AppBar(
backgroundColor: Colors.blue,
title: Text('自定义背景颜色'),
)
- 自定义文字颜色:可以通过
foregroundColor
属性设置AppBar内文字(包括标题、leading和actions中的文字)的颜色。例如设置为白色:
AppBar(
foregroundColor: Colors.white,
title: Text('自定义文字颜色'),
)