From 5140376ee34fc030ba1c5f1f1e637ed6c702547d Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Tue, 13 Apr 2021 22:54:04 +0200 Subject: [PATCH] Calculate the system overlay style based on the AppBar background color (#75091) --- .../flutter/lib/src/material/app_bar.dart | 21 ++++++--- .../flutter/test/material/app_bar_test.dart | 44 ++++++++++++++++++- .../test/material/app_bar_theme_test.dart | 6 +-- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 1397c6e31d..ef48fbc0ad 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -660,9 +660,11 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { /// {@template flutter.material.appbar.systemOverlayStyle} /// Specifies the style to use for the system overlays that overlap the AppBar. /// - /// If this property is null, then [SystemUiOverlayStyle.light] is used if the - /// overall theme is dark, [SystemUiOverlayStyle.dark] otherwise. Theme brightness - /// is defined by [ColorScheme.brightness] for [ThemeData.colorScheme]. + /// This property is only used if [backwardsCompatibility] is set to false. + /// + /// If this property is null, then [AppBarTheme.systemOverlayStyle] of + /// [ThemeData.appBarTheme] is used. If that is also null, an appropriate + /// [SystemUiOverlayStyle] is calculated based on the [backgroundColor]. /// /// The AppBar's descendants are built within a /// `AnnotatedRegion` widget, which causes @@ -710,6 +712,10 @@ class _AppBarState extends State { Scaffold.of(context).openEndDrawer(); } + SystemUiOverlayStyle _systemOverlayStyleForBrightness(Brightness brightness) { + return brightness == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark; + } + @override Widget build(BuildContext context) { assert(!widget.primary || debugCheckHasMediaQuery(context)); @@ -953,12 +959,15 @@ class _AppBarState extends State { ); } - final Brightness overlayStyleBrightness = widget.brightness ?? appBarTheme.brightness ?? colorScheme.brightness; final SystemUiOverlayStyle overlayStyle = backwardsCompatibility - ? (overlayStyleBrightness == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark) + ? _systemOverlayStyleForBrightness( + widget.brightness + ?? appBarTheme.brightness + ?? theme.primaryColorBrightness, + ) : widget.systemOverlayStyle ?? appBarTheme.systemOverlayStyle - ?? (colorScheme.brightness == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark); + ?? _systemOverlayStyleForBrightness(ThemeData.estimateBrightnessForColor(backgroundColor)); return Semantics( container: true, diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index d54a4ead1f..1ff0b2e96f 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -1953,7 +1953,7 @@ void main() { }); testWidgets('AppBar draws a dark system bar for a light background', (WidgetTester tester) async { - final ThemeData lightTheme = ThemeData(primaryColor: Colors.white); + final ThemeData lightTheme = ThemeData(primarySwatch: Colors.lightBlue); await tester.pumpWidget( MaterialApp( theme: lightTheme, @@ -1974,6 +1974,48 @@ void main() { )); }); + testWidgets('AppBar draws a light system bar for a light theme with a dark background', (WidgetTester tester) async { + final ThemeData lightTheme = ThemeData(primarySwatch: Colors.deepOrange); + await tester.pumpWidget(MaterialApp( + theme: lightTheme, + home: Scaffold( + appBar: AppBar( + backwardsCompatibility: false, + title: const Text('test') + ), + ), + )); + + expect(lightTheme.primaryColorBrightness, Brightness.dark); + expect(lightTheme.colorScheme.brightness, Brightness.light); + expect(SystemChrome.latestStyle, const SystemUiOverlayStyle( + statusBarBrightness: Brightness.dark, + statusBarIconBrightness: Brightness.light, + )); + }); + + testWidgets('AppBar draws a dark system bar for a dark theme with a light background', (WidgetTester tester) async { + final ThemeData darkTheme = ThemeData(brightness: Brightness.dark, cardColor: Colors.white); + await tester.pumpWidget( + MaterialApp( + theme: darkTheme, + home: Scaffold( + appBar: AppBar( + backwardsCompatibility: false, + title: const Text('test') + ), + ), + ), + ); + + expect(darkTheme.primaryColorBrightness, Brightness.dark); + expect(darkTheme.colorScheme.brightness, Brightness.dark); + expect(SystemChrome.latestStyle, const SystemUiOverlayStyle( + statusBarBrightness: Brightness.light, + statusBarIconBrightness: Brightness.dark, + )); + }); + testWidgets('Changing SliverAppBar snap from true to false', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/17598 const double appBarHeight = 256.0; diff --git a/packages/flutter/test/material/app_bar_theme_test.dart b/packages/flutter/test/material/app_bar_theme_test.dart index 6a77705eb5..d371bd428f 100644 --- a/packages/flutter/test/material/app_bar_theme_test.dart +++ b/packages/flutter/test/material/app_bar_theme_test.dart @@ -30,7 +30,7 @@ void main() { final RichText actionIconText = _getAppBarIconRichText(tester); final DefaultTextStyle text = _getAppBarText(tester); - expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.dark.statusBarBrightness); + expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.light.statusBarBrightness); expect(widget.color, Colors.blue); expect(widget.elevation, 4.0); expect(widget.shadowColor, Colors.black); @@ -249,7 +249,7 @@ void main() { // - background color: ColorScheme.primary // - foreground color: ColorScheme.onPrimary // - actions text: style bodyText2, foreground color - // - status bar brightness: dark (based on color scheme brightness) + // - status bar brightness: light (based on color scheme brightness) { await tester.pumpWidget(buildFrame(ThemeData.from(colorScheme: const ColorScheme.light()))); @@ -259,7 +259,7 @@ void main() { final RichText actionIconText = _getAppBarIconRichText(tester); final DefaultTextStyle text = _getAppBarText(tester); - expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.dark.statusBarBrightness); + expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.light.statusBarBrightness); expect(widget.color, theme.colorScheme.primary); expect(widget.elevation, 4.0); expect(widget.shadowColor, Colors.black);