-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathmain.dart
134 lines (121 loc) · 3.9 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_isolate/flutter_isolate.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
@pragma('vm:entry-point')
void isolate2(String arg) {
getTemporaryDirectory().then((dir) async {
print("isolate2 temporary directory: $dir");
await FlutterDownloader.initialize(debug: true);
FlutterDownloader.registerCallback(AppWidget.downloaderCallback);
final taskId = await FlutterDownloader.enqueue(
url:
"https://raw.githubusercontent.com/rmawatson/flutter_isolate/master/README.md",
savedDir: dir.path);
});
Timer.periodic(
Duration(seconds: 1), (timer) => print("Timer Running From Isolate 2"));
}
@pragma('vm:entry-point')
void isolate1(String arg) async {
await FlutterIsolate.spawn(isolate2, "hello2");
getTemporaryDirectory().then((dir) {
print("isolate1 temporary directory: $dir");
});
Timer.periodic(
Duration(seconds: 1), (timer) => print("Timer Running From Isolate 1"));
}
@pragma('vm:entry-point')
void computeFunction(String arg) async {
getTemporaryDirectory().then((dir) {
print("Temporary directory in compute function : $dir with arg $arg");
});
}
void main() async {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: AppWidget()));
}
}
class AppWidget extends StatelessWidget {
static void downloaderCallback(String id, int status, int progress) {
print("progress: $progress");
}
Future<void> _run() async {
print(
"Temp directory in main isolate : ${(await getTemporaryDirectory()).path}");
final isolate = await FlutterIsolate.spawn(isolate1, "hello");
Timer(Duration(seconds: 5), () {
print("Pausing Isolate 1");
isolate.pause();
});
Timer(Duration(seconds: 10), () {
print("Resuming Isolate 1");
isolate.resume();
});
Timer(Duration(seconds: 20), () {
print("Killing Isolate 1");
isolate.kill();
});
}
@override
Widget build(BuildContext context) {
return Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
ElevatedButton(
child: Text('Spawn isolates'),
onPressed: _run,
),
ElevatedButton(
child: Text('Check running isolates'),
onPressed: () async {
final isolates = await FlutterIsolate.runningIsolates;
await showDialog(
builder: (ctx) {
return Center(
child: Container(
color: Colors.white,
padding: EdgeInsets.all(5),
child: Column(
children: isolates
.map((i) => Text(i))
.cast<Widget>()
.toList() +
[
ElevatedButton(
child: Text("Close"),
onPressed: () {
Navigator.of(ctx).pop();
})
])));
},
context: context);
},
),
ElevatedButton(
child: Text('Kill all running isolates'),
onPressed: () async {
await FlutterIsolate.killAll();
},
),
ElevatedButton(
child: Text('Run in compute function'),
onPressed: () async {
await flutterCompute(computeFunction, "foo");
},
),
]);
}
}