From b05741cbada55b1db1d612a25afab52e6ebe011f Mon Sep 17 00:00:00 2001 From: Markus Aksli <68219924+markusaksli-nc@users.noreply.github.com> Date: Wed, 25 May 2022 23:03:16 +0300 Subject: [PATCH] Hide the text selection toolbar on mobile when orientation changes (#103512) --- .../lib/src/widgets/editable_text.dart | 22 +++++++++ .../test/widgets/editable_text_test.dart | 48 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index f50d6a6b96..a5df88c8d4 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -1646,6 +1646,8 @@ class EditableTextState extends State with AutomaticKeepAliveClien AnimationController? _floatingCursorResetController; + Orientation? _lastOrientation; + @override bool get wantKeepAlive => widget.focusNode.hasFocus; @@ -1850,6 +1852,26 @@ class EditableTextState extends State with AutomaticKeepAliveClien _cursorTimer = null; } } + + if (defaultTargetPlatform != TargetPlatform.iOS && defaultTargetPlatform != TargetPlatform.android) { + return; + } + + // Hide the text selection toolbar on mobile when orientation changes. + final Orientation orientation = MediaQuery.of(context).orientation; + if (_lastOrientation == null) { + _lastOrientation = orientation; + return; + } + if (orientation != _lastOrientation) { + _lastOrientation = orientation; + if (defaultTargetPlatform == TargetPlatform.iOS) { + hideToolbar(false); + } + if (defaultTargetPlatform == TargetPlatform.android) { + hideToolbar(); + } + } } @override diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 813606533d..fc9ff5b487 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -1503,6 +1503,54 @@ void main() { expect(find.text('Paste'), findsNothing); }); + testWidgets('toolbar hidden on mobile when orientation changes', (WidgetTester tester) async { + addTearDown(tester.binding.window.clearPhysicalSizeTestValue); + + await tester.pumpWidget( + MaterialApp( + home: EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + selectionControls: materialTextSelectionControls, + ), + ), + ); + + final EditableTextState state = + tester.state(find.byType(EditableText)); + + // Show the toolbar + state.renderEditable.selectWordsInRange( + from: Offset.zero, + cause: SelectionChangedCause.tap, + ); + await tester.pump(); + + expect(state.showToolbar(), true); + await tester.pumpAndSettle(); + expect(find.text('Paste'), findsOneWidget); + + // Hide the menu by changing orientation. + tester.binding.window.physicalSizeTestValue = const Size(1800.0, 2400.0); + await tester.pumpAndSettle(); + expect(find.text('Paste'), findsNothing); + + // Handles should be hidden as well on Android + expect( + find.descendant( + of: find.byType(CompositedTransformFollower), + matching: find.byType(Padding), + ), + defaultTargetPlatform == TargetPlatform.android ? findsNothing : findsOneWidget, + ); + + // On web, we don't show the Flutter toolbar and instead rely on the browser + // toolbar. Until we change that, this test should remain skipped. + }, skip: kIsWeb, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.android })); // [intended] + testWidgets('Paste is shown only when there is something to paste', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp(