Unverified Commit 96ca0e27 authored by Pedro Massango's avatar Pedro Massango Committed by GitHub

Add onSelectionChanged into SelectableText widget (#65320)

parent dfd0c627
...@@ -199,6 +199,7 @@ class SelectableText extends StatefulWidget { ...@@ -199,6 +199,7 @@ class SelectableText extends StatefulWidget {
this.scrollPhysics, this.scrollPhysics,
this.textHeightBehavior, this.textHeightBehavior,
this.textWidthBasis, this.textWidthBasis,
this.onSelectionChanged,
}) : assert(showCursor != null), }) : assert(showCursor != null),
assert(autofocus != null), assert(autofocus != null),
assert(dragStartBehavior != null), assert(dragStartBehavior != null),
...@@ -250,6 +251,7 @@ class SelectableText extends StatefulWidget { ...@@ -250,6 +251,7 @@ class SelectableText extends StatefulWidget {
this.scrollPhysics, this.scrollPhysics,
this.textHeightBehavior, this.textHeightBehavior,
this.textWidthBasis, this.textWidthBasis,
this.onSelectionChanged,
}) : assert(showCursor != null), }) : assert(showCursor != null),
assert(autofocus != null), assert(autofocus != null),
assert(dragStartBehavior != null), assert(dragStartBehavior != null),
...@@ -392,6 +394,9 @@ class SelectableText extends StatefulWidget { ...@@ -392,6 +394,9 @@ class SelectableText extends StatefulWidget {
/// {@macro flutter.painting.textPainter.textWidthBasis} /// {@macro flutter.painting.textPainter.textWidthBasis}
final TextWidthBasis textWidthBasis; final TextWidthBasis textWidthBasis;
/// {@macro flutter.widgets.editableText.onSelectionChanged}
final SelectionChangedCallback onSelectionChanged;
@override @override
_SelectableTextState createState() => _SelectableTextState(); _SelectableTextState createState() => _SelectableTextState();
...@@ -494,6 +499,10 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive ...@@ -494,6 +499,10 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive
}); });
} }
if (widget.onSelectionChanged != null) {
widget.onSelectionChanged(selection, cause);
}
switch (Theme.of(context).platform) { switch (Theme.of(context).platform) {
case TargetPlatform.iOS: case TargetPlatform.iOS:
case TargetPlatform.macOS: case TargetPlatform.macOS:
......
...@@ -1044,8 +1044,10 @@ class EditableText extends StatefulWidget { ...@@ -1044,8 +1044,10 @@ class EditableText extends StatefulWidget {
/// {@endtemplate} /// {@endtemplate}
final AppPrivateCommandCallback onAppPrivateCommand; final AppPrivateCommandCallback onAppPrivateCommand;
/// {@template flutter.widgets.editableText.onSelectionChanged}
/// Called when the user changes the selection of text (including the cursor /// Called when the user changes the selection of text (including the cursor
/// location). /// location).
/// {@endtemplate}
final SelectionChangedCallback onSelectionChanged; final SelectionChangedCallback onSelectionChanged;
/// {@macro flutter.widgets.textSelection.onSelectionHandleTapped} /// {@macro flutter.widgets.textSelection.onSelectionHandleTapped}
......
...@@ -4012,4 +4012,33 @@ void main() { ...@@ -4012,4 +4012,33 @@ void main() {
expect(state.selectionOverlay.handlesAreVisible, isFalse); expect(state.selectionOverlay.handlesAreVisible, isFalse);
} }
}); });
testWidgets('onSelectionChanged is called when selection changes', (WidgetTester tester) async {
int onSelectionChangedCallCount = 0;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: SelectableText(
'abc def ghi',
onSelectionChanged: (TextSelection selection, SelectionChangedCause cause) {
onSelectionChangedCallCount += 1;
},
),
),
),
);
// Long press to select 'abc'.
final Offset aLocation = textOffsetToPosition(tester, 1);
await tester.longPressAt(aLocation);
await tester.pump();
expect(onSelectionChangedCallCount, equals(1));
// Tap on 'Select all' option to select the whole text.
await tester.longPressAt(textOffsetToPosition(tester, 5));
await tester.pump();
await tester.tap(find.text('Select all'));
expect(onSelectionChangedCallCount, equals(2));
});
} }
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