I cannot figure out how to change the appBar\'s automatic back button to a different color. it\'s under a scaffold and I\'ve tried to research it but I can\'t wrap my head aroun
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.white, //modify arrow color from here..
),
);
To customise the leading icon, you may want to mimic the functionality of the AppBar
widget, which properly handles showing a back button, drawer icon, or close icon, depending on the current context. Here is an example which replaces the default icons.
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;
}
}
This class uses the PreferredSizeWidget
as a mixin, so you can use it as a replacement for an existing AppBar
widget in a Scaffold
. Note the _buildLeadingWidget
method, which only shows a back button if necessary, and shows a menu button if a drawer is present, or a close button if a full-screen dialog is displayed.
You can also set leading icon color globally for the app
MaterialApp(
theme: ThemeData(
appBarTheme: AppBarTheme(
iconTheme: IconThemeData(
color: Colors.green
)
)
)
)