mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
add js module
This commit is contained in:
8
example/js/hello.js
Normal file
8
example/js/hello.js
Normal file
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* @Description: module example
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-27 19:06:30
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-27 20:39:11
|
||||
*/
|
||||
export default (name) => `hello ${name}!`;
|
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-01 13:20:06
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-08 17:52:22
|
||||
*/
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'highlight.dart';
|
||||
|
||||
class CodeEditor extends StatefulWidget {
|
||||
final void Function(String) onChanged;
|
||||
|
||||
const CodeEditor({Key key, this.onChanged}) : super(key: key);
|
||||
@override
|
||||
_CodeEditorState createState() => _CodeEditorState();
|
||||
}
|
||||
|
||||
class _CodeEditorState extends State<CodeEditor> {
|
||||
CodeInputController _controller = CodeInputController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextField(
|
||||
autofocus: true,
|
||||
controller: _controller,
|
||||
textCapitalization: TextCapitalization.none,
|
||||
decoration: null,
|
||||
maxLines: null,
|
||||
onChanged: this.widget.onChanged,
|
||||
);
|
||||
}
|
||||
}
|
@@ -3,15 +3,16 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-08 08:16:51
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-24 22:26:03
|
||||
* @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 'code/editor.dart';
|
||||
import 'highlight.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
@@ -44,9 +45,11 @@ class TestPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _TestPageState extends State<TestPage> {
|
||||
String code, resp;
|
||||
String resp;
|
||||
FlutterJs engine;
|
||||
|
||||
CodeInputController _controller = CodeInputController();
|
||||
|
||||
_createEngine() async {
|
||||
if (engine != null) return;
|
||||
engine = FlutterJs();
|
||||
@@ -72,6 +75,10 @@ class _TestPageState extends State<TestPage> {
|
||||
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
|
||||
@@ -89,8 +96,7 @@ class _TestPageState extends State<TestPage> {
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
children: [
|
||||
FlatButton(
|
||||
child: Text("create engine"), onPressed: _createEngine),
|
||||
FlatButton(child: Text("create engine"), onPressed: _createEngine),
|
||||
FlatButton(
|
||||
child: Text("evaluate"),
|
||||
onPressed: () async {
|
||||
@@ -99,8 +105,8 @@ class _TestPageState extends State<TestPage> {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
resp = (await engine.evaluate(code ?? '', "<eval>"))
|
||||
.toString();
|
||||
resp =
|
||||
(await engine.evaluate(_controller.text ?? '', "<eval>")).toString();
|
||||
} catch (e) {
|
||||
resp = e.toString();
|
||||
}
|
||||
@@ -120,10 +126,12 @@ class _TestPageState extends State<TestPage> {
|
||||
padding: const EdgeInsets.all(12),
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
constraints: BoxConstraints(minHeight: 200),
|
||||
child: CodeEditor(
|
||||
onChanged: (v) {
|
||||
code = v;
|
||||
},
|
||||
child: TextField(
|
||||
autofocus: true,
|
||||
controller: _controller,
|
||||
decoration: null,
|
||||
expands: true,
|
||||
maxLines: null
|
||||
),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
|
@@ -75,7 +75,7 @@ packages:
|
||||
path: ".."
|
||||
relative: true
|
||||
source: path
|
||||
version: "0.0.4"
|
||||
version: "0.0.5"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
|
@@ -40,7 +40,8 @@ flutter:
|
||||
uses-material-design: true
|
||||
|
||||
# To add assets to your application, add an assets section, like this:
|
||||
# assets:
|
||||
assets:
|
||||
- js/
|
||||
# - images/a_dot_burr.jpeg
|
||||
# - images/a_dot_ham.jpeg
|
||||
|
||||
|
Reference in New Issue
Block a user