-
Notifications
You must be signed in to change notification settings - Fork 2
/
search_compute.dart
60 lines (53 loc) · 1.75 KB
/
search_compute.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import 'dart:math';
import 'package:compute/compute.dart';
import 'package:flutter/material.dart';
List<Map<String, dynamic>> list = []; // computedData
List<Map<String, dynamic>> filteredList = [];
TextEditingController inputController = TextEditingController();
bool searching = false;
void main() async {
inputController.addListener(searchFromCompute);
await emulateTextInput();
inputController.removeListener(searchFromCompute);
}
Future<void> searchFromCompute() async {
searching = true;
final input = inputController.text;
if (input.isNotEmpty && list.isNotEmpty) {
await compute(() {
filteredList.clear();
filteredList.addAll(list.where((Map<String, dynamic> map) {
return map.values.where((e) => e.value.contains(input)).isNotEmpty;
}).toList());
} as ComputeCallback<void, dynamic>, null);
}
searching = false;
}
Future<void> emulateTextInput() async {
List<String> words = [];
for (int i = 0; i < list.length; i++) {
words.addAll(list[i].values.map((e) => e.value as String).toSet().toList());
}
words = words.map((String word) => word.substring(0, min(word.length, 3))).toSet().take(3).toList();
for (var word in words) {
final List<String> letters = word.split('');
String search = '';
for (String letter in letters) {
search += letter;
await inputText(search);
}
while (search.isNotEmpty) {
search = search.substring(0, search.length - 1);
await inputText(search);
}
}
}
Future<void> inputText(String word) async {
if (!searching) {
await Future.delayed(const Duration(milliseconds: 500));
await inputText(word);
} else {
inputController.value = TextEditingValue(text: word);
await Future.delayed(const Duration(milliseconds: 500));
}
}