forked from What-the-Flutter/Isolate-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_isolate.dart
70 lines (62 loc) · 1.97 KB
/
search_isolate.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
61
62
63
64
65
66
67
68
69
70
import 'dart:isolate';
import 'dart:math';
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(searchFromIsolate);
await emulateTextInput();
inputController.removeListener(searchFromIsolate);
}
Future<void> searchFromIsolate() async {
searching = true;
final ReceivePort receivePort = ReceivePort();
final isolate = await Isolate.spawn(filterDataIsolate, receivePort.sendPort);
await for (var response in receivePort) {
if (response == null) {
break;
}
if (response is List<Map<String, dynamic>>) {
filteredList = response;
}
}
searching = false;
}
void filterDataIsolate(SendPort sendPort) {
final input = inputController.text;
final filteredData = list.where((Map<String, dynamic> map) {
return map.values.any((e) => e.toString().contains(input));
}).toList();
sendPort.send(filteredData);
sendPort.send(null);
}
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));
}
}