added ability to disable blur in CupertinoNavigationBar with transparent background (#153633)

This PR is enabling the CupertinoNavigationBar and CupertinoSliverNavigationBar to disable blur effect if background color is transparent.

Issues:
Fixes https://github.com/flutter/flutter/issues/151716

Before

![Simulator_Screenshot_-_iPhone_15_Pro_Max_-_2024-08-17_at_18 30 43](https://github.com/user-attachments/assets/9297e176-374c-4a4d-b6df-fa81f1b89ae2)

After

![simulator_screenshot_8587DA9F-E18B-4ED4-AFFB-1DA52FF5C0D2](https://github.com/user-attachments/assets/ac6eadbc-be9a-4c4c-9fb4-06e58055bab7)

<details>
  <summary>Demo App Code</summary>

```dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(const NavBarApp());

class NavBarApp extends StatelessWidget {
  const NavBarApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const CupertinoApp(
      theme: CupertinoThemeData(brightness: Brightness.light),
      home: NavBarExample(),
    );
  }
}

class NavBarExample extends StatefulWidget {
  const NavBarExample({super.key});

  @override
  State<NavBarExample> createState() => _NavBarExampleState();
}

class _NavBarExampleState extends State<NavBarExample> {
  @override
  Widget build(BuildContext context) {
    return const CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        backgroundColor: Colors.transparent,
        automaticBackgroundVisibility: false,
        disableBackgroundFilterBlur: true,
        middle: Text(
          'CupertinoNavigationBar Sample',
          style: TextStyle(color: CupertinoColors.activeBlue),
        ),
      ),
      child: Column(
        children: [
          Image(
            image: NetworkImage(
                'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
          ),
        ],
      ),
    );
  }
} 
```

</details>
This commit is contained in:
Sikandar Sadaqat
2024-08-27 02:40:47 +05:00
committed by GitHub
parent ac11f162e3
commit 9920a8727d
2 changed files with 54 additions and 3 deletions

View File

@@ -146,6 +146,7 @@ Widget _wrapWithBackground({
Brightness? brightness,
required Widget child,
bool updateSystemUiOverlay = true,
bool enableBackgroundFilterBlur = true,
}) {
Widget result = child;
if (updateSystemUiOverlay) {
@@ -182,7 +183,7 @@ Widget _wrapWithBackground({
return ClipRect(
child: BackdropFilter(
enabled: backgroundColor.alpha != 0xFF,
enabled: backgroundColor.alpha != 0xFF && enableBackgroundFilterBlur,
filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: childWithBackground,
),
@@ -266,6 +267,7 @@ class CupertinoNavigationBar extends StatefulWidget implements ObstructingPrefer
this.border = _kDefaultNavBarBorder,
this.backgroundColor,
this.automaticBackgroundVisibility = true,
this.enableBackgroundFilterBlur = true,
this.brightness,
this.padding,
this.transitionBetweenRoutes = true,
@@ -342,7 +344,8 @@ class CupertinoNavigationBar extends StatefulWidget implements ObstructingPrefer
/// {@template flutter.cupertino.CupertinoNavigationBar.backgroundColor}
/// The background color of the navigation bar. If it contains transparency, the
/// tab bar will automatically produce a blurring effect to the content
/// behind it.
/// behind it. This behavior can be disabled by setting [enableBackgroundFilterBlur]
/// to false.
///
/// By default, the navigation bar's background is visible only when scrolled under.
/// This behavior can be controlled with [automaticBackgroundVisibility].
@@ -417,6 +420,17 @@ class CupertinoNavigationBar extends StatefulWidget implements ObstructingPrefer
/// {@endtemplate}
final bool transitionBetweenRoutes;
/// {@template flutter.cupertino.CupertinoNavigationBar.enableBackgroundFilterBlur}
/// Whether to have a blur effect when a non-opaque background color is used.
///
/// When [enableBackgroundFilterBlur] is set to false, the blur effect will be
/// disabled. The behaviour of [enableBackgroundFilterBlur] will only be respected when
/// [automaticBackgroundVisibility] is false or until content scrolls under the navbar.
///
/// This value defaults to true.
/// {@endtemplate}
final bool enableBackgroundFilterBlur;
/// {@template flutter.cupertino.CupertinoNavigationBar.heroTag}
/// Tag for the navigation bar's Hero widget if [transitionBetweenRoutes] is true.
///
@@ -552,6 +566,7 @@ class _CupertinoNavigationBarState extends State<CupertinoNavigationBar> {
border: effectiveBorder,
backgroundColor: effectiveBackgroundColor,
brightness: widget.brightness,
enableBackgroundFilterBlur: widget.enableBackgroundFilterBlur,
child: DefaultTextStyle(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: _PersistentNavigationBar(
@@ -676,6 +691,7 @@ class CupertinoSliverNavigationBar extends StatefulWidget {
this.border = _kDefaultNavBarBorder,
this.backgroundColor,
this.automaticBackgroundVisibility = true,
this.enableBackgroundFilterBlur = true,
this.brightness,
this.padding,
this.transitionBetweenRoutes = true,
@@ -760,6 +776,9 @@ class CupertinoSliverNavigationBar extends StatefulWidget {
/// {@macro flutter.cupertino.CupertinoNavigationBar.automaticBackgroundVisibility}
final bool automaticBackgroundVisibility;
/// {@macro flutter.cupertino.CupertinoNavigationBar.enableBackgroundFilterBlur}
final bool enableBackgroundFilterBlur;
/// {@macro flutter.cupertino.CupertinoNavigationBar.brightness}
final Brightness? brightness;
@@ -840,6 +859,7 @@ class _CupertinoSliverNavigationBarState extends State<CupertinoSliverNavigation
persistentHeight: _kNavBarPersistentHeight + MediaQuery.paddingOf(context).top,
alwaysShowMiddle: widget.alwaysShowMiddle && widget.middle != null,
stretchConfiguration: widget.stretch ? OverScrollHeaderStretchConfiguration() : null,
enableBackgroundFilterBlur: widget.enableBackgroundFilterBlur,
),
),
);
@@ -863,6 +883,7 @@ class _LargeTitleNavigationBarSliverDelegate
required this.persistentHeight,
required this.alwaysShowMiddle,
required this.stretchConfiguration,
required this.enableBackgroundFilterBlur,
});
final _NavigationBarStaticComponentsKeys keys;
@@ -878,6 +899,7 @@ class _LargeTitleNavigationBarSliverDelegate
final Object heroTag;
final double persistentHeight;
final bool alwaysShowMiddle;
final bool enableBackgroundFilterBlur;
@override
double get minExtent => persistentHeight;
@@ -922,6 +944,7 @@ class _LargeTitleNavigationBarSliverDelegate
border: effectiveBorder,
backgroundColor: effectiveBackgroundColor,
brightness: brightness,
enableBackgroundFilterBlur: enableBackgroundFilterBlur,
child: DefaultTextStyle(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: Stack(
@@ -1014,7 +1037,8 @@ class _LargeTitleNavigationBarSliverDelegate
|| transitionBetweenRoutes != oldDelegate.transitionBetweenRoutes
|| persistentHeight != oldDelegate.persistentHeight
|| alwaysShowMiddle != oldDelegate.alwaysShowMiddle
|| heroTag != oldDelegate.heroTag;
|| heroTag != oldDelegate.heroTag
|| enableBackgroundFilterBlur != oldDelegate.enableBackgroundFilterBlur;
}
}

View File

@@ -157,6 +157,33 @@ void main() {
);
});
testWidgets('Blur affect is disabled when enableBackgroundFilterBlur is false', (WidgetTester test) async {
await test.pumpWidget(
CupertinoApp(
theme: const CupertinoThemeData(brightness: Brightness.light),
home: CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
backgroundColor: const Color(0xFFFFFFFF).withOpacity(0),
middle: const Text('Title'),
automaticBackgroundVisibility: false,
enableBackgroundFilterBlur: false,
),
child: const Column(
children: <Widget>[
Placeholder(),
],
),
),
),
);
expect(
test.widget(find.byType(BackdropFilter)),
isA<BackdropFilter>().having(
(BackdropFilter filter) => filter.enabled, 'filter enabled', false),
);
});
testWidgets('Nav bar displays correctly', (WidgetTester tester) async {
final GlobalKey<NavigatorState> navigator = GlobalKey<NavigatorState>();
await tester.pumpWidget(