Unverified Commit 62eebb49 authored by Markus Aksli's avatar Markus Aksli Committed by GitHub

Hide `SelectableRegion` text selection toolbar when orientation changes (#104841)

parent 053144b7
......@@ -16,6 +16,7 @@ import 'focus_manager.dart';
import 'focus_scope.dart';
import 'framework.dart';
import 'gesture_detector.dart';
import 'media_query.dart';
import 'overlay.dart';
import 'selection_container.dart';
import 'text_editing_intents.dart';
......@@ -212,6 +213,8 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
bool get _hasSelectionOverlayGeometry => _selectionDelegate.value.startSelectionPoint != null
|| _selectionDelegate.value.endSelectionPoint != null;
Orientation? _lastOrientation;
@override
void initState() {
super.initState();
......@@ -228,6 +231,33 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
break;
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
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;
hideToolbar(defaultTargetPlatform == TargetPlatform.android);
}
}
@override
void didUpdateWidget(SelectableRegion oldWidget) {
super.didUpdateWidget(oldWidget);
......@@ -703,7 +733,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD
void hideToolbar([bool hideHandles = true]) {
_selectionOverlay?.hideToolbar();
if (hideHandles) {
_selectionOverlay?.hideToolbar();
_selectionOverlay?.hideHandles();
}
}
......
......@@ -1100,6 +1100,46 @@ void main() {
expect(clipboardData['text'], 'thank');
}, skip: kIsWeb); // [intended] Web uses its native context menu.
});
testWidgets('toolbar is hidden on mobile when orientation changes', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: SelectableRegion(
focusNode: FocusNode(),
selectionControls: materialTextSelectionControls,
child: const Text('How are you?'),
),
),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
final RenderParagraph paragraph1 = tester.renderObject<RenderParagraph>(find.descendant(of: find.text('How are you?'), matching: find.byType(RichText)));
final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph1, 6)); // at the 'r'
addTearDown(gesture.removePointer);
await tester.pump(const Duration(milliseconds: 500));
// `are` is selected.
expect(paragraph1.selections[0], const TextSelection(baseOffset: 4, extentOffset: 7));
await tester.pumpAndSettle();
// Text selection toolbar has appeared.
expect(find.text('Copy'), findsOneWidget);
// Hide the toolbar by changing orientation.
tester.binding.window.physicalSizeTestValue = const Size(1800.0, 2400.0);
await tester.pumpAndSettle();
expect(find.text('Copy'), findsNothing);
// Handles should be hidden as well on Android
expect(
find.descendant(
of: find.byType(CompositedTransformFollower),
matching: find.byType(Padding),
),
defaultTargetPlatform == TargetPlatform.android ? findsNothing : findsNWidgets(2),
);
},
skip: kIsWeb, // [intended] Web uses its native context menu.
variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.android }),
);
}
class SelectionSpy extends LeafRenderObjectWidget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment