@@ -1288,4 +1288,274 @@ void main() {
12881288 await tester.pump ();
12891289 expect (find.byKey (optionsKey), findsNothing);
12901290 });
1291+
1292+ testWidgets ('can prevent older optionsBuilder results from replacing the new ones' , (WidgetTester tester) async {
1293+ final GlobalKey fieldKey = GlobalKey ();
1294+ final GlobalKey optionsKey = GlobalKey ();
1295+ late FocusNode focusNode;
1296+ late TextEditingController textEditingController;
1297+ Iterable <String >? lastOptions;
1298+ Duration ? delay;
1299+
1300+ await tester.pumpWidget (
1301+ MaterialApp (
1302+ home: Scaffold (
1303+ body: RawAutocomplete <String >(
1304+ optionsBuilder: (TextEditingValue textEditingValue) async {
1305+ final Iterable <String > options = kOptions.where ((String option) {
1306+ return option.contains (textEditingValue.text.toLowerCase ());
1307+ });
1308+ if (delay == null ) {
1309+ return options;
1310+ }
1311+ return Future <Iterable <String >>.delayed (delay, () => options);
1312+ },
1313+ fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1314+ focusNode = fieldFocusNode;
1315+ textEditingController = fieldTextEditingController;
1316+ return TextField (
1317+ key: fieldKey,
1318+ focusNode: focusNode,
1319+ controller: textEditingController,
1320+ );
1321+ },
1322+ optionsViewBuilder: (BuildContext context, AutocompleteOnSelected <String > onSelected, Iterable <String > options) {
1323+ lastOptions = options;
1324+ return Container (key: optionsKey);
1325+ },
1326+ ),
1327+ ),
1328+ )
1329+ );
1330+
1331+ const Duration longRequestDelay = Duration (milliseconds: 5000 );
1332+ const Duration shortRequestDelay = Duration (milliseconds: 1000 );
1333+ focusNode.requestFocus ();
1334+
1335+ // Enter the first letter.
1336+ delay = longRequestDelay;
1337+ await tester.enterText (find.byKey (fieldKey), 'c' );
1338+ await tester.pump ();
1339+ expect (lastOptions, null );
1340+
1341+ // Enter the second letter which resolves faster.
1342+ delay = shortRequestDelay;
1343+ await tester.enterText (find.byKey (fieldKey), 'ch' );
1344+ await tester.pump ();
1345+ expect (lastOptions, null );
1346+
1347+ // Wait for the short request to resolve.
1348+ await tester.pump (shortRequestDelay);
1349+
1350+ // lastOptions must contain results from the last request.
1351+ expect (find.byKey (optionsKey), findsOneWidget);
1352+ expect (lastOptions, < String > ['chameleon' ]);
1353+
1354+ // Wait for the last timer to finish.
1355+ await tester.pump (longRequestDelay - shortRequestDelay);
1356+ expect (lastOptions, < String > ['chameleon' ]);
1357+ });
1358+
1359+ testWidgets ('updates result only from the last call made to optionsBuilder' , (WidgetTester tester) async {
1360+ final GlobalKey fieldKey = GlobalKey ();
1361+ final GlobalKey optionsKey = GlobalKey ();
1362+ late FocusNode focusNode;
1363+ late TextEditingController textEditingController;
1364+ Iterable <String >? lastOptions;
1365+ Duration ? delay;
1366+
1367+ await tester.pumpWidget (
1368+ MaterialApp (
1369+ home: Scaffold (
1370+ body: RawAutocomplete <String >(
1371+ optionsBuilder: (TextEditingValue textEditingValue) async {
1372+ final Iterable <String > options = kOptions.where ((String option) {
1373+ return option.contains (textEditingValue.text.toLowerCase ());
1374+ });
1375+ if (delay == null ) {
1376+ return options;
1377+ }
1378+ return Future <Iterable <String >>.delayed (delay, () => options);
1379+ },
1380+ fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1381+ focusNode = fieldFocusNode;
1382+ textEditingController = fieldTextEditingController;
1383+ return TextField (
1384+ key: fieldKey,
1385+ focusNode: focusNode,
1386+ controller: textEditingController,
1387+ );
1388+ },
1389+ optionsViewBuilder: (BuildContext context, AutocompleteOnSelected <String > onSelected, Iterable <String > options) {
1390+ lastOptions = options;
1391+ return Container (key: optionsKey);
1392+ },
1393+ ),
1394+ ),
1395+ )
1396+ );
1397+
1398+ focusNode.requestFocus ();
1399+ const Duration firstRequestDelay = Duration (milliseconds: 1000 );
1400+ const Duration secondRequestDelay = Duration (milliseconds: 2000 );
1401+ const Duration thirdRequestDelay = Duration (milliseconds: 3000 );
1402+
1403+ // Enter the first letter.
1404+ delay = firstRequestDelay;
1405+ await tester.enterText (find.byKey (fieldKey), 'l' );
1406+ await tester.pump ();
1407+ expect (lastOptions, null );
1408+
1409+ // Enter the second letter which resolves slower.
1410+ delay = secondRequestDelay;
1411+ await tester.enterText (find.byKey (fieldKey), 'le' );
1412+ await tester.pump ();
1413+ expect (lastOptions, null );
1414+
1415+ // Enter the third letter which resolves the slowest.
1416+ delay = thirdRequestDelay;
1417+ await tester.enterText (find.byKey (fieldKey), 'lem' );
1418+ await tester.pump ();
1419+ expect (lastOptions, null );
1420+
1421+ // lastOptions should get updated only from the last request.
1422+ await tester.pump (firstRequestDelay);
1423+ expect (find.byKey (optionsKey), findsNothing);
1424+ expect (lastOptions, null );
1425+
1426+ await tester.pump (secondRequestDelay - firstRequestDelay);
1427+ expect (find.byKey (optionsKey), findsNothing);
1428+ expect (lastOptions, null );
1429+
1430+ await tester.pump (thirdRequestDelay - secondRequestDelay);
1431+ expect (find.byKey (optionsKey), findsOneWidget);
1432+ expect (lastOptions, < String > ['lemur' ]);
1433+ });
1434+
1435+ testWidgets ('update options view when field input changes return to the starting keyword' , (WidgetTester tester) async {
1436+ final GlobalKey fieldKey = GlobalKey ();
1437+ final GlobalKey optionsKey = GlobalKey ();
1438+ late FocusNode focusNode;
1439+ late TextEditingController textEditingController;
1440+ Iterable <String >? lastOptions;
1441+ const Duration delay = Duration (milliseconds: 100 );
1442+
1443+ await tester.pumpWidget (
1444+ MaterialApp (
1445+ home: Scaffold (
1446+ body: RawAutocomplete <String >(
1447+ optionsBuilder: (TextEditingValue textEditingValue) async {
1448+ final Iterable <String > options = kOptions.where ((String option) {
1449+ return option.contains (textEditingValue.text.toLowerCase ());
1450+ });
1451+ return Future <Iterable <String >>.delayed (delay, () => options);
1452+ },
1453+ fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1454+ focusNode = fieldFocusNode;
1455+ textEditingController = fieldTextEditingController;
1456+ return TextField (
1457+ key: fieldKey,
1458+ focusNode: focusNode,
1459+ controller: textEditingController,
1460+ );
1461+ },
1462+ optionsViewBuilder: (BuildContext context, AutocompleteOnSelected <String > onSelected, Iterable <String > options) {
1463+ lastOptions = options;
1464+ return Container (key: optionsKey);
1465+ },
1466+ ),
1467+ ),
1468+ )
1469+ );
1470+
1471+ // Setup starting point.
1472+ await tester.enterText (find.byKey (fieldKey), 'ele' );
1473+ await tester.pump (delay);
1474+ expect (lastOptions, < String > ['chameleon' , 'elephant' ]);
1475+ expect (find.byKey (optionsKey), findsOneWidget);
1476+
1477+ // Hide options.
1478+ await tester.sendKeyEvent (LogicalKeyboardKey .escape);
1479+ await tester.pump ();
1480+ expect (find.byKey (optionsKey), findsNothing);
1481+
1482+ // Enter another letter and then immediately erase it.
1483+ await tester.enterText (find.byKey (fieldKey), 'eleo' );
1484+ await tester.pump ();
1485+ expect (find.byKey (optionsKey), findsNothing);
1486+
1487+ await tester.enterText (find.byKey (fieldKey), 'ele' );
1488+ await tester.pump (delay);
1489+ expect (lastOptions, < String > ['chameleon' , 'elephant' ]);
1490+
1491+ // Options dropdown should be visible after the last optionsBuilder
1492+ // call is resolved.
1493+ expect (find.byKey (optionsKey), findsOneWidget);
1494+ });
1495+
1496+ testWidgets ('optionsBuilder does not have to be a pure function' , (WidgetTester tester) async {
1497+ final GlobalKey fieldKey = GlobalKey ();
1498+ final GlobalKey optionsKey = GlobalKey ();
1499+ late FocusNode focusNode;
1500+ late TextEditingController textEditingController;
1501+ Iterable <String >? lastOptions;
1502+ const Duration delay = Duration (milliseconds: 100 );
1503+
1504+ // This is used to tell optionsBuilder to return something different after
1505+ // being called with "ele" the second time. I.e. it is not a pure function.
1506+ int timesOptionsBuilderCalledWithEle = 0 ;
1507+ final Iterable <String > altEleOptions = < String > ['something new and crazy for ele!' ];
1508+
1509+ await tester.pumpWidget (
1510+ MaterialApp (
1511+ home: Scaffold (
1512+ body: RawAutocomplete <String >(
1513+ optionsBuilder: (TextEditingValue textEditingValue) async {
1514+ if (textEditingValue.text == 'ele' ) {
1515+ timesOptionsBuilderCalledWithEle += 1 ;
1516+ if (timesOptionsBuilderCalledWithEle > 1 ) {
1517+ return Future <Iterable <String >>.delayed (delay, () => altEleOptions);
1518+ }
1519+ }
1520+ final Iterable <String > options = kOptions.where ((String option) {
1521+ return option.contains (textEditingValue.text.toLowerCase ());
1522+ });
1523+ return Future <Iterable <String >>.delayed (delay, () => options);
1524+ },
1525+ fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1526+ focusNode = fieldFocusNode;
1527+ textEditingController = fieldTextEditingController;
1528+ return TextField (
1529+ key: fieldKey,
1530+ focusNode: focusNode,
1531+ controller: textEditingController,
1532+ );
1533+ },
1534+ optionsViewBuilder: (BuildContext context, AutocompleteOnSelected <String > onSelected, Iterable <String > options) {
1535+ lastOptions = options;
1536+ return Container (key: optionsKey);
1537+ },
1538+ ),
1539+ ),
1540+ )
1541+ );
1542+
1543+ await tester.enterText (find.byKey (fieldKey), 'ele' );
1544+ await tester.pump (delay);
1545+ expect (lastOptions, < String > ['chameleon' , 'elephant' ]);
1546+ expect (find.byKey (optionsKey), findsOneWidget);
1547+
1548+ await tester.sendKeyEvent (LogicalKeyboardKey .escape);
1549+ await tester.pump ();
1550+ expect (find.byKey (optionsKey), findsNothing);
1551+
1552+ await tester.enterText (find.byKey (fieldKey), 'eleo' );
1553+ await tester.pump ();
1554+ expect (find.byKey (optionsKey), findsNothing);
1555+
1556+ await tester.enterText (find.byKey (fieldKey), 'ele' );
1557+ await tester.pump (delay);
1558+ expect (lastOptions, altEleOptions);
1559+ expect (find.byKey (optionsKey), findsOneWidget);
1560+ });
12911561}
0 commit comments