Skip to content

Commit dc80d49

Browse files
authored
Added backwardsCompatibility flag to AppBarTheme (flutter#72472)
1 parent 23f5fbc commit dc80d49

File tree

4 files changed

+112
-11
lines changed

4 files changed

+112
-11
lines changed

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
192192
this.bottomOpacity = 1.0,
193193
this.toolbarHeight,
194194
this.leadingWidth,
195-
this.backwardsCompatibility = true,
195+
this.backwardsCompatibility,
196196
this.toolbarTextStyle,
197197
this.titleTextStyle,
198198
this.systemOverlayStyle,
@@ -201,7 +201,6 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
201201
assert(primary != null),
202202
assert(toolbarOpacity != null),
203203
assert(bottomOpacity != null),
204-
assert(backwardsCompatibility != null),
205204
preferredSize = Size.fromHeight(toolbarHeight ?? kToolbarHeight + (bottom?.preferredSize.height ?? 0.0)),
206205
super(key: key);
207206

@@ -609,16 +608,18 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
609608
/// [iconTheme], [actionsIconTheme] properties, and the original use of
610609
/// the [textTheme] and [brightness] properties.
611610
///
612-
/// This property is true by default.
611+
/// If this property is null, then [AppBarTheme.backwardsCompatibility] of
612+
/// [ThemeData.appBarTheme] is used. If that is also null, the default
613+
/// value is true.
613614
///
614615
/// This is a temporary property. When setting it to false is no
615-
/// longer considereed a breaking change, it will be depreacted and
616+
/// longer considered a breaking change, it will be depreacted and
616617
/// its default value will be changed to false. App developers are
617618
/// encouraged to opt into the new features by setting it to false
618619
/// and using the [foregroundColor] and [systemOverlayStyle]
619620
/// properties as needed.
620621
/// {@endtemplate}
621-
final bool backwardsCompatibility;
622+
final bool? backwardsCompatibility;
622623

623624
/// {@template flutter.material.appbar.toolbarTextStyle}
624625
/// The default text style for the AppBar's [leading], and
@@ -724,8 +725,9 @@ class _AppBarState extends State<AppBar> {
724725
final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;
725726

726727
final double toolbarHeight = widget.toolbarHeight ?? kToolbarHeight;
728+
final bool backwardsCompatibility = widget.backwardsCompatibility ?? appBarTheme.backwardsCompatibility ?? true;
727729

728-
final Color backgroundColor = widget.backwardsCompatibility
730+
final Color backgroundColor = backwardsCompatibility
729731
? widget.backgroundColor
730732
?? appBarTheme.color
731733
?? theme.primaryColor
@@ -737,7 +739,7 @@ class _AppBarState extends State<AppBar> {
737739
?? appBarTheme.foregroundColor
738740
?? (colorScheme.brightness == Brightness.dark ? colorScheme.onSurface : colorScheme.onPrimary);
739741

740-
IconThemeData overallIconTheme = widget.backwardsCompatibility
742+
IconThemeData overallIconTheme = backwardsCompatibility
741743
? widget.iconTheme
742744
?? appBarTheme.iconTheme
743745
?? theme.primaryIconTheme
@@ -749,15 +751,15 @@ class _AppBarState extends State<AppBar> {
749751
?? appBarTheme.actionsIconTheme
750752
?? overallIconTheme;
751753

752-
TextStyle? toolbarTextStyle = widget.backwardsCompatibility
754+
TextStyle? toolbarTextStyle = backwardsCompatibility
753755
? widget.textTheme?.bodyText2
754756
?? appBarTheme.textTheme?.bodyText2
755757
?? theme.primaryTextTheme.bodyText2
756758
: widget.toolbarTextStyle
757759
?? appBarTheme.toolbarTextStyle
758760
?? theme.textTheme.bodyText2?.copyWith(color: foregroundColor);
759761

760-
TextStyle? titleTextStyle = widget.backwardsCompatibility
762+
TextStyle? titleTextStyle = backwardsCompatibility
761763
? widget.textTheme?.headline6
762764
?? appBarTheme.textTheme?.headline6
763765
?? theme.primaryTextTheme.headline6
@@ -951,7 +953,7 @@ class _AppBarState extends State<AppBar> {
951953
}
952954

953955
final Brightness overlayStyleBrightness = widget.brightness ?? appBarTheme.brightness ?? colorScheme.brightness;
954-
final SystemUiOverlayStyle overlayStyle = widget.backwardsCompatibility
956+
final SystemUiOverlayStyle overlayStyle = backwardsCompatibility
955957
? (overlayStyleBrightness == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark)
956958
: widget.systemOverlayStyle
957959
?? appBarTheme.systemOverlayStyle

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class AppBarTheme with Diagnosticable {
4242
this.toolbarTextStyle,
4343
this.titleTextStyle,
4444
this.systemOverlayStyle,
45+
this.backwardsCompatibility,
4546
});
4647

4748
/// This property is obsolete, please use [systemOverlayStyle] instead.
@@ -161,6 +162,10 @@ class AppBarTheme with Diagnosticable {
161162
/// property in all descendant [AppBar] widgets.
162163
final SystemUiOverlayStyle? systemOverlayStyle;
163164

165+
/// Overrides the default value of [AppBar.backwardsCompatibility]
166+
/// property in all descendant [AppBar] widgets.
167+
final bool? backwardsCompatibility;
168+
164169
/// Creates a copy of this object with the given fields replaced with the
165170
/// new values.
166171
AppBarTheme copyWith({
@@ -178,6 +183,7 @@ class AppBarTheme with Diagnosticable {
178183
TextStyle? toolbarTextStyle,
179184
TextStyle? titleTextStyle,
180185
SystemUiOverlayStyle? systemOverlayStyle,
186+
bool? backwardsCompatibility,
181187
}) {
182188
return AppBarTheme(
183189
brightness: brightness ?? this.brightness,
@@ -194,6 +200,7 @@ class AppBarTheme with Diagnosticable {
194200
toolbarTextStyle: toolbarTextStyle ?? this.toolbarTextStyle,
195201
titleTextStyle: titleTextStyle ?? this.titleTextStyle,
196202
systemOverlayStyle: systemOverlayStyle ?? this.systemOverlayStyle,
203+
backwardsCompatibility: backwardsCompatibility ?? this.backwardsCompatibility,
197204
);
198205
}
199206

@@ -224,6 +231,7 @@ class AppBarTheme with Diagnosticable {
224231
toolbarTextStyle: TextStyle.lerp(a?.toolbarTextStyle, b?.toolbarTextStyle, t),
225232
titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t),
226233
systemOverlayStyle: t < 0.5 ? a?.systemOverlayStyle : b?.systemOverlayStyle,
234+
backwardsCompatibility: t < 0.5 ? a?.backwardsCompatibility : b?.backwardsCompatibility,
227235
);
228236
}
229237

@@ -244,6 +252,7 @@ class AppBarTheme with Diagnosticable {
244252
toolbarTextStyle,
245253
titleTextStyle,
246254
systemOverlayStyle,
255+
backwardsCompatibility,
247256
);
248257
}
249258

@@ -267,7 +276,8 @@ class AppBarTheme with Diagnosticable {
267276
&& other.titleSpacing == titleSpacing
268277
&& other.toolbarTextStyle == toolbarTextStyle
269278
&& other.titleTextStyle == titleTextStyle
270-
&& other.systemOverlayStyle == systemOverlayStyle;
279+
&& other.systemOverlayStyle == systemOverlayStyle
280+
&& other.backwardsCompatibility == backwardsCompatibility;
271281
}
272282

273283
@override
@@ -286,5 +296,6 @@ class AppBarTheme with Diagnosticable {
286296
properties.add(DiagnosticsProperty<double>('titleSpacing', titleSpacing, defaultValue: null));
287297
properties.add(DiagnosticsProperty<TextStyle>('toolbarTextStyle', toolbarTextStyle, defaultValue: null));
288298
properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
299+
properties.add(DiagnosticsProperty<bool>('backwardsCompatibility', backwardsCompatibility, defaultValue: null));
289300
}
290301
}

packages/flutter/test/material/app_bar_test.dart

+86
Original file line numberDiff line numberDiff line change
@@ -2273,4 +2273,90 @@ void main() {
22732273
final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
22742274
expect(navToolBar.middleSpacing, NavigationToolbar.kMiddleSpacing);
22752275
});
2276+
2277+
testWidgets('AppBar foregroundColor and backgroundColor', (WidgetTester tester) async {
2278+
const Color foregroundColor = Color(0xff00ff00);
2279+
const Color backgroundColor = Color(0xff00ffff);
2280+
final Key leadingIconKey = UniqueKey();
2281+
final Key actionIconKey = UniqueKey();
2282+
2283+
await tester.pumpWidget(
2284+
MaterialApp(
2285+
home: Scaffold(
2286+
appBar: AppBar(
2287+
backwardsCompatibility: false,
2288+
foregroundColor: foregroundColor,
2289+
backgroundColor: backgroundColor,
2290+
leading: Icon(Icons.add_circle, key: leadingIconKey),
2291+
title: const Text('title'),
2292+
actions: <Widget>[Icon(Icons.add_circle, key: actionIconKey), const Text('action')],
2293+
),
2294+
),
2295+
),
2296+
);
2297+
2298+
final Material appBarMaterial = tester.widget<Material>(
2299+
find.descendant(
2300+
of: find.byType(AppBar),
2301+
matching: find.byType(Material),
2302+
),
2303+
);
2304+
expect(appBarMaterial.color, backgroundColor);
2305+
2306+
final TextStyle titleTextStyle = tester.widget<DefaultTextStyle>(
2307+
find.ancestor(of: find.text('title'), matching: find.byType(DefaultTextStyle)).first,
2308+
).style;
2309+
expect(titleTextStyle.color, foregroundColor);
2310+
2311+
final IconThemeData leadingIconTheme = tester.widget<IconTheme>(
2312+
find.ancestor(of: find.byKey(leadingIconKey), matching: find.byType(IconTheme)).first,
2313+
).data;
2314+
expect(leadingIconTheme.color, foregroundColor);
2315+
2316+
final IconThemeData actionIconTheme = tester.widget<IconTheme>(
2317+
find.ancestor(of: find.byKey(actionIconKey), matching: find.byType(IconTheme)).first,
2318+
).data;
2319+
expect(actionIconTheme.color, foregroundColor);
2320+
});
2321+
2322+
2323+
testWidgets('AppBarTheme.backwardsCompatibility', (WidgetTester tester) async {
2324+
const Color foregroundColor = Color(0xff00ff00);
2325+
final Key leadingIconKey = UniqueKey();
2326+
final Key actionIconKey = UniqueKey();
2327+
2328+
await tester.pumpWidget(
2329+
MaterialApp(
2330+
theme: ThemeData.light().copyWith(
2331+
appBarTheme: const AppBarTheme(
2332+
backwardsCompatibility: false,
2333+
),
2334+
),
2335+
home: Scaffold(
2336+
appBar: AppBar(
2337+
foregroundColor: foregroundColor, // only applies if backwardsCompatibility is false
2338+
leading: Icon(Icons.add_circle, key: leadingIconKey),
2339+
title: const Text('title'),
2340+
actions: <Widget>[Icon(Icons.add_circle, key: actionIconKey), const Text('action')],
2341+
),
2342+
),
2343+
),
2344+
);
2345+
2346+
final TextStyle titleTextStyle = tester.widget<DefaultTextStyle>(
2347+
find.ancestor(of: find.text('title'), matching: find.byType(DefaultTextStyle)).first,
2348+
).style;
2349+
expect(titleTextStyle.color, foregroundColor);
2350+
2351+
final IconThemeData leadingIconTheme = tester.widget<IconTheme>(
2352+
find.ancestor(of: find.byKey(leadingIconKey), matching: find.byType(IconTheme)).first,
2353+
).data;
2354+
expect(leadingIconTheme.color, foregroundColor);
2355+
2356+
final IconThemeData actionIconTheme = tester.widget<IconTheme>(
2357+
find.ancestor(of: find.byKey(actionIconKey), matching: find.byType(IconTheme)).first,
2358+
).data;
2359+
expect(actionIconTheme.color, foregroundColor);
2360+
});
2361+
22762362
}

packages/flutter/test/material/app_bar_theme_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ void main() {
440440
testWidgets('AppBarTheme implements debugFillProperties', (WidgetTester tester) async {
441441
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
442442
const AppBarTheme(
443+
backwardsCompatibility: false,
443444
brightness: Brightness.dark,
444445
color: Color(0xff000001),
445446
elevation: 8.0,
@@ -460,6 +461,7 @@ void main() {
460461
'shadowColor: Color(0xff000002)',
461462
'centerTitle: true',
462463
'titleSpacing: 40.0',
464+
'backwardsCompatibility: false',
463465
]);
464466

465467
// On the web, Dart doubles and ints are backed by the same kind of object because

0 commit comments

Comments
 (0)