diff --git a/packages/flutter/lib/src/cupertino/nav_bar.dart b/packages/flutter/lib/src/cupertino/nav_bar.dart index 91cac6cb24..d5776ee8a0 100644 --- a/packages/flutter/lib/src/cupertino/nav_bar.dart +++ b/packages/flutter/lib/src/cupertino/nav_bar.dart @@ -87,15 +87,24 @@ class _HeroTag { Widget _wrapWithBackground({ Border border, Color backgroundColor, + Brightness brightness, Widget child, bool updateSystemUiOverlay = true, }) { Widget result = child; if (updateSystemUiOverlay) { - final bool darkBackground = backgroundColor.computeLuminance() < 0.179; - final SystemUiOverlayStyle overlayStyle = darkBackground - ? SystemUiOverlayStyle.light - : SystemUiOverlayStyle.dark; + final bool isDark = backgroundColor.computeLuminance() < 0.179; + final Brightness newBrightness = brightness ?? (isDark ? Brightness.dark : Brightness.light); + SystemUiOverlayStyle overlayStyle; + switch (newBrightness) { + case Brightness.dark: + overlayStyle = SystemUiOverlayStyle.light; + break; + case Brightness.light: + default: + overlayStyle = SystemUiOverlayStyle.dark; + break; + } result = AnnotatedRegion( value: overlayStyle, sized: true, @@ -206,6 +215,7 @@ class CupertinoNavigationBar extends StatefulWidget implements ObstructingPrefer this.trailing, this.border = _kDefaultNavBarBorder, this.backgroundColor, + this.brightness, this.padding, this.actionsForegroundColor, this.transitionBetweenRoutes = true, @@ -301,6 +311,18 @@ class CupertinoNavigationBar extends StatefulWidget implements ObstructingPrefer /// {@endtemplate} final Color backgroundColor; + /// {@template flutter.cupertino.navBar.brightness} + /// The brightness of the specified [backgroundColor]. + /// + /// Setting this value changes the style of the system status bar. Typically + /// used to increase the contrast ratio of the system status bar over + /// [backgroundColor]. + /// + /// If set to null, the value of the property will be inferred from the relative + /// luminance of [backgroundColor]. + /// {@endtemplate} + final Brightness brightness; + /// {@template flutter.cupertino.navBar.padding} /// Padding for the contents of the navigation bar. /// @@ -427,6 +449,7 @@ class _CupertinoNavigationBarState extends State { final Widget navBar = _wrapWithBackground( border: widget.border, backgroundColor: backgroundColor, + brightness: widget.brightness, child: DefaultTextStyle( style: CupertinoTheme.of(context).textTheme.textStyle, child: _PersistentNavigationBar( @@ -547,6 +570,7 @@ class CupertinoSliverNavigationBar extends StatefulWidget { this.trailing, this.border = _kDefaultNavBarBorder, this.backgroundColor, + this.brightness, this.padding, this.actionsForegroundColor, this.transitionBetweenRoutes = true, @@ -620,6 +644,9 @@ class CupertinoSliverNavigationBar extends StatefulWidget { /// {@macro flutter.cupertino.navBar.backgroundColor} final Color backgroundColor; + /// {@macro flutter.cupertino.navBar.brightness} + final Brightness brightness; + /// {@macro flutter.cupertino.navBar.padding} final EdgeInsetsDirectional padding; @@ -694,6 +721,7 @@ class _CupertinoSliverNavigationBarState extends State region1 = tester.allWidgets.singleWhere(( + Widget widget) => widget is AnnotatedRegion); + expect(region1.value, SystemUiOverlayStyle.light); + + await tester.pumpWidget( + const CupertinoApp( + home: CupertinoNavigationBar( + backgroundColor: Color(0xF01D1D1D), + brightness: Brightness.light, + ), + ), + ); + + final AnnotatedRegion region2 = tester.allWidgets.singleWhere(( + Widget widget) => widget is AnnotatedRegion); + expect(region2.value, SystemUiOverlayStyle.dark); + + await tester.pumpWidget( + const CupertinoApp( + home: CustomScrollView( + slivers: [ + CupertinoSliverNavigationBar( + largeTitle: Text('Title'), + backgroundColor: Color(0xF0F9F9F9), + brightness: Brightness.dark, + ) + ], + ), + ), + ); + + final AnnotatedRegion region3 = tester.allWidgets.singleWhere(( + Widget widget) => widget is AnnotatedRegion); + expect(region3.value, SystemUiOverlayStyle.light); + + await tester.pumpWidget( + const CupertinoApp( + home: CustomScrollView( + slivers: [ + CupertinoSliverNavigationBar( + largeTitle: Text('Title'), + backgroundColor: Color(0xF01D1D1D), + brightness: Brightness.light, + ) + ], + ), + ), + ); + + final AnnotatedRegion region4 = tester.allWidgets.singleWhere(( + Widget widget) => widget is AnnotatedRegion); + expect(region4.value, SystemUiOverlayStyle.dark); + }); + testWidgets('Padding works in RTL', (WidgetTester tester) async { await tester.pumpWidget( const CupertinoApp(