forked from ekibun/flutter_qjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
154 lines (144 loc) · 4.43 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* @Description: example
* @Author: ekibun
* @Date: 2020-08-08 08:16:51
* @LastEditors: ekibun
* @LastEditTime: 2020-08-27 20:39:32
*/
import 'package:flutter/material.dart';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:flutter_qjs/flutter_qjs.dart';
import 'highlight.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter_qjs',
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: AppBarTheme(brightness: Brightness.dark, elevation: 0),
backgroundColor: Colors.grey[300],
primaryColorBrightness: Brightness.dark,
),
routes: {
'home': (BuildContext context) => TestPage(),
},
initialRoute: 'home',
);
}
}
class TestPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _TestPageState();
}
class _TestPageState extends State<TestPage> {
String resp;
FlutterJs engine;
CodeInputController _controller = CodeInputController();
_createEngine() async {
if (engine != null) return;
engine = FlutterJs();
await engine.setMethodHandler((String method, List arg) async {
switch (method) {
case "http":
Response response = await Dio().get(arg[0]);
return response.data;
case "test":
return await arg[0]([
true,
1,
0.5,
"str",
{"key": "val", 0: 1},
Uint8List(2),
Int32List(2),
Int64List(2),
Float64List(2),
Float32List(2)
]);
default:
return JsMethodHandlerNotImplement();
}
});
await engine.setModuleHandler((String module) async {
if (module == "test") return "export default '${new DateTime.now()}'";
return await rootBundle.loadString(
"js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("JS engine test"),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
FlatButton(
child: Text("create engine"), onPressed: _createEngine),
FlatButton(
child: Text("evaluate"),
onPressed: () async {
if (engine == null) {
print("please create engine first");
return;
}
try {
resp = (await engine.evaluate(
_controller.text ?? '', "<eval>"))
.toString();
} catch (e) {
resp = e.toString();
}
setState(() {});
}),
FlatButton(
child: Text("close engine"),
onPressed: () async {
if (engine == null) return;
await engine.destroy();
engine = null;
}),
],
),
),
Container(
padding: const EdgeInsets.all(12),
color: Colors.grey.withOpacity(0.1),
constraints: BoxConstraints(minHeight: 200),
child: TextField(
autofocus: true,
controller: _controller,
decoration: null,
expands: true,
maxLines: null),
),
SizedBox(height: 16),
Text("result:"),
SizedBox(height: 16),
Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
color: Colors.green.withOpacity(0.05),
constraints: BoxConstraints(minHeight: 100),
child: Text(resp ?? ''),
),
],
),
),
);
}
}