From 7b19b4d38707a4a93032529273c21a1fd93dd71e Mon Sep 17 00:00:00 2001 From: Tomasz Gucio <72562119+tgucio@users.noreply.github.com> Date: Wed, 14 Dec 2022 22:38:04 +0100 Subject: [PATCH] Fix CupertinoTextSelectionToolbar showing unnecessary pagination (#104587) --- .../src/cupertino/text_selection_toolbar.dart | 5 ++-- .../text_selection_toolbar_test.dart | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart b/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart index d0fee9557951..6e38518783fe 100644 --- a/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart +++ b/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart @@ -103,7 +103,7 @@ class CupertinoTextSelectionToolbar extends StatelessWidget { /// default Cupertino toolbar. final CupertinoToolbarBuilder toolbarBuilder; - // Add the visial vertical line spacer between children buttons. + // Add the visual vertical line spacer between children buttons. static List _addChildrenSpacers(List children) { final List nextChildren = []; for (int i = 0; i < children.length; i++) { @@ -801,8 +801,9 @@ class _RenderCupertinoTextSelectionToolbarItems extends RenderBox with Container double paginationButtonsWidth = 0.0; if (currentPage == 0) { // If this is the last child, it's ok to fit without a forward button. + // Note childCount doesn't include slotted children which come before the list ones. paginationButtonsWidth = - i == childCount - 1 ? 0.0 : _nextButton!.size.width; + i == childCount + 2 ? 0.0 : _nextButton!.size.width; } else { paginationButtonsWidth = subsequentPageButtonsWidth; } diff --git a/packages/flutter/test/cupertino/text_selection_toolbar_test.dart b/packages/flutter/test/cupertino/text_selection_toolbar_test.dart index 5f236968ea00..21c18c91921c 100644 --- a/packages/flutter/test/cupertino/text_selection_toolbar_test.dart +++ b/packages/flutter/test/cupertino/text_selection_toolbar_test.dart @@ -181,6 +181,33 @@ void main() { expect(findOverflowBackButton(), findsNothing); }, skip: kIsWeb); // [intended] We do not use Flutter-rendered context menu on the Web. + testWidgets('does not paginate if children fit with zero margin', (WidgetTester tester) async { + final List children = List.generate(7, (int i) => const TestBox()); + final double spacerWidth = 1.0 / tester.binding.window.devicePixelRatio; + final double dividerWidth = 1.0 / tester.binding.window.devicePixelRatio; + const double borderRadius = 8.0; // Should match _kToolbarBorderRadius + final double width = 7 * TestBox.itemWidth + 6 * (dividerWidth + 2 * spacerWidth) + 2 * borderRadius; + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: SizedBox( + width: width, + child: CupertinoTextSelectionToolbar( + anchorAbove: const Offset(50.0, 100.0), + anchorBelow: const Offset(50.0, 200.0), + children: children, + ), + ), + ), + ), + ); + + // All children fit on the screen, so they are all rendered. + expect(find.byType(TestBox), findsNWidgets(children.length)); + expect(findOverflowNextButton(), findsNothing); + expect(findOverflowBackButton(), findsNothing); + }, skip: kIsWeb); // [intended] We do not use Flutter-rendered context menu on the Web. + testWidgets('positions itself at anchorAbove if it fits', (WidgetTester tester) async { late StateSetter setState; const double height = _kToolbarHeight;