Commit 2cbf126f authored by 神楽坂花火's avatar 神楽坂花火 Committed by LongCatIsLooong

Add `brightness` to CupertinoNavigationBar (fixes #46216) (#47521)

parent 6e72faca
......@@ -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<SystemUiOverlayStyle>(
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<CupertinoNavigationBar> {
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<CupertinoSliverNavigation
components: components,
userMiddle: widget.middle,
backgroundColor: CupertinoDynamicColor.resolve(widget.backgroundColor, context) ?? CupertinoTheme.of(context).barBackgroundColor,
brightness: widget.brightness,
border: widget.border,
padding: widget.padding,
actionsForegroundColor: actionsForegroundColor,
......@@ -715,6 +743,7 @@ class _LargeTitleNavigationBarSliverDelegate
@required this.components,
@required this.userMiddle,
@required this.backgroundColor,
@required this.brightness,
@required this.border,
@required this.padding,
@required this.actionsForegroundColor,
......@@ -730,6 +759,7 @@ class _LargeTitleNavigationBarSliverDelegate
final _NavigationBarStaticComponents components;
final Widget userMiddle;
final Color backgroundColor;
final Brightness brightness;
final Border border;
final EdgeInsetsDirectional padding;
final Color actionsForegroundColor;
......@@ -760,6 +790,7 @@ class _LargeTitleNavigationBarSliverDelegate
final Widget navBar = _wrapWithBackground(
border: border,
backgroundColor: CupertinoDynamicColor.resolve(backgroundColor, context),
brightness: brightness,
child: DefaultTextStyle(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: Stack(
......
......@@ -136,6 +136,70 @@ void main() {
expect(tester.getCenter(find.text('Title')).dx, 400.0);
});
testWidgets('Can specify custom brightness', (WidgetTester tester) async {
await tester.pumpWidget(
const CupertinoApp(
home: CupertinoNavigationBar(
backgroundColor: Color(0xF0F9F9F9),
brightness: Brightness.dark,
),
),
);
final AnnotatedRegion<SystemUiOverlayStyle> 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<SystemUiOverlayStyle> region2 = tester.allWidgets.singleWhere((
Widget widget) => widget is AnnotatedRegion);
expect(region2.value, SystemUiOverlayStyle.dark);
await tester.pumpWidget(
const CupertinoApp(
home: CustomScrollView(
slivers: <Widget>[
CupertinoSliverNavigationBar(
largeTitle: Text('Title'),
backgroundColor: Color(0xF0F9F9F9),
brightness: Brightness.dark,
)
],
),
),
);
final AnnotatedRegion<SystemUiOverlayStyle> region3 = tester.allWidgets.singleWhere((
Widget widget) => widget is AnnotatedRegion);
expect(region3.value, SystemUiOverlayStyle.light);
await tester.pumpWidget(
const CupertinoApp(
home: CustomScrollView(
slivers: <Widget>[
CupertinoSliverNavigationBar(
largeTitle: Text('Title'),
backgroundColor: Color(0xF01D1D1D),
brightness: Brightness.light,
)
],
),
),
);
final AnnotatedRegion<SystemUiOverlayStyle> 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(
......
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