flutter_js 0.2.0+0 flutter_js: ^0.2.0+0 copied to clipboard
A Javascript engine to use with flutter. It uses Quickjs on Android and JavascriptCore on IOS
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_js/flutter_js.dart';
import 'package:flutter_js_example/ajv_example.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
GlobalKey<ScaffoldState> scaffoldState = GlobalKey();
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FlutterJsHomeScreen(),
);
}
}
class FlutterJsHomeScreen extends StatefulWidget {
@override
_FlutterJsHomeScreenState createState() => _FlutterJsHomeScreenState();
}
class _FlutterJsHomeScreenState extends State<FlutterJsHomeScreen> {
String _jsResult = '';
JavascriptRuntime javascriptRuntime;
@override
void initState() {
super.initState();
javascriptRuntime = getJavascriptRuntime();
javascriptRuntime.onMessage('ConsoleLog2', (args) {
print('ConsoleLog2 (Dart Side): $args');
return json.encode(args);
});
}
@override
dispose() {
super.dispose();
javascriptRuntime.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('FlutterJS Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('JS Evaluate Result: $_jsResult\n'),
SizedBox(
height: 20,
),
Padding(
padding: EdgeInsets.all(10),
child: Text(
'Click on the big JS Yellow Button to evaluate the expression bellow using the flutter_js plugin'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Math.trunc(Math.random() * 100).toString();",
style: TextStyle(
fontSize: 12,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold),
),
),
RaisedButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (ctx) => AjvExample(
javascriptRuntime,
),
),
),
child: const Text('See Ajv Example'),
),
],
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.transparent,
child: Image.asset('assets/js.ico'),
onPressed: () async {
try {
String jsResult = javascriptRuntime.evaluate("""
(async function() {
globalThis.objSendMsg1 = await sendMessage('ConsoleLog2', JSON.stringify(['info', 'message']));
console.log('OBJ: ', objSendMsg1);
})();Math.trunc(Math.random() * 100).toString();""").stringResult;
setState(() {
_jsResult = jsResult;
});
} on PlatformException catch (e) {
print('ERRO: ${e.details}');
}
},
),
);
}
}