Implement actionsPadding for AppBar (#155632)
This pull requests adds a new `actionsPadding` property to AppBar, which controls the padding of the actions row's end. By default, no padding is added. As it was already discussed in the linked issue below (https://github.com/flutter/flutter/issues/115349#issuecomment-1315582192), it is not feasible to change defaults. This PR is an interim solution to allow developers to change layout across the app, until that change/transition can be made (see: the new TODO line). Different to the original code from 2018, this PR only adds padding to the actual actions row, instead of the entire `NavigationToolbar`. Fixes #155747
This commit is contained in:
@@ -56,6 +56,12 @@ class _${blockName}DefaultsM3 extends AppBarTheme {
|
||||
|
||||
@override
|
||||
TextStyle? get titleTextStyle => ${textStyle('md.comp.top-app-bar.small.headline')};
|
||||
|
||||
// TODO(Craftplacer): Consider using EdgeInsets.only(right: 8.0) instead of
|
||||
// EdgeInsets.zero for Material 3 in the future,
|
||||
// https://github.com/flutter/flutter/issues/155747
|
||||
@override
|
||||
EdgeInsets? get actionsPadding => EdgeInsets.zero;
|
||||
}
|
||||
|
||||
// Variant configuration
|
||||
|
||||
@@ -218,6 +218,7 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
|
||||
this.systemOverlayStyle,
|
||||
this.forceMaterialTransparency = false,
|
||||
this.clipBehavior,
|
||||
this.actionsPadding,
|
||||
}) : assert(elevation == null || elevation >= 0.0),
|
||||
preferredSize = _PreferredAppBarSize(toolbarHeight, bottom?.preferredSize.height);
|
||||
|
||||
@@ -744,6 +745,13 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
|
||||
/// {@macro flutter.material.Material.clipBehavior}
|
||||
final Clip? clipBehavior;
|
||||
|
||||
/// {@template flutter.material.appbar.actionsPadding}
|
||||
/// The padding between the [actions] and the end of the AppBar.
|
||||
///
|
||||
/// Defaults to zero.
|
||||
/// {@endtemplate}
|
||||
final EdgeInsetsGeometry? actionsPadding;
|
||||
|
||||
bool _getEffectiveCenterTitle(ThemeData theme) {
|
||||
bool platformCenter() {
|
||||
switch (theme.platform) {
|
||||
@@ -906,6 +914,10 @@ class _AppBarState extends State<AppBar> {
|
||||
?? defaults.actionsIconTheme?.copyWith(color: actionForegroundColor)
|
||||
?? overallIconTheme;
|
||||
|
||||
final EdgeInsetsGeometry actionsPadding = widget.actionsPadding
|
||||
?? appBarTheme.actionsPadding
|
||||
?? defaults.actionsPadding!;
|
||||
|
||||
TextStyle? toolbarTextStyle = widget.toolbarTextStyle
|
||||
?? appBarTheme.toolbarTextStyle
|
||||
?? defaults.toolbarTextStyle?.copyWith(color: foregroundColor);
|
||||
@@ -1019,10 +1031,13 @@ class _AppBarState extends State<AppBar> {
|
||||
|
||||
Widget? actions;
|
||||
if (widget.actions != null && widget.actions!.isNotEmpty) {
|
||||
actions = Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: theme.useMaterial3 ? CrossAxisAlignment.center : CrossAxisAlignment.stretch,
|
||||
children: widget.actions!,
|
||||
actions = Padding(
|
||||
padding: actionsPadding,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: theme.useMaterial3 ? CrossAxisAlignment.center : CrossAxisAlignment.stretch,
|
||||
children: widget.actions!,
|
||||
),
|
||||
);
|
||||
} else if (hasEndDrawer) {
|
||||
actions = EndDrawerButton(
|
||||
@@ -1219,6 +1234,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
|
||||
required this.clipBehavior,
|
||||
required this.variant,
|
||||
required this.accessibleNavigation,
|
||||
required this.actionsPadding,
|
||||
}) : assert(primary || topPadding == 0.0),
|
||||
_bottomHeight = bottom?.preferredSize.height ?? 0.0;
|
||||
|
||||
@@ -1257,6 +1273,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
|
||||
final Clip? clipBehavior;
|
||||
final _SliverAppVariant variant;
|
||||
final bool accessibleNavigation;
|
||||
final EdgeInsetsGeometry? actionsPadding;
|
||||
|
||||
@override
|
||||
double get minExtent => collapsedHeight;
|
||||
@@ -1338,6 +1355,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
|
||||
titleTextStyle: titleTextStyle,
|
||||
systemOverlayStyle: systemOverlayStyle,
|
||||
forceMaterialTransparency: forceMaterialTransparency,
|
||||
actionsPadding: actionsPadding,
|
||||
),
|
||||
);
|
||||
return appBar;
|
||||
@@ -1376,7 +1394,8 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
|
||||
|| titleTextStyle != oldDelegate.titleTextStyle
|
||||
|| systemOverlayStyle != oldDelegate.systemOverlayStyle
|
||||
|| forceMaterialTransparency != oldDelegate.forceMaterialTransparency
|
||||
|| accessibleNavigation != oldDelegate.accessibleNavigation;
|
||||
|| accessibleNavigation != oldDelegate.accessibleNavigation
|
||||
|| actionsPadding != oldDelegate.actionsPadding;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1516,6 +1535,7 @@ class SliverAppBar extends StatefulWidget {
|
||||
this.systemOverlayStyle,
|
||||
this.forceMaterialTransparency = false,
|
||||
this.clipBehavior,
|
||||
this.actionsPadding,
|
||||
}) : assert(floating || !snap, 'The "snap" argument only makes sense for floating app bars.'),
|
||||
assert(stretchTriggerOffset > 0.0),
|
||||
assert(
|
||||
@@ -1584,6 +1604,7 @@ class SliverAppBar extends StatefulWidget {
|
||||
this.systemOverlayStyle,
|
||||
this.forceMaterialTransparency = false,
|
||||
this.clipBehavior,
|
||||
this.actionsPadding,
|
||||
}) : assert(floating || !snap, 'The "snap" argument only makes sense for floating app bars.'),
|
||||
assert(stretchTriggerOffset > 0.0),
|
||||
assert(
|
||||
@@ -1652,6 +1673,7 @@ class SliverAppBar extends StatefulWidget {
|
||||
this.systemOverlayStyle,
|
||||
this.forceMaterialTransparency = false,
|
||||
this.clipBehavior,
|
||||
this.actionsPadding,
|
||||
}) : assert(floating || !snap, 'The "snap" argument only makes sense for floating app bars.'),
|
||||
assert(stretchTriggerOffset > 0.0),
|
||||
assert(
|
||||
@@ -1915,6 +1937,11 @@ class SliverAppBar extends StatefulWidget {
|
||||
/// {@macro flutter.material.Material.clipBehavior}
|
||||
final Clip? clipBehavior;
|
||||
|
||||
/// {@macro flutter.material.appbar.actionsPadding}
|
||||
///
|
||||
/// This property is used to configure an [AppBar].
|
||||
final EdgeInsetsGeometry? actionsPadding;
|
||||
|
||||
final _SliverAppVariant _variant;
|
||||
|
||||
@override
|
||||
@@ -2059,6 +2086,7 @@ class _SliverAppBarState extends State<SliverAppBar> with TickerProviderStateMix
|
||||
clipBehavior: widget.clipBehavior,
|
||||
variant: widget._variant,
|
||||
accessibleNavigation: MediaQuery.of(context).accessibleNavigation,
|
||||
actionsPadding: widget.actionsPadding,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -2366,6 +2394,9 @@ class _AppBarDefaultsM2 extends AppBarTheme {
|
||||
|
||||
@override
|
||||
TextStyle? get titleTextStyle => _theme.textTheme.titleLarge;
|
||||
|
||||
@override
|
||||
EdgeInsets? get actionsPadding => EdgeInsets.zero;
|
||||
}
|
||||
|
||||
// BEGIN GENERATED TOKEN PROPERTIES - AppBar
|
||||
@@ -2418,6 +2449,12 @@ class _AppBarDefaultsM3 extends AppBarTheme {
|
||||
|
||||
@override
|
||||
TextStyle? get titleTextStyle => _textTheme.titleLarge;
|
||||
|
||||
// TODO(Craftplacer): Consider using EdgeInsets.only(right: 8.0) instead of
|
||||
// EdgeInsets.zero for Material 3 in the future,
|
||||
// https://github.com/flutter/flutter/issues/155747
|
||||
@override
|
||||
EdgeInsets? get actionsPadding => EdgeInsets.zero;
|
||||
}
|
||||
|
||||
// Variant configuration
|
||||
|
||||
@@ -46,6 +46,7 @@ class AppBarTheme with Diagnosticable {
|
||||
this.toolbarTextStyle,
|
||||
this.titleTextStyle,
|
||||
this.systemOverlayStyle,
|
||||
this.actionsPadding,
|
||||
}) : assert(
|
||||
color == null || backgroundColor == null,
|
||||
'The color and backgroundColor parameters mean the same thing. Only specify one.',
|
||||
@@ -153,6 +154,10 @@ class AppBarTheme with Diagnosticable {
|
||||
/// property in all descendant [AppBar] widgets.
|
||||
final SystemUiOverlayStyle? systemOverlayStyle;
|
||||
|
||||
/// Overrides the default value of [AppBar.actionsPadding]
|
||||
/// property in all descendant [AppBar] widgets.
|
||||
final EdgeInsetsGeometry? actionsPadding;
|
||||
|
||||
/// Creates a copy of this object with the given fields replaced with the
|
||||
/// new values.
|
||||
AppBarTheme copyWith({
|
||||
@@ -172,6 +177,7 @@ class AppBarTheme with Diagnosticable {
|
||||
TextStyle? toolbarTextStyle,
|
||||
TextStyle? titleTextStyle,
|
||||
SystemUiOverlayStyle? systemOverlayStyle,
|
||||
EdgeInsetsGeometry? actionsPadding,
|
||||
}) {
|
||||
assert(
|
||||
color == null || backgroundColor == null,
|
||||
@@ -193,6 +199,7 @@ class AppBarTheme with Diagnosticable {
|
||||
toolbarTextStyle: toolbarTextStyle ?? this.toolbarTextStyle,
|
||||
titleTextStyle: titleTextStyle ?? this.titleTextStyle,
|
||||
systemOverlayStyle: systemOverlayStyle ?? this.systemOverlayStyle,
|
||||
actionsPadding: actionsPadding ?? this.actionsPadding,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -224,6 +231,7 @@ class AppBarTheme with Diagnosticable {
|
||||
toolbarTextStyle: TextStyle.lerp(a?.toolbarTextStyle, b?.toolbarTextStyle, t),
|
||||
titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t),
|
||||
systemOverlayStyle: t < 0.5 ? a?.systemOverlayStyle : b?.systemOverlayStyle,
|
||||
actionsPadding: EdgeInsetsGeometry.lerp(a?.actionsPadding, b?.actionsPadding, t),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -244,6 +252,7 @@ class AppBarTheme with Diagnosticable {
|
||||
toolbarTextStyle,
|
||||
titleTextStyle,
|
||||
systemOverlayStyle,
|
||||
actionsPadding,
|
||||
);
|
||||
|
||||
@override
|
||||
@@ -269,7 +278,8 @@ class AppBarTheme with Diagnosticable {
|
||||
&& other.toolbarHeight == toolbarHeight
|
||||
&& other.toolbarTextStyle == toolbarTextStyle
|
||||
&& other.titleTextStyle == titleTextStyle
|
||||
&& other.systemOverlayStyle == systemOverlayStyle;
|
||||
&& other.systemOverlayStyle == systemOverlayStyle
|
||||
&& other.actionsPadding == actionsPadding;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -289,5 +299,6 @@ class AppBarTheme with Diagnosticable {
|
||||
properties.add(DiagnosticsProperty<double>('toolbarHeight', toolbarHeight, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<TextStyle>('toolbarTextStyle', toolbarTextStyle, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('actionsPadding', actionsPadding, defaultValue: null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3254,6 +3254,37 @@ void main() {
|
||||
expect(find.text('Second Screen'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('AppBar actions padding can be adjusted', (WidgetTester tester) async {
|
||||
final Key appBarKey = UniqueKey();
|
||||
final Key actionKey = UniqueKey();
|
||||
|
||||
Widget buildAppBar({ EdgeInsetsGeometry? actionsPadding }) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
key: appBarKey,
|
||||
actions: <Widget>[
|
||||
SizedBox.square(key: actionKey, dimension: 40.0),
|
||||
],
|
||||
actionsPadding: actionsPadding,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await tester.pumpWidget(buildAppBar());
|
||||
|
||||
// Actions padding default to zero padding.
|
||||
Offset actionsOffset = tester.getTopRight(find.byKey(actionKey));
|
||||
final Offset appBarOffset = tester.getTopRight(find.byKey(appBarKey));
|
||||
expect(appBarOffset.dx - actionsOffset.dx, 0);
|
||||
|
||||
const EdgeInsets actionsPadding = EdgeInsets.only(right: 8.0);
|
||||
await tester.pumpWidget(buildAppBar(actionsPadding: actionsPadding));
|
||||
actionsOffset = tester.getTopRight(find.byKey(actionKey));
|
||||
expect(actionsOffset.dx, equals(appBarOffset.dx - actionsPadding.right));
|
||||
});
|
||||
|
||||
group('Material 2', () {
|
||||
testWidgets('Material2 - AppBar draws a light system bar for a dark background', (WidgetTester tester) async {
|
||||
final ThemeData darkTheme = ThemeData.dark(useMaterial3: false);
|
||||
|
||||
Reference in New Issue
Block a user