Add onSelectionChanged into SelectableText widget (#65320)
This commit is contained in:
@@ -199,6 +199,7 @@ class SelectableText extends StatefulWidget {
|
||||
this.scrollPhysics,
|
||||
this.textHeightBehavior,
|
||||
this.textWidthBasis,
|
||||
this.onSelectionChanged,
|
||||
}) : assert(showCursor != null),
|
||||
assert(autofocus != null),
|
||||
assert(dragStartBehavior != null),
|
||||
@@ -250,6 +251,7 @@ class SelectableText extends StatefulWidget {
|
||||
this.scrollPhysics,
|
||||
this.textHeightBehavior,
|
||||
this.textWidthBasis,
|
||||
this.onSelectionChanged,
|
||||
}) : assert(showCursor != null),
|
||||
assert(autofocus != null),
|
||||
assert(dragStartBehavior != null),
|
||||
@@ -392,6 +394,9 @@ class SelectableText extends StatefulWidget {
|
||||
/// {@macro flutter.painting.textPainter.textWidthBasis}
|
||||
final TextWidthBasis textWidthBasis;
|
||||
|
||||
/// {@macro flutter.widgets.editableText.onSelectionChanged}
|
||||
final SelectionChangedCallback onSelectionChanged;
|
||||
|
||||
@override
|
||||
_SelectableTextState createState() => _SelectableTextState();
|
||||
|
||||
@@ -494,6 +499,10 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive
|
||||
});
|
||||
}
|
||||
|
||||
if (widget.onSelectionChanged != null) {
|
||||
widget.onSelectionChanged(selection, cause);
|
||||
}
|
||||
|
||||
switch (Theme.of(context).platform) {
|
||||
case TargetPlatform.iOS:
|
||||
case TargetPlatform.macOS:
|
||||
|
||||
@@ -1044,8 +1044,10 @@ class EditableText extends StatefulWidget {
|
||||
/// {@endtemplate}
|
||||
final AppPrivateCommandCallback onAppPrivateCommand;
|
||||
|
||||
/// {@template flutter.widgets.editableText.onSelectionChanged}
|
||||
/// Called when the user changes the selection of text (including the cursor
|
||||
/// location).
|
||||
/// {@endtemplate}
|
||||
final SelectionChangedCallback onSelectionChanged;
|
||||
|
||||
/// {@macro flutter.widgets.textSelection.onSelectionHandleTapped}
|
||||
|
||||
@@ -4012,4 +4012,33 @@ void main() {
|
||||
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));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user