flutter_lua 0.3.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

Flutter Lua Plugin #

Project license Pub package Dartdoc reference Travis CI build status

This is a Flutter plugin that embeds a Lua interpreter and runtime for executing dynamic scripts from Flutter apps.

Features #

  • Embeds a Lua 5.2 interpreter into your Flutter app.

  • Executes Lua code on a background thread (not on the main UI thread).

  • Supports executing source code snippets from strings as well as from source files bundled in your app's asset bundle.

Compatibility #

Android and iOS both.

Examples #

Checking the Lua runtime version #

import 'package:flutter_lua/flutter_lua.dart' show Lua;

print(await Lua.version);

Spawning a new Lua interpreter thread #

import 'package:flutter_lua/flutter_lua.dart' show LuaThread;

var thread = await LuaThread.spawn();

Evaluating a Lua code snippet #

import 'package:flutter_lua/flutter_lua.dart' show LuaThread;

var thread = await LuaThread.spawn();

await thread.eval("return 6 * 7"); //=> 42.0

Executing a bundled Lua source file #

import 'package:flutter_lua/flutter_lua.dart' show LuaThread;

var thread = await LuaThread.spawn();

await thread.execAsset("scripts/myscript.lua");

Frequently Asked Questions #

How much does using this plugin increase my final app size? #

About 3.8 MiB, at present.

Caveats #

  • Currently the only supported result datatypes from LuaThread#eval*() methods are booleans, floating-point numbers (doubles), and strings. This will be extended further over the next releases.

Changelog #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

0.3.1 - 2019-04-24 #

Changed #

  • Improved the example app

0.3.0 - 2019-03-14 #

Added #

  • Added initial iOS platform support (#1)
  • Lua.eval() static method
  • Lua.evalAsset() static method
  • Lua.evalFile() static method

Removed #

  • Lua.doString() method (use Lua.eval())
  • Lua.doFile() method (use Lua.evalFile())

0.2.0 - 2019-03-10 #

Added #

  • LuaError exception class

Changed #

  • Implemented PlatformException wrapping

0.1.0 - 2019-02-27 #

Added #

  • Lua.version static getter
  • LuaThread#eval() method
  • LuaThread#evalAsset() method
  • LuaThread#evalFile() method
  • LuaThread#exec() method
  • LuaThread#execAsset() method
  • LuaThread#execFile() method

example/lib/main.dart

/* This is free and unencumbered software released into the public domain. */

import 'dart:async' show Future;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_lua/flutter_lua.dart' show Lua, LuaThread;

void main() async {
  final version = await Lua.version;
  print("Lua $version");

  final thread = await LuaThread.spawn();
  print(await thread.eval('return 6*7'));

  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _codeController = TextEditingController(text: "return 6*7");
  String _luaVersion = "Unknown";
  LuaThread _luaThread;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String luaVersion;
    LuaThread luaThread;

    // Platform messages may fail, so we catch platform exceptions:
    try {
      luaVersion = await Lua.version;
      luaThread = await LuaThread.spawn();
    } on PlatformException {
      luaVersion = "Failed to get platform version.";
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance:
    if (!mounted) return;

    setState(() {
      _luaVersion = luaVersion;
      _luaThread = luaThread;
    });
  }

  @override
  Widget build(final BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Flutter Lua plugin"),
        ),
        body: Builder(
          builder: (final BuildContext context) {
            return Center(
              child: ListView(
                shrinkWrap: true,
                padding: EdgeInsets.only(left: 24, right: 24),
                children: <Widget>[
                  Center(child: Text("Lua $_luaVersion")),
                  SizedBox(height: 8),
                  TextFormField(
                    controller: _codeController,
                    maxLines: 10,
                    autofocus: true,
                    autocorrect: false,
                    keyboardType: TextInputType.multiline,
                    decoration: InputDecoration(
                      hintText: "Your code",
                      contentPadding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                      border: OutlineInputBorder(borderRadius: BorderRadius.circular(16)),
                    ),
                  ),
                  SizedBox(height: 8),
                  RaisedButton(
                    child: Text("Evaluate", style: TextStyle(color: Colors.white)),
                    onPressed: (_luaThread != null) ? () => _evaluate(context) : null,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(24),
                    ),
                    padding: EdgeInsets.all(12),
                    color: Colors.blueAccent,
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }

  void _evaluate(final BuildContext context) async {
    final result = await _luaThread.eval(_codeController.text);
    Scaffold.of(context).showSnackBar(
      SnackBar(
        content: Text(result.toString(), textAlign: TextAlign.center),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_lua: ^0.3.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_lua/flutter_lua.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
33
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
meta ^1.1.6 1.1.8
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
test any