Unverified Commit 5140376e authored by Michael Debertol's avatar Michael Debertol Committed by GitHub

Calculate the system overlay style based on the AppBar background color (#75091)

parent ed5b8feb
......@@ -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<SystemUiOverlayStyle>` widget, which causes
......@@ -710,6 +712,10 @@ class _AppBarState extends State<AppBar> {
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<AppBar> {
);
}
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,
......
......@@ -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;
......
......@@ -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);
......
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