Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
40b33e23
Unverified
Commit
40b33e23
authored
Sep 08, 2021
by
J-P Nurmi
Committed by
GitHub
Sep 08, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Autocomplete: support asynchronous options (#88193)
parent
13b38add
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
3 deletions
+66
-3
autocomplete.dart
packages/flutter/lib/src/widgets/autocomplete.dart
+5
-3
autocomplete_test.dart
packages/flutter/test/widgets/autocomplete_test.dart
+61
-0
No files found.
packages/flutter/lib/src/widgets/autocomplete.dart
View file @
40b33e23
...
@@ -2,6 +2,8 @@
...
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// found in the LICENSE file.
import
'dart:async'
;
import
'package:flutter/scheduler.dart'
;
import
'package:flutter/scheduler.dart'
;
import
'package:flutter/services.dart'
;
import
'package:flutter/services.dart'
;
...
@@ -22,7 +24,7 @@ import 'shortcuts.dart';
...
@@ -22,7 +24,7 @@ import 'shortcuts.dart';
/// See also:
/// See also:
///
///
/// * [RawAutocomplete.optionsBuilder], which is of this type.
/// * [RawAutocomplete.optionsBuilder], which is of this type.
typedef
AutocompleteOptionsBuilder
<
T
extends
Object
>
=
Iterable
<
T
>
Function
(
TextEditingValue
textEditingValue
);
typedef
AutocompleteOptionsBuilder
<
T
extends
Object
>
=
FutureOr
<
Iterable
<
T
>
>
Function
(
TextEditingValue
textEditingValue
);
/// The type of the callback used by the [RawAutocomplete] widget to indicate
/// The type of the callback used by the [RawAutocomplete] widget to indicate
/// that the user has selected an option.
/// that the user has selected an option.
...
@@ -293,8 +295,8 @@ class _RawAutocompleteState<T extends Object> extends State<RawAutocomplete<T>>
...
@@ -293,8 +295,8 @@ class _RawAutocompleteState<T extends Object> extends State<RawAutocomplete<T>>
}
}
// Called when _textEditingController changes.
// Called when _textEditingController changes.
void
_onChangedField
()
{
Future
<
void
>
_onChangedField
()
async
{
final
Iterable
<
T
>
options
=
widget
.
optionsBuilder
(
final
Iterable
<
T
>
options
=
await
widget
.
optionsBuilder
(
_textEditingController
.
value
,
_textEditingController
.
value
,
);
);
_options
=
options
;
_options
=
options
;
...
...
packages/flutter/test/widgets/autocomplete_test.dart
View file @
40b33e23
...
@@ -647,6 +647,67 @@ void main() {
...
@@ -647,6 +647,67 @@ void main() {
);
);
});
});
testWidgets
(
'support asynchronous options builder'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
fieldKey
=
GlobalKey
();
final
GlobalKey
optionsKey
=
GlobalKey
();
late
FocusNode
focusNode
;
late
TextEditingController
textEditingController
;
Iterable
<
String
>?
lastOptions
;
Duration
?
delay
;
await
tester
.
pumpWidget
(
MaterialApp
(
home:
Scaffold
(
body:
RawAutocomplete
<
String
>(
optionsBuilder:
(
TextEditingValue
textEditingValue
)
async
{
final
Iterable
<
String
>
options
=
kOptions
.
where
((
String
option
)
{
return
option
.
contains
(
textEditingValue
.
text
.
toLowerCase
());
});
if
(
delay
==
null
)
{
return
options
;
}
return
Future
<
Iterable
<
String
>>.
delayed
(
delay
,
()
=>
options
);
},
fieldViewBuilder:
(
BuildContext
context
,
TextEditingController
fieldTextEditingController
,
FocusNode
fieldFocusNode
,
VoidCallback
onFieldSubmitted
)
{
focusNode
=
fieldFocusNode
;
textEditingController
=
fieldTextEditingController
;
return
TextField
(
key:
fieldKey
,
focusNode:
focusNode
,
controller:
textEditingController
,
);
},
optionsViewBuilder:
(
BuildContext
context
,
AutocompleteOnSelected
<
String
>
onSelected
,
Iterable
<
String
>
options
)
{
lastOptions
=
options
;
return
Container
(
key:
optionsKey
);
},
),
),
)
);
// Enter text to build the options with delay.
focusNode
.
requestFocus
();
delay
=
const
Duration
(
milliseconds:
500
);
await
tester
.
enterText
(
find
.
byKey
(
fieldKey
),
'go'
);
await
tester
.
pumpAndSettle
();
// The options have not yet been built.
expect
(
find
.
byKey
(
optionsKey
),
findsNothing
);
expect
(
lastOptions
,
isNull
);
// Await asynchronous options builder.
await
tester
.
pumpAndSettle
(
delay
);
expect
(
find
.
byKey
(
optionsKey
),
findsOneWidget
);
expect
(
lastOptions
,
<
String
>[
'dingo'
,
'flamingo'
,
'goose'
]);
// Enter text to rebuild the options without delay.
delay
=
null
;
await
tester
.
enterText
(
find
.
byKey
(
fieldKey
),
'ngo'
);
await
tester
.
pump
();
expect
(
lastOptions
,
<
String
>[
'dingo'
,
'flamingo'
]);
});
testWidgets
(
'can navigate options with the keyboard'
,
(
WidgetTester
tester
)
async
{
testWidgets
(
'can navigate options with the keyboard'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
fieldKey
=
GlobalKey
();
final
GlobalKey
fieldKey
=
GlobalKey
();
final
GlobalKey
optionsKey
=
GlobalKey
();
final
GlobalKey
optionsKey
=
GlobalKey
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment