Skip to content

Commit b05741c

Browse files
Hide the text selection toolbar on mobile when orientation changes (#103512)
1 parent 5a91967 commit b05741c

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

packages/flutter/lib/src/widgets/editable_text.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,8 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
16461646

16471647
AnimationController? _floatingCursorResetController;
16481648

1649+
Orientation? _lastOrientation;
1650+
16491651
@override
16501652
bool get wantKeepAlive => widget.focusNode.hasFocus;
16511653

@@ -1850,6 +1852,26 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
18501852
_cursorTimer = null;
18511853
}
18521854
}
1855+
1856+
if (defaultTargetPlatform != TargetPlatform.iOS && defaultTargetPlatform != TargetPlatform.android) {
1857+
return;
1858+
}
1859+
1860+
// Hide the text selection toolbar on mobile when orientation changes.
1861+
final Orientation orientation = MediaQuery.of(context).orientation;
1862+
if (_lastOrientation == null) {
1863+
_lastOrientation = orientation;
1864+
return;
1865+
}
1866+
if (orientation != _lastOrientation) {
1867+
_lastOrientation = orientation;
1868+
if (defaultTargetPlatform == TargetPlatform.iOS) {
1869+
hideToolbar(false);
1870+
}
1871+
if (defaultTargetPlatform == TargetPlatform.android) {
1872+
hideToolbar();
1873+
}
1874+
}
18531875
}
18541876

18551877
@override

packages/flutter/test/widgets/editable_text_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,54 @@ void main() {
15031503
expect(find.text('Paste'), findsNothing);
15041504
});
15051505

1506+
testWidgets('toolbar hidden on mobile when orientation changes', (WidgetTester tester) async {
1507+
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
1508+
1509+
await tester.pumpWidget(
1510+
MaterialApp(
1511+
home: EditableText(
1512+
backgroundCursorColor: Colors.grey,
1513+
controller: controller,
1514+
focusNode: focusNode,
1515+
style: textStyle,
1516+
cursorColor: cursorColor,
1517+
selectionControls: materialTextSelectionControls,
1518+
),
1519+
),
1520+
);
1521+
1522+
final EditableTextState state =
1523+
tester.state<EditableTextState>(find.byType(EditableText));
1524+
1525+
// Show the toolbar
1526+
state.renderEditable.selectWordsInRange(
1527+
from: Offset.zero,
1528+
cause: SelectionChangedCause.tap,
1529+
);
1530+
await tester.pump();
1531+
1532+
expect(state.showToolbar(), true);
1533+
await tester.pumpAndSettle();
1534+
expect(find.text('Paste'), findsOneWidget);
1535+
1536+
// Hide the menu by changing orientation.
1537+
tester.binding.window.physicalSizeTestValue = const Size(1800.0, 2400.0);
1538+
await tester.pumpAndSettle();
1539+
expect(find.text('Paste'), findsNothing);
1540+
1541+
// Handles should be hidden as well on Android
1542+
expect(
1543+
find.descendant(
1544+
of: find.byType(CompositedTransformFollower),
1545+
matching: find.byType(Padding),
1546+
),
1547+
defaultTargetPlatform == TargetPlatform.android ? findsNothing : findsOneWidget,
1548+
);
1549+
1550+
// On web, we don't show the Flutter toolbar and instead rely on the browser
1551+
// toolbar. Until we change that, this test should remain skipped.
1552+
}, skip: kIsWeb, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.android })); // [intended]
1553+
15061554
testWidgets('Paste is shown only when there is something to paste', (WidgetTester tester) async {
15071555
await tester.pumpWidget(
15081556
MaterialApp(

0 commit comments

Comments
 (0)