Unverified Commit 44ebcb66 authored by Bikram Pandit's avatar Bikram Pandit Committed by GitHub

Move cursor position to the end of search query in SearchDelegate after query is set (#83512)

parent 921c724d
......@@ -248,9 +248,14 @@ abstract class SearchDelegate<T> {
/// If the user taps on a suggestion provided by [buildSuggestions] this
/// string should be updated to that suggestion via the setter.
String get query => _queryTextController.text;
/// Changes the current query string.
///
/// Setting the query string programmatically moves the cursor to the end of the text field.
set query(String value) {
assert(query != null);
_queryTextController.text = value;
_queryTextController.selection = TextSelection.fromPosition(TextPosition(offset: _queryTextController.text.length));
}
/// Transition from the suggestions returned by [buildSuggestions] to the
......
......@@ -40,6 +40,27 @@ void main() {
TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, null);
});
testWidgets('Changing query moves cursor to the end of query', (WidgetTester tester) async {
final _TestSearchDelegate delegate = _TestSearchDelegate();
await tester.pumpWidget(TestHomePage(delegate: delegate));
await tester.tap(find.byTooltip('Search'));
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
delegate.query = 'Foo';
final TextField textField = tester.widget<TextField>(find.byType(TextField));
expect(
textField.controller!.selection,
TextSelection(
baseOffset: delegate.query.length,
extentOffset: delegate.query.length,
),
);
});
testWidgets('Can open and close search', (WidgetTester tester) async {
final _TestSearchDelegate delegate = _TestSearchDelegate();
final List<String> selectedResults = <String>[];
......
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