Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit d8fdb83

Browse files
authored
Update MaterialBanner to support Material 3 (#105957)
1 parent 202f577 commit d8fdb83

File tree

8 files changed

+400
-38
lines changed

8 files changed

+400
-38
lines changed

dev/tools/gen_defaults/bin/gen_defaults.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
import 'dart:convert';
1818
import 'dart:io';
1919

20+
import 'package:gen_defaults/action_chip_template.dart';
2021
import 'package:gen_defaults/app_bar_template.dart';
22+
import 'package:gen_defaults/banner_template.dart';
2123
import 'package:gen_defaults/button_template.dart';
2224
import 'package:gen_defaults/card_template.dart';
2325
import 'package:gen_defaults/checkbox_template.dart';
24-
import 'package:gen_defaults/chip_action_template.dart';
25-
import 'package:gen_defaults/chip_filter_template.dart';
26-
import 'package:gen_defaults/chip_input_template.dart';
2726
import 'package:gen_defaults/dialog_template.dart';
2827
import 'package:gen_defaults/fab_template.dart';
28+
import 'package:gen_defaults/filter_chip_template.dart';
2929
import 'package:gen_defaults/icon_button_template.dart';
30+
import 'package:gen_defaults/input_chip_template.dart';
3031
import 'package:gen_defaults/input_decorator_template.dart';
3132
import 'package:gen_defaults/navigation_bar_template.dart';
3233
import 'package:gen_defaults/navigation_rail_template.dart';
@@ -103,17 +104,18 @@ Future<void> main(List<String> args) async {
103104
tokens['colorsDark'] = _readTokenFile('color_dark.json');
104105

105106
AppBarTemplate('AppBar', '$materialLib/app_bar.dart', tokens).updateFile();
107+
BannerTemplate('Banner', '$materialLib/banner.dart', tokens).updateFile();
106108
ButtonTemplate('md.comp.elevated-button', 'ElevatedButton', '$materialLib/elevated_button.dart', tokens).updateFile();
107109
ButtonTemplate('md.comp.filled-button', 'FilledButton', '$materialLib/filled_button.dart', tokens).updateFile();
108110
ButtonTemplate('md.comp.filled-tonal-button', 'FilledTonalButton', '$materialLib/filled_button.dart', tokens).updateFile();
109111
ButtonTemplate('md.comp.outlined-button', 'OutlinedButton', '$materialLib/outlined_button.dart', tokens).updateFile();
110112
ButtonTemplate('md.comp.text-button', 'TextButton', '$materialLib/text_button.dart', tokens).updateFile();
111113
CardTemplate('Card', '$materialLib/card.dart', tokens).updateFile();
112114
CheckboxTemplate('Checkbox', '$materialLib/checkbox.dart', tokens).updateFile();
113-
ChipActionTemplate('ActionChip', '$materialLib/chip_action.dart', tokens).updateFile();
114-
ChipFilterTemplate('FilterChip', '$materialLib/chip_filter.dart', tokens).updateFile();
115-
ChipFilterTemplate('FilterChip', '$materialLib/chip_choice.dart', tokens).updateFile();
116-
ChipInputTemplate('InputChip', '$materialLib/chip_input.dart', tokens).updateFile();
115+
ChipActionTemplate('ActionChip', '$materialLib/action_chip.dart', tokens).updateFile();
116+
ChipFilterTemplate('FilterChip', '$materialLib/filter_chip.dart', tokens).updateFile();
117+
ChipFilterTemplate('FilterChip', '$materialLib/choice_chip.dart', tokens).updateFile();
118+
ChipInputTemplate('InputChip', '$materialLib/input_chip.dart', tokens).updateFile();
117119
DialogTemplate('Dialog', '$materialLib/dialog.dart', tokens).updateFile();
118120
FABTemplate('FAB', '$materialLib/floating_action_button.dart', tokens).updateFile();
119121
IconButtonTemplate('IconButton', '$materialLib/icon_button.dart', tokens).updateFile();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'template.dart';
6+
7+
class BannerTemplate extends TokenTemplate {
8+
const BannerTemplate(super.blockName, super.fileName, super.tokens);
9+
10+
@override
11+
String generate() => '''
12+
class _${blockName}DefaultsM3 extends MaterialBannerThemeData {
13+
const _${blockName}DefaultsM3(this.context)
14+
: super(elevation: ${elevation("md.comp.banner.container")});
15+
16+
final BuildContext context;
17+
18+
@override
19+
Color? get backgroundColor => ${componentColor("md.comp.banner.container")};
20+
21+
@override
22+
Color? get surfaceTintColor => ${color("md.comp.banner.container.surface-tint-layer.color")};
23+
24+
@override
25+
Color? get dividerColor => ${color("md.comp.banner.divider.color")};
26+
27+
@override
28+
TextStyle? get contentTextStyle => ${textStyle("md.comp.banner.supporting-text")};
29+
}
30+
''';
31+
}

packages/flutter/lib/src/material/banner.dart

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ class MaterialBanner extends StatefulWidget {
102102
this.elevation,
103103
this.leading,
104104
this.backgroundColor,
105+
this.surfaceTintColor,
106+
this.shadowColor,
107+
this.dividerColor,
105108
this.padding,
106109
this.leadingPadding,
107110
this.forceActionsBelow = false,
108111
this.overflowAlignment = OverflowBarAlignment.end,
109112
this.animation,
110-
this.onVisible
113+
this.onVisible,
111114
}) : assert(elevation == null || elevation >= 0.0),
112115
assert(content != null),
113116
assert(actions != null),
@@ -153,6 +156,29 @@ class MaterialBanner extends StatefulWidget {
153156
/// also `null`, [ColorScheme.surface] of [ThemeData.colorScheme] is used.
154157
final Color? backgroundColor;
155158

159+
/// The color used as an overlay on [backgroundColor] to indicate elevation.
160+
///
161+
/// If null, [MaterialBannerThemeData.surfaceTintColor] is used. If that
162+
/// is also null, the default value is [ColorScheme.surfaceTint].
163+
///
164+
/// See [Material.surfaceTintColor] for more details on how this
165+
/// overlay is applied.
166+
final Color? surfaceTintColor;
167+
168+
/// The color of the shadow below the [MaterialBanner].
169+
///
170+
/// If this property is null, then [MaterialBannerThemeData.shadowColor] of
171+
/// [ThemeData.bannerTheme] is used. If that is also null, the default value
172+
/// is null.
173+
final Color? shadowColor;
174+
175+
/// The color of the divider.
176+
///
177+
/// If this property is null, then [MaterialBannerThemeData.dividerColor] of
178+
/// [ThemeData.bannerTheme] is used. If that is also null, the default value
179+
/// is [ColorScheme.surfaceVariant].
180+
final Color? dividerColor;
181+
156182
/// The amount of space by which to inset the [content].
157183
///
158184
/// If the [actions] are below the [content], this defaults to
@@ -273,6 +299,7 @@ class _MaterialBannerState extends State<MaterialBanner> {
273299

274300
final ThemeData theme = Theme.of(context);
275301
final MaterialBannerThemeData bannerTheme = MaterialBannerTheme.of(context);
302+
final MaterialBannerThemeData defaults = theme.useMaterial3 ? _BannerDefaultsM3(context) : _BannerDefaultsM2(context);
276303

277304
final bool isSingleRow = widget.actions.length == 1 && !widget.forceActionsBelow;
278305
final EdgeInsetsGeometry padding = widget.padding ?? bannerTheme.padding ?? (isSingleRow
@@ -296,16 +323,26 @@ class _MaterialBannerState extends State<MaterialBanner> {
296323
final double elevation = widget.elevation ?? bannerTheme.elevation ?? 0.0;
297324
final Color backgroundColor = widget.backgroundColor
298325
?? bannerTheme.backgroundColor
299-
?? theme.colorScheme.surface;
326+
?? defaults.backgroundColor!;
327+
final Color? surfaceTintColor = widget.surfaceTintColor
328+
?? bannerTheme.surfaceTintColor
329+
?? defaults.surfaceTintColor;
330+
final Color? shadowColor = widget.shadowColor
331+
?? bannerTheme.shadowColor;
332+
final Color? dividerColor = widget.dividerColor
333+
?? bannerTheme.dividerColor
334+
?? defaults.dividerColor;
300335
final TextStyle? textStyle = widget.contentTextStyle
301336
?? bannerTheme.contentTextStyle
302-
?? theme.textTheme.bodyMedium;
337+
?? defaults.contentTextStyle;
303338

304339
Widget materialBanner = Container(
305340
margin: EdgeInsets.only(bottom: elevation > 0 ? 10.0 : 0.0),
306341
child: Material(
307342
elevation: elevation,
308343
color: backgroundColor,
344+
surfaceTintColor: surfaceTintColor,
345+
shadowColor: shadowColor,
309346
child: Column(
310347
mainAxisSize: MainAxisSize.min,
311348
children: <Widget>[
@@ -331,9 +368,8 @@ class _MaterialBannerState extends State<MaterialBanner> {
331368
),
332369
if (!isSingleRow)
333370
buttonBar,
334-
335371
if (elevation == 0)
336-
const Divider(height: 0),
372+
Divider(height: 0, color: dividerColor),
337373
],
338374
),
339375
),
@@ -394,3 +430,51 @@ class _MaterialBannerState extends State<MaterialBanner> {
394430
);
395431
}
396432
}
433+
434+
class _BannerDefaultsM2 extends MaterialBannerThemeData {
435+
_BannerDefaultsM2(this.context)
436+
: _theme = Theme.of(context),
437+
super(elevation: 0.0);
438+
439+
final BuildContext context;
440+
final ThemeData _theme;
441+
442+
@override
443+
Color? get backgroundColor => _theme.colorScheme.surface;
444+
445+
@override
446+
Color? get dividerColor => Theme.of(context).colorScheme.surfaceVariant;
447+
448+
@override
449+
TextStyle? get contentTextStyle => _theme.textTheme.bodyText2;
450+
}
451+
452+
// BEGIN GENERATED TOKEN PROPERTIES - Banner
453+
454+
// Do not edit by hand. The code between the "BEGIN GENERATED" and
455+
// "END GENERATED" comments are generated from data in the Material
456+
// Design token database by the script:
457+
// dev/tools/gen_defaults/bin/gen_defaults.dart.
458+
459+
// Token database version: v0_101
460+
461+
class _BannerDefaultsM3 extends MaterialBannerThemeData {
462+
const _BannerDefaultsM3(this.context)
463+
: super(elevation: 1.0);
464+
465+
final BuildContext context;
466+
467+
@override
468+
Color? get backgroundColor => Theme.of(context).colorScheme.surface;
469+
470+
@override
471+
Color? get surfaceTintColor => Theme.of(context).colorScheme.surfaceTint;
472+
473+
@override
474+
Color? get dividerColor => Theme.of(context).colorScheme.surfaceVariant;
475+
476+
@override
477+
TextStyle? get contentTextStyle => Theme.of(context).textTheme.bodyMedium;
478+
}
479+
480+
// END GENERATED TOKEN PROPERTIES - Banner

packages/flutter/lib/src/material/banner_theme.dart

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class MaterialBannerThemeData with Diagnosticable {
3535
/// [ThemeData.bannerTheme].
3636
const MaterialBannerThemeData({
3737
this.backgroundColor,
38+
this.surfaceTintColor,
39+
this.shadowColor,
40+
this.dividerColor,
3841
this.contentTextStyle,
3942
this.elevation,
4043
this.padding,
@@ -44,6 +47,15 @@ class MaterialBannerThemeData with Diagnosticable {
4447
/// The background color of a [MaterialBanner].
4548
final Color? backgroundColor;
4649

50+
/// Overrides the default value of [MaterialBanner.surfaceTintColor].
51+
final Color? surfaceTintColor;
52+
53+
/// Overrides the default value of [MaterialBanner.shadowColor].
54+
final Color? shadowColor;
55+
56+
/// Overrides the default value of [MaterialBanner.dividerColor].
57+
final Color? dividerColor;
58+
4759
/// Used to configure the [DefaultTextStyle] for the [MaterialBanner.content]
4860
/// widget.
4961
final TextStyle? contentTextStyle;
@@ -63,13 +75,19 @@ class MaterialBannerThemeData with Diagnosticable {
6375
/// new values.
6476
MaterialBannerThemeData copyWith({
6577
Color? backgroundColor,
78+
Color? surfaceTintColor,
79+
Color? shadowColor,
80+
Color? dividerColor,
6681
TextStyle? contentTextStyle,
6782
double? elevation,
6883
EdgeInsetsGeometry? padding,
6984
EdgeInsetsGeometry? leadingPadding,
7085
}) {
7186
return MaterialBannerThemeData(
7287
backgroundColor: backgroundColor ?? this.backgroundColor,
88+
surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
89+
shadowColor: shadowColor ?? this.shadowColor,
90+
dividerColor: dividerColor ?? this.dividerColor,
7391
contentTextStyle: contentTextStyle ?? this.contentTextStyle,
7492
elevation: elevation ?? this.elevation,
7593
padding: padding ?? this.padding,
@@ -86,6 +104,9 @@ class MaterialBannerThemeData with Diagnosticable {
86104
assert(t != null);
87105
return MaterialBannerThemeData(
88106
backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
107+
surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
108+
shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
109+
dividerColor: Color.lerp(a?.dividerColor, b?.dividerColor, t),
89110
contentTextStyle: TextStyle.lerp(a?.contentTextStyle, b?.contentTextStyle, t),
90111
elevation: lerpDouble(a?.elevation, b?.elevation, t),
91112
padding: EdgeInsetsGeometry.lerp(a?.padding, b?.padding, t),
@@ -96,6 +117,9 @@ class MaterialBannerThemeData with Diagnosticable {
96117
@override
97118
int get hashCode => Object.hash(
98119
backgroundColor,
120+
surfaceTintColor,
121+
shadowColor,
122+
dividerColor,
99123
contentTextStyle,
100124
elevation,
101125
padding,
@@ -111,19 +135,25 @@ class MaterialBannerThemeData with Diagnosticable {
111135
return false;
112136
}
113137
return other is MaterialBannerThemeData
114-
&& other.backgroundColor == backgroundColor
115-
&& other.contentTextStyle == contentTextStyle
116-
&& other.elevation == elevation
117-
&& other.padding == padding
118-
&& other.leadingPadding == leadingPadding;
138+
&& other.backgroundColor == backgroundColor
139+
&& other.surfaceTintColor == surfaceTintColor
140+
&& other.shadowColor == shadowColor
141+
&& other.dividerColor == dividerColor
142+
&& other.contentTextStyle == contentTextStyle
143+
&& other.elevation == elevation
144+
&& other.padding == padding
145+
&& other.leadingPadding == leadingPadding;
119146
}
120147

121148
@override
122149
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
123150
super.debugFillProperties(properties);
124151
properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
152+
properties.add(ColorProperty('surfaceTintColor', surfaceTintColor, defaultValue: null));
153+
properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
154+
properties.add(ColorProperty('dividerColor', dividerColor, defaultValue: null));
125155
properties.add(DiagnosticsProperty<TextStyle>('contentTextStyle', contentTextStyle, defaultValue: null));
126-
properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
156+
properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
127157
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding, defaultValue: null));
128158
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('leadingPadding', leadingPadding, defaultValue: null));
129159
}

0 commit comments

Comments
 (0)