Skip to content

Commit 7b19b4d

Browse files
authored
Fix CupertinoTextSelectionToolbar showing unnecessary pagination (#104587)
1 parent d8b7eb6 commit 7b19b4d

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class CupertinoTextSelectionToolbar extends StatelessWidget {
103103
/// default Cupertino toolbar.
104104
final CupertinoToolbarBuilder toolbarBuilder;
105105

106-
// Add the visial vertical line spacer between children buttons.
106+
// Add the visual vertical line spacer between children buttons.
107107
static List<Widget> _addChildrenSpacers(List<Widget> children) {
108108
final List<Widget> nextChildren = <Widget>[];
109109
for (int i = 0; i < children.length; i++) {
@@ -801,8 +801,9 @@ class _RenderCupertinoTextSelectionToolbarItems extends RenderBox with Container
801801
double paginationButtonsWidth = 0.0;
802802
if (currentPage == 0) {
803803
// If this is the last child, it's ok to fit without a forward button.
804+
// Note childCount doesn't include slotted children which come before the list ones.
804805
paginationButtonsWidth =
805-
i == childCount - 1 ? 0.0 : _nextButton!.size.width;
806+
i == childCount + 2 ? 0.0 : _nextButton!.size.width;
806807
} else {
807808
paginationButtonsWidth = subsequentPageButtonsWidth;
808809
}

packages/flutter/test/cupertino/text_selection_toolbar_test.dart

+27
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ void main() {
181181
expect(findOverflowBackButton(), findsNothing);
182182
}, skip: kIsWeb); // [intended] We do not use Flutter-rendered context menu on the Web.
183183

184+
testWidgets('does not paginate if children fit with zero margin', (WidgetTester tester) async {
185+
final List<Widget> children = List<Widget>.generate(7, (int i) => const TestBox());
186+
final double spacerWidth = 1.0 / tester.binding.window.devicePixelRatio;
187+
final double dividerWidth = 1.0 / tester.binding.window.devicePixelRatio;
188+
const double borderRadius = 8.0; // Should match _kToolbarBorderRadius
189+
final double width = 7 * TestBox.itemWidth + 6 * (dividerWidth + 2 * spacerWidth) + 2 * borderRadius;
190+
await tester.pumpWidget(
191+
CupertinoApp(
192+
home: Center(
193+
child: SizedBox(
194+
width: width,
195+
child: CupertinoTextSelectionToolbar(
196+
anchorAbove: const Offset(50.0, 100.0),
197+
anchorBelow: const Offset(50.0, 200.0),
198+
children: children,
199+
),
200+
),
201+
),
202+
),
203+
);
204+
205+
// All children fit on the screen, so they are all rendered.
206+
expect(find.byType(TestBox), findsNWidgets(children.length));
207+
expect(findOverflowNextButton(), findsNothing);
208+
expect(findOverflowBackButton(), findsNothing);
209+
}, skip: kIsWeb); // [intended] We do not use Flutter-rendered context menu on the Web.
210+
184211
testWidgets('positions itself at anchorAbove if it fits', (WidgetTester tester) async {
185212
late StateSetter setState;
186213
const double height = _kToolbarHeight;

0 commit comments

Comments
 (0)