Add onSelectionChanged into SelectableText widget (#65320)

This commit is contained in:
Pedro Massango
2020-09-15 09:02:04 +01:00
committed by GitHub
parent dfd0c6270f
commit 96ca0e272b
3 changed files with 40 additions and 0 deletions

View File

@@ -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:

View File

@@ -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}

View File

@@ -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));
});
}