Unverified Commit dc80d499 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Added backwardsCompatibility flag to AppBarTheme (#72472)

parent 23f5fbc6
...@@ -192,7 +192,7 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -192,7 +192,7 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
this.bottomOpacity = 1.0, this.bottomOpacity = 1.0,
this.toolbarHeight, this.toolbarHeight,
this.leadingWidth, this.leadingWidth,
this.backwardsCompatibility = true, this.backwardsCompatibility,
this.toolbarTextStyle, this.toolbarTextStyle,
this.titleTextStyle, this.titleTextStyle,
this.systemOverlayStyle, this.systemOverlayStyle,
...@@ -201,7 +201,6 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -201,7 +201,6 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
assert(primary != null), assert(primary != null),
assert(toolbarOpacity != null), assert(toolbarOpacity != null),
assert(bottomOpacity != null), assert(bottomOpacity != null),
assert(backwardsCompatibility != null),
preferredSize = Size.fromHeight(toolbarHeight ?? kToolbarHeight + (bottom?.preferredSize.height ?? 0.0)), preferredSize = Size.fromHeight(toolbarHeight ?? kToolbarHeight + (bottom?.preferredSize.height ?? 0.0)),
super(key: key); super(key: key);
...@@ -609,16 +608,18 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -609,16 +608,18 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
/// [iconTheme], [actionsIconTheme] properties, and the original use of /// [iconTheme], [actionsIconTheme] properties, and the original use of
/// the [textTheme] and [brightness] properties. /// the [textTheme] and [brightness] properties.
/// ///
/// This property is true by default. /// If this property is null, then [AppBarTheme.backwardsCompatibility] of
/// [ThemeData.appBarTheme] is used. If that is also null, the default
/// value is true.
/// ///
/// This is a temporary property. When setting it to false is no /// This is a temporary property. When setting it to false is no
/// longer considereed a breaking change, it will be depreacted and /// longer considered a breaking change, it will be depreacted and
/// its default value will be changed to false. App developers are /// its default value will be changed to false. App developers are
/// encouraged to opt into the new features by setting it to false /// encouraged to opt into the new features by setting it to false
/// and using the [foregroundColor] and [systemOverlayStyle] /// and using the [foregroundColor] and [systemOverlayStyle]
/// properties as needed. /// properties as needed.
/// {@endtemplate} /// {@endtemplate}
final bool backwardsCompatibility; final bool? backwardsCompatibility;
/// {@template flutter.material.appbar.toolbarTextStyle} /// {@template flutter.material.appbar.toolbarTextStyle}
/// The default text style for the AppBar's [leading], and /// The default text style for the AppBar's [leading], and
...@@ -724,8 +725,9 @@ class _AppBarState extends State<AppBar> { ...@@ -724,8 +725,9 @@ class _AppBarState extends State<AppBar> {
final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog; final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;
final double toolbarHeight = widget.toolbarHeight ?? kToolbarHeight; final double toolbarHeight = widget.toolbarHeight ?? kToolbarHeight;
final bool backwardsCompatibility = widget.backwardsCompatibility ?? appBarTheme.backwardsCompatibility ?? true;
final Color backgroundColor = widget.backwardsCompatibility final Color backgroundColor = backwardsCompatibility
? widget.backgroundColor ? widget.backgroundColor
?? appBarTheme.color ?? appBarTheme.color
?? theme.primaryColor ?? theme.primaryColor
...@@ -737,7 +739,7 @@ class _AppBarState extends State<AppBar> { ...@@ -737,7 +739,7 @@ class _AppBarState extends State<AppBar> {
?? appBarTheme.foregroundColor ?? appBarTheme.foregroundColor
?? (colorScheme.brightness == Brightness.dark ? colorScheme.onSurface : colorScheme.onPrimary); ?? (colorScheme.brightness == Brightness.dark ? colorScheme.onSurface : colorScheme.onPrimary);
IconThemeData overallIconTheme = widget.backwardsCompatibility IconThemeData overallIconTheme = backwardsCompatibility
? widget.iconTheme ? widget.iconTheme
?? appBarTheme.iconTheme ?? appBarTheme.iconTheme
?? theme.primaryIconTheme ?? theme.primaryIconTheme
...@@ -749,7 +751,7 @@ class _AppBarState extends State<AppBar> { ...@@ -749,7 +751,7 @@ class _AppBarState extends State<AppBar> {
?? appBarTheme.actionsIconTheme ?? appBarTheme.actionsIconTheme
?? overallIconTheme; ?? overallIconTheme;
TextStyle? toolbarTextStyle = widget.backwardsCompatibility TextStyle? toolbarTextStyle = backwardsCompatibility
? widget.textTheme?.bodyText2 ? widget.textTheme?.bodyText2
?? appBarTheme.textTheme?.bodyText2 ?? appBarTheme.textTheme?.bodyText2
?? theme.primaryTextTheme.bodyText2 ?? theme.primaryTextTheme.bodyText2
...@@ -757,7 +759,7 @@ class _AppBarState extends State<AppBar> { ...@@ -757,7 +759,7 @@ class _AppBarState extends State<AppBar> {
?? appBarTheme.toolbarTextStyle ?? appBarTheme.toolbarTextStyle
?? theme.textTheme.bodyText2?.copyWith(color: foregroundColor); ?? theme.textTheme.bodyText2?.copyWith(color: foregroundColor);
TextStyle? titleTextStyle = widget.backwardsCompatibility TextStyle? titleTextStyle = backwardsCompatibility
? widget.textTheme?.headline6 ? widget.textTheme?.headline6
?? appBarTheme.textTheme?.headline6 ?? appBarTheme.textTheme?.headline6
?? theme.primaryTextTheme.headline6 ?? theme.primaryTextTheme.headline6
...@@ -951,7 +953,7 @@ class _AppBarState extends State<AppBar> { ...@@ -951,7 +953,7 @@ class _AppBarState extends State<AppBar> {
} }
final Brightness overlayStyleBrightness = widget.brightness ?? appBarTheme.brightness ?? colorScheme.brightness; final Brightness overlayStyleBrightness = widget.brightness ?? appBarTheme.brightness ?? colorScheme.brightness;
final SystemUiOverlayStyle overlayStyle = widget.backwardsCompatibility final SystemUiOverlayStyle overlayStyle = backwardsCompatibility
? (overlayStyleBrightness == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark) ? (overlayStyleBrightness == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark)
: widget.systemOverlayStyle : widget.systemOverlayStyle
?? appBarTheme.systemOverlayStyle ?? appBarTheme.systemOverlayStyle
......
...@@ -42,6 +42,7 @@ class AppBarTheme with Diagnosticable { ...@@ -42,6 +42,7 @@ class AppBarTheme with Diagnosticable {
this.toolbarTextStyle, this.toolbarTextStyle,
this.titleTextStyle, this.titleTextStyle,
this.systemOverlayStyle, this.systemOverlayStyle,
this.backwardsCompatibility,
}); });
/// This property is obsolete, please use [systemOverlayStyle] instead. /// This property is obsolete, please use [systemOverlayStyle] instead.
...@@ -161,6 +162,10 @@ class AppBarTheme with Diagnosticable { ...@@ -161,6 +162,10 @@ class AppBarTheme with Diagnosticable {
/// property in all descendant [AppBar] widgets. /// property in all descendant [AppBar] widgets.
final SystemUiOverlayStyle? systemOverlayStyle; final SystemUiOverlayStyle? systemOverlayStyle;
/// Overrides the default value of [AppBar.backwardsCompatibility]
/// property in all descendant [AppBar] widgets.
final bool? backwardsCompatibility;
/// Creates a copy of this object with the given fields replaced with the /// Creates a copy of this object with the given fields replaced with the
/// new values. /// new values.
AppBarTheme copyWith({ AppBarTheme copyWith({
...@@ -178,6 +183,7 @@ class AppBarTheme with Diagnosticable { ...@@ -178,6 +183,7 @@ class AppBarTheme with Diagnosticable {
TextStyle? toolbarTextStyle, TextStyle? toolbarTextStyle,
TextStyle? titleTextStyle, TextStyle? titleTextStyle,
SystemUiOverlayStyle? systemOverlayStyle, SystemUiOverlayStyle? systemOverlayStyle,
bool? backwardsCompatibility,
}) { }) {
return AppBarTheme( return AppBarTheme(
brightness: brightness ?? this.brightness, brightness: brightness ?? this.brightness,
...@@ -194,6 +200,7 @@ class AppBarTheme with Diagnosticable { ...@@ -194,6 +200,7 @@ class AppBarTheme with Diagnosticable {
toolbarTextStyle: toolbarTextStyle ?? this.toolbarTextStyle, toolbarTextStyle: toolbarTextStyle ?? this.toolbarTextStyle,
titleTextStyle: titleTextStyle ?? this.titleTextStyle, titleTextStyle: titleTextStyle ?? this.titleTextStyle,
systemOverlayStyle: systemOverlayStyle ?? this.systemOverlayStyle, systemOverlayStyle: systemOverlayStyle ?? this.systemOverlayStyle,
backwardsCompatibility: backwardsCompatibility ?? this.backwardsCompatibility,
); );
} }
...@@ -224,6 +231,7 @@ class AppBarTheme with Diagnosticable { ...@@ -224,6 +231,7 @@ class AppBarTheme with Diagnosticable {
toolbarTextStyle: TextStyle.lerp(a?.toolbarTextStyle, b?.toolbarTextStyle, t), toolbarTextStyle: TextStyle.lerp(a?.toolbarTextStyle, b?.toolbarTextStyle, t),
titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t), titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t),
systemOverlayStyle: t < 0.5 ? a?.systemOverlayStyle : b?.systemOverlayStyle, systemOverlayStyle: t < 0.5 ? a?.systemOverlayStyle : b?.systemOverlayStyle,
backwardsCompatibility: t < 0.5 ? a?.backwardsCompatibility : b?.backwardsCompatibility,
); );
} }
...@@ -244,6 +252,7 @@ class AppBarTheme with Diagnosticable { ...@@ -244,6 +252,7 @@ class AppBarTheme with Diagnosticable {
toolbarTextStyle, toolbarTextStyle,
titleTextStyle, titleTextStyle,
systemOverlayStyle, systemOverlayStyle,
backwardsCompatibility,
); );
} }
...@@ -267,7 +276,8 @@ class AppBarTheme with Diagnosticable { ...@@ -267,7 +276,8 @@ class AppBarTheme with Diagnosticable {
&& other.titleSpacing == titleSpacing && other.titleSpacing == titleSpacing
&& other.toolbarTextStyle == toolbarTextStyle && other.toolbarTextStyle == toolbarTextStyle
&& other.titleTextStyle == titleTextStyle && other.titleTextStyle == titleTextStyle
&& other.systemOverlayStyle == systemOverlayStyle; && other.systemOverlayStyle == systemOverlayStyle
&& other.backwardsCompatibility == backwardsCompatibility;
} }
@override @override
...@@ -286,5 +296,6 @@ class AppBarTheme with Diagnosticable { ...@@ -286,5 +296,6 @@ class AppBarTheme with Diagnosticable {
properties.add(DiagnosticsProperty<double>('titleSpacing', titleSpacing, defaultValue: null)); properties.add(DiagnosticsProperty<double>('titleSpacing', titleSpacing, defaultValue: null));
properties.add(DiagnosticsProperty<TextStyle>('toolbarTextStyle', toolbarTextStyle, defaultValue: null)); properties.add(DiagnosticsProperty<TextStyle>('toolbarTextStyle', toolbarTextStyle, defaultValue: null));
properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null)); properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
properties.add(DiagnosticsProperty<bool>('backwardsCompatibility', backwardsCompatibility, defaultValue: null));
} }
} }
...@@ -2273,4 +2273,90 @@ void main() { ...@@ -2273,4 +2273,90 @@ void main() {
final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
expect(navToolBar.middleSpacing, NavigationToolbar.kMiddleSpacing); expect(navToolBar.middleSpacing, NavigationToolbar.kMiddleSpacing);
}); });
testWidgets('AppBar foregroundColor and backgroundColor', (WidgetTester tester) async {
const Color foregroundColor = Color(0xff00ff00);
const Color backgroundColor = Color(0xff00ffff);
final Key leadingIconKey = UniqueKey();
final Key actionIconKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
leading: Icon(Icons.add_circle, key: leadingIconKey),
title: const Text('title'),
actions: <Widget>[Icon(Icons.add_circle, key: actionIconKey), const Text('action')],
),
),
),
);
final Material appBarMaterial = tester.widget<Material>(
find.descendant(
of: find.byType(AppBar),
matching: find.byType(Material),
),
);
expect(appBarMaterial.color, backgroundColor);
final TextStyle titleTextStyle = tester.widget<DefaultTextStyle>(
find.ancestor(of: find.text('title'), matching: find.byType(DefaultTextStyle)).first,
).style;
expect(titleTextStyle.color, foregroundColor);
final IconThemeData leadingIconTheme = tester.widget<IconTheme>(
find.ancestor(of: find.byKey(leadingIconKey), matching: find.byType(IconTheme)).first,
).data;
expect(leadingIconTheme.color, foregroundColor);
final IconThemeData actionIconTheme = tester.widget<IconTheme>(
find.ancestor(of: find.byKey(actionIconKey), matching: find.byType(IconTheme)).first,
).data;
expect(actionIconTheme.color, foregroundColor);
});
testWidgets('AppBarTheme.backwardsCompatibility', (WidgetTester tester) async {
const Color foregroundColor = Color(0xff00ff00);
final Key leadingIconKey = UniqueKey();
final Key actionIconKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
theme: ThemeData.light().copyWith(
appBarTheme: const AppBarTheme(
backwardsCompatibility: false,
),
),
home: Scaffold(
appBar: AppBar(
foregroundColor: foregroundColor, // only applies if backwardsCompatibility is false
leading: Icon(Icons.add_circle, key: leadingIconKey),
title: const Text('title'),
actions: <Widget>[Icon(Icons.add_circle, key: actionIconKey), const Text('action')],
),
),
),
);
final TextStyle titleTextStyle = tester.widget<DefaultTextStyle>(
find.ancestor(of: find.text('title'), matching: find.byType(DefaultTextStyle)).first,
).style;
expect(titleTextStyle.color, foregroundColor);
final IconThemeData leadingIconTheme = tester.widget<IconTheme>(
find.ancestor(of: find.byKey(leadingIconKey), matching: find.byType(IconTheme)).first,
).data;
expect(leadingIconTheme.color, foregroundColor);
final IconThemeData actionIconTheme = tester.widget<IconTheme>(
find.ancestor(of: find.byKey(actionIconKey), matching: find.byType(IconTheme)).first,
).data;
expect(actionIconTheme.color, foregroundColor);
});
} }
...@@ -440,6 +440,7 @@ void main() { ...@@ -440,6 +440,7 @@ void main() {
testWidgets('AppBarTheme implements debugFillProperties', (WidgetTester tester) async { testWidgets('AppBarTheme implements debugFillProperties', (WidgetTester tester) async {
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder(); final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
const AppBarTheme( const AppBarTheme(
backwardsCompatibility: false,
brightness: Brightness.dark, brightness: Brightness.dark,
color: Color(0xff000001), color: Color(0xff000001),
elevation: 8.0, elevation: 8.0,
...@@ -460,6 +461,7 @@ void main() { ...@@ -460,6 +461,7 @@ void main() {
'shadowColor: Color(0xff000002)', 'shadowColor: Color(0xff000002)',
'centerTitle: true', 'centerTitle: true',
'titleSpacing: 40.0', 'titleSpacing: 40.0',
'backwardsCompatibility: false',
]); ]);
// On the web, Dart doubles and ints are backed by the same kind of object because // On the web, Dart doubles and ints are backed by the same kind of object because
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment