Skip to content

Commit

Permalink
Fix CupertinoTextSelectionToolbar showing unnecessary pagination (#10…
Browse files Browse the repository at this point in the history
…4587)
  • Loading branch information
tgucio committed Dec 14, 2022
1 parent d8b7eb6 commit 7b19b4d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Expand Up @@ -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<Widget> _addChildrenSpacers(List<Widget> children) {
final List<Widget> nextChildren = <Widget>[];
for (int i = 0; i < children.length; i++) {
Expand Down Expand Up @@ -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;
}
Expand Down
27 changes: 27 additions & 0 deletions packages/flutter/test/cupertino/text_selection_toolbar_test.dart
Expand Up @@ -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<Widget> children = List<Widget>.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;
Expand Down

0 comments on commit 7b19b4d

Please sign in to comment.