Skip to content

Instantly share code, notes, and snippets.

@g123k
Created August 26, 2022 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g123k/35b2249ecd93f374c8055444badde188 to your computer and use it in GitHub Desktop.
Save g123k/35b2249ecd93f374c8055444badde188 to your computer and use it in GitHub Desktop.
flutter_eval (tuto DevCafé)
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:dart_eval/stdlib/core.dart';
import 'package:dart_eval/dart_eval.dart';
import 'package:flutter_eval/flutter_eval.dart';
void main() {
runApp(const Example());
}
class Example extends StatefulWidget {
const Example({Key? key}) : super(key: key);
@override
State<Example> createState() => ExampleState();
}
class ExampleState extends State<Example> {
late Runtime runtime;
@override
void initState() {
super.initState();
final compiler = Compiler();
setupFlutterForCompile(compiler);
final program = compiler.compile({
'devcafe': { 'main.dart': '''
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
HomePage();
@override
Widget build(BuildContext context) {
return MaterialApp(home: Padding(
padding: EdgeInsets.all(2.3 * 5),
child: Container(
color: Colors.blue,
child: Text('Current amount: 42')
),)
);
}
}
''' }
});
final file = File('out.evc');
file.writeAsBytesSync(program.write());
print('Wrote out.evc to: ${file.absolute.uri}');
runtime = Runtime.ofProgram(program);
setupFlutterForRuntime(runtime);
runtime.setup();
}
@override
Widget build(BuildContext context) {
return CircularProgressIndicator();
}
}
import 'package:dart_eval/stdlib/core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_eval/flutter_eval.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return EvalWidget(
packages: const {
'devcafe': {
'main.dart': '''
import 'package:flutter/material.dart';
class MyApp extends StatefulWidget {
MyApp(this.name);
final String name;
@override
State<MyApp> createState() {
return MyAppState(name);
}
}
class MyAppState extends State<MyApp> {
MyAppState(this.name);
final String name;
int number = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: name,
home: Scaffold(
appBar: AppBar(
title: Text(name)
),
body: HomePage(number),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
number++;
});
},
child: Text("+")
)
)
);
}
}
class HomePage extends StatelessWidget {
HomePage(this.number);
final int number;
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(2.3 * 5),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
color: Colors.green,
child: Text('Current amount: ' + number.toString())
),
Container(
color: Colors.red,
child: Text('Some text')
),
],
)
);
}
}
'''
}
},
/// In debug mode, flutter_eval will continually re-generate a compiled
/// EVC bytecode file for the given program, and save it to the specified
/// assetPath. During runtime, it will instead load the compiled EVC file.
/// To ensure this works, you must add the file path to the assets section of
/// your pubspec.yaml file.
assetPath: 'assets/program.evc',
/// Specify which library (i.e. which file) to use as an entrypoint.
library: 'package:devcafe/main.dart',
/// Specify which function to call as the entrypoint.
/// To use a constructor, use "ClassName.constructorName" syntax. In
/// this case we are calling a default constructor so the constructor
/// name is blank.
function: 'MyApp.',
/// Specify the arguments to pass to the entrypoint. Generally these
/// should be dart_eval [$Value] objects, but when invoking a static or
/// top-level function or constructor, [int]s, [double]s, and [bool]s
/// should be passed directly.
args: [$String('Dev Café')],
);
}
}
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:dart_eval/stdlib/core.dart';
import 'package:dart_eval/dart_eval.dart';
import 'package:flutter_eval/flutter_eval.dart';
void main() {
runApp(const Example());
}
class Example extends StatefulWidget {
const Example({Key? key}) : super(key: key);
@override
State<Example> createState() => ExampleState();
}
class ExampleState extends State<Example> {
Runtime? runtime;
@override
void initState() {
super.initState();
File('out.evc').readAsBytes().then((bytecode) =>
setState(() {
runtime = Runtime(ByteData.sublistView(bytecode));
setupFlutterForRuntime(runtime!);
runtime!.setup();
}));
}
@override
Widget build(BuildContext context) {
if (runtime == null) return CircularProgressIndicator();
return (runtime!.executeLib(
'package:devcafe/main.dart', 'HomePage.').$value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment