Skip to content

Commit d988c11

Browse files
authored
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)
1 parent e6be983 commit d988c11

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

packages/flutter/lib/src/cupertino/nav_bar.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class CupertinoSliverNavigationBar extends StatefulWidget {
586586
this.leading,
587587
this.automaticallyImplyLeading = true,
588588
this.automaticallyImplyTitle = true,
589+
this.alwaysShowMiddle = true,
589590
this.previousPageTitle,
590591
this.middle,
591592
this.trailing,
@@ -645,13 +646,25 @@ class CupertinoSliverNavigationBar extends StatefulWidget {
645646
/// This value cannot be null.
646647
final bool automaticallyImplyTitle;
647648

649+
/// Controls whether [middle] widget should always be visible (even in
650+
/// expanded state).
651+
///
652+
/// If true (default) and [middle] is not null, [middle] widget is always
653+
/// visible. If false, [middle] widget is visible only in collapsed state if
654+
/// it is provided.
655+
///
656+
/// This should be set to false if you only want to show [largeTitle] in
657+
/// expanded state and [middle] in collapsed state.
658+
final bool alwaysShowMiddle;
659+
648660
/// {@macro flutter.cupertino.CupertinoNavigationBar.previousPageTitle}
649661
final String? previousPageTitle;
650662

651663
/// A widget to place in the middle of the static navigation bar instead of
652664
/// the [largeTitle].
653665
///
654-
/// This widget is visible in both collapsed and expanded states. The text
666+
/// This widget is visible in both collapsed and expanded states if
667+
/// [alwaysShowMiddle] is true, otherwise just in collapsed state. The text
655668
/// supplied in [largeTitle] will no longer appear in collapsed state if a
656669
/// [middle] widget is provided.
657670
final Widget? middle;
@@ -742,7 +755,7 @@ class _CupertinoSliverNavigationBarState extends State<CupertinoSliverNavigation
742755
transitionBetweenRoutes: widget.transitionBetweenRoutes,
743756
heroTag: widget.heroTag,
744757
persistentHeight: _kNavBarPersistentHeight + MediaQuery.of(context).padding.top,
745-
alwaysShowMiddle: widget.middle != null,
758+
alwaysShowMiddle: widget.alwaysShowMiddle && widget.middle != null,
746759
stretchConfiguration: widget.stretch ? OverScrollHeaderStretchConfiguration() : null,
747760
),
748761
),

packages/flutter/test/cupertino/nav_bar_test.dart

+46
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,52 @@ void main() {
532532
);
533533
});
534534

535+
testWidgets('User specified middle is only visible when sliver is collapsed if alwaysShowMiddle is false', (WidgetTester tester) async {
536+
final ScrollController scrollController = ScrollController();
537+
await tester.pumpWidget(
538+
CupertinoApp(
539+
home: CupertinoPageScaffold(
540+
child: CustomScrollView(
541+
controller: scrollController,
542+
slivers: const <Widget>[
543+
CupertinoSliverNavigationBar(
544+
largeTitle: Text('Large'),
545+
middle: Text('Middle'),
546+
alwaysShowMiddle: false,
547+
),
548+
SliverToBoxAdapter(
549+
child: SizedBox(
550+
height: 1200.0,
551+
),
552+
),
553+
],
554+
),
555+
),
556+
),
557+
);
558+
559+
expect(scrollController.offset, 0.0);
560+
expect(find.text('Middle'), findsOneWidget);
561+
562+
// Initially (in expanded state) middle widget is not visible.
563+
RenderAnimatedOpacity middleOpacity = tester.element(find.text('Middle')).findAncestorRenderObjectOfType<RenderAnimatedOpacity>()!;
564+
expect(middleOpacity.opacity.value, 0.0);
565+
566+
scrollController.jumpTo(600.0);
567+
await tester.pumpAndSettle();
568+
569+
// Middle widget is visible when nav bar is collapsed.
570+
middleOpacity = tester.element(find.text('Middle')).findAncestorRenderObjectOfType<RenderAnimatedOpacity>()!;
571+
expect(middleOpacity.opacity.value, 1.0);
572+
573+
scrollController.jumpTo(0.0);
574+
await tester.pumpAndSettle();
575+
576+
// Middle widget is not visible when nav bar is again expanded.
577+
middleOpacity = tester.element(find.text('Middle')).findAncestorRenderObjectOfType<RenderAnimatedOpacity>()!;
578+
expect(middleOpacity.opacity.value, 0.0);
579+
});
580+
535581
testWidgets('Small title can be overridden', (WidgetTester tester) async {
536582
final ScrollController scrollController = ScrollController();
537583
await tester.pumpWidget(

0 commit comments

Comments
 (0)