Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Expose alwaysShowMiddle in CupertinoSliverNavigationBar #113544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/flutter/lib/src/cupertino/nav_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -742,7 +755,7 @@ class _CupertinoSliverNavigationBarState extends State<CupertinoSliverNavigation
transitionBetweenRoutes: widget.transitionBetweenRoutes,
heroTag: widget.heroTag,
persistentHeight: _kNavBarPersistentHeight + MediaQuery.of(context).padding.top,
alwaysShowMiddle: widget.middle != null,
alwaysShowMiddle: widget.alwaysShowMiddle && widget.middle != null,
stretchConfiguration: widget.stretch ? OverScrollHeaderStretchConfiguration() : null,
),
),
Expand Down
46 changes: 46 additions & 0 deletions packages/flutter/test/cupertino/nav_bar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,52 @@ void main() {
);
});

testWidgets('User specified middle is only visible when sliver is collapsed if alwaysShowMiddle is false', (WidgetTester tester) async {
final ScrollController scrollController = ScrollController();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
child: CustomScrollView(
controller: scrollController,
slivers: const <Widget>[
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<RenderAnimatedOpacity>()!;
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<RenderAnimatedOpacity>()!;
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<RenderAnimatedOpacity>()!;
expect(middleOpacity.opacity.value, 0.0);
});

testWidgets('Small title can be overridden', (WidgetTester tester) async {
final ScrollController scrollController = ScrollController();
await tester.pumpWidget(
Expand Down