From 9920a8727ddce6dbb2fe39819fad960ef43a9631 Mon Sep 17 00:00:00 2001 From: Sikandar Sadaqat Date: Tue, 27 Aug 2024 02:40:47 +0500 Subject: [PATCH] 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)
Demo App Code ```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 createState() => _NavBarExampleState(); } class _NavBarExampleState extends State { @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'), ), ], ), ); } } ```
--- .../flutter/lib/src/cupertino/nav_bar.dart | 30 +++++++++++++++++-- .../flutter/test/cupertino/nav_bar_test.dart | 27 +++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/nav_bar.dart b/packages/flutter/lib/src/cupertino/nav_bar.dart index 00b3c05eee..e7c2c37cd0 100644 --- a/packages/flutter/lib/src/cupertino/nav_bar.dart +++ b/packages/flutter/lib/src/cupertino/nav_bar.dart @@ -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 { 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 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; } } diff --git a/packages/flutter/test/cupertino/nav_bar_test.dart b/packages/flutter/test/cupertino/nav_bar_test.dart index faf500e9d6..f87e3e12c8 100644 --- a/packages/flutter/test/cupertino/nav_bar_test.dart +++ b/packages/flutter/test/cupertino/nav_bar_test.dart @@ -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: [ + Placeholder(), + ], + ), + ), + ), + ); + + expect( + test.widget(find.byType(BackdropFilter)), + isA().having( + (BackdropFilter filter) => filter.enabled, 'filter enabled', false), + ); + }); + testWidgets('Nav bar displays correctly', (WidgetTester tester) async { final GlobalKey navigator = GlobalKey(); await tester.pumpWidget(