我不知道如何将appBar的自动后退按钮更改为不同的颜色。它在脚手架下,我试图研究它,但我无法理解它。
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Image.asset(
'images/.jpg',
fit: BoxFit.fill,
),
centerTitle: true,
),
要自定义引导图标,您可能需要模仿AppBar小部件的功能,该小部件根据当前上下文正确地处理显示后退按钮、抽屉图标或关闭图标。下面是一个替换默认图标的例子。
import 'package:flutter/material.dart';
class TopBar extends StatelessWidget with PreferredSizeWidget {
static const double _topBarHeight = 60;
@override
Widget build(BuildContext context) {
return AppBar(
toolbarHeight: _topBarHeight,
title: Text('Title'),
automaticallyImplyLeading: false,
leading: _buildLeadingWidget(context),
);
}
@override
Size get preferredSize => Size.fromHeight(_topBarHeight);
Widget _buildLeadingWidget(BuildContext context) {
final ScaffoldState scaffold = Scaffold.of(context);
final ModalRoute<dynamic> parentRoute = ModalRoute.of(context);
final bool canPop = ModalRoute.of(context)?.canPop ?? false;
final bool hasDrawer = scaffold?.hasDrawer ?? false;
final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;
Widget leading;
if (hasDrawer) {
leading = IconButton(
icon: const Icon(Icons.menu_rounded),
onPressed: Scaffold.of(context).openDrawer,
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
);
} else {
if (canPop) {
if (useCloseButton) {
leading = IconButton(
color: Theme.of(context).colorScheme.onBackground,
icon: Icon(Icons.close_rounded),
onPressed: () => Navigator.of(context).maybePop());
} else {
leading = IconButton(
padding: EdgeInsets.all(0),
iconSize: 38,
icon: Icon(Icons.chevron_left_rounded),
onPressed: Navigator.of(context).pop);
}
}
}
return leading;
}
}
这个类使用PreferredSizeWidget作为一个mixin,因此您可以使用它来替换脚手架中现有的AppBar小部件。请注意_buildLeadingWidget方法,它只在必要时显示后退按钮,如果有抽屉,则显示菜单按钮,如果显示全屏对话框,则显示关闭按钮。
你必须使用AppBar中的iconTheme属性,像这样:
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.black, //change your color here
),
title: Text("Sample"),
centerTitle: true,
),
或者你想自己操作后退按钮。
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
),
title: Text("Sample"),
centerTitle: true,
),
更好的是,只有当你想改变后退按钮的颜色时才可以这样做。
appBar: AppBar(
leading: BackButton(
color: Colors.black
),
title: Text("Sample"),
centerTitle: true,
),