From d988c11a87ef99f428d1e731464340be2bf891e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20=C4=8Campa?= Date: Wed, 26 Oct 2022 18:28:44 +0200 Subject: [PATCH] Expose `alwaysShowMiddle` in `CupertinoSliverNavigationBar` (#113544) * SLFLF-5: Expose `alwaysShowMiddle` in `CupertinoSliverNavigationBar` * SLFLF-5: Remove space in docstring * SLFLF-5: Add test and update documentation * SLFLF-5: Remove trailing whitespace (again) --- .../flutter/lib/src/cupertino/nav_bar.dart | 17 ++++++- .../flutter/test/cupertino/nav_bar_test.dart | 46 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/nav_bar.dart b/packages/flutter/lib/src/cupertino/nav_bar.dart index 5d730e820e..8261bd9a48 100644 --- a/packages/flutter/lib/src/cupertino/nav_bar.dart +++ b/packages/flutter/lib/src/cupertino/nav_bar.dart @@ -586,6 +586,7 @@ class CupertinoSliverNavigationBar extends StatefulWidget { this.leading, this.automaticallyImplyLeading = true, this.automaticallyImplyTitle = true, + this.alwaysShowMiddle = true, this.previousPageTitle, this.middle, this.trailing, @@ -645,13 +646,25 @@ class CupertinoSliverNavigationBar extends StatefulWidget { /// This value cannot be null. final bool automaticallyImplyTitle; + /// Controls whether [middle] widget should always be visible (even in + /// expanded state). + /// + /// If true (default) and [middle] is not null, [middle] widget is always + /// visible. If false, [middle] widget is visible only in collapsed state if + /// it is provided. + /// + /// This should be set to false if you only want to show [largeTitle] in + /// expanded state and [middle] in collapsed state. + final bool alwaysShowMiddle; + /// {@macro flutter.cupertino.CupertinoNavigationBar.previousPageTitle} final String? previousPageTitle; /// A widget to place in the middle of the static navigation bar instead of /// the [largeTitle]. /// - /// This widget is visible in both collapsed and expanded states. The text + /// This widget is visible in both collapsed and expanded states if + /// [alwaysShowMiddle] is true, otherwise just in collapsed state. The text /// supplied in [largeTitle] will no longer appear in collapsed state if a /// [middle] widget is provided. final Widget? middle; @@ -742,7 +755,7 @@ class _CupertinoSliverNavigationBarState extends State[ + CupertinoSliverNavigationBar( + largeTitle: Text('Large'), + middle: Text('Middle'), + alwaysShowMiddle: false, + ), + SliverToBoxAdapter( + child: SizedBox( + height: 1200.0, + ), + ), + ], + ), + ), + ), + ); + + expect(scrollController.offset, 0.0); + expect(find.text('Middle'), findsOneWidget); + + // Initially (in expanded state) middle widget is not visible. + RenderAnimatedOpacity middleOpacity = tester.element(find.text('Middle')).findAncestorRenderObjectOfType()!; + expect(middleOpacity.opacity.value, 0.0); + + scrollController.jumpTo(600.0); + await tester.pumpAndSettle(); + + // Middle widget is visible when nav bar is collapsed. + middleOpacity = tester.element(find.text('Middle')).findAncestorRenderObjectOfType()!; + expect(middleOpacity.opacity.value, 1.0); + + scrollController.jumpTo(0.0); + await tester.pumpAndSettle(); + + // Middle widget is not visible when nav bar is again expanded. + middleOpacity = tester.element(find.text('Middle')).findAncestorRenderObjectOfType()!; + expect(middleOpacity.opacity.value, 0.0); + }); + testWidgets('Small title can be overridden', (WidgetTester tester) async { final ScrollController scrollController = ScrollController(); await tester.pumpWidget(