flutter_turtle 0.3.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 72

flutter_turtle #

pub package

flutter_turtle is a simple implementation of turtle graphics for Flutter. It simply uses a custom painter to draw graphics by a series of Logo-like commands.

For further information about turtle graphics, please visit Wikipedia:

Why I make this? #

It is always fun to make your own DSL!

Example #

screenshot.png

A quick example:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: TurtleView(
        child: Container(),
        commands: [
          PenDown(),
          SetColor((_) => Color(0xffff9933)),
          SetStrokeWidth((_) => 2),
          Repeat((_) => 20, [
            Repeat((_) => 180, [
              Forward((_) => 25.0),
              Right((_) => 20),
            ]),
            Right((_) => 18),
          ]),
          PenUp(),
        ],
      ),
    );
  }

A Flutter web app example is available at https://zonble.github.io/flutter_turtle/

Usage #

flutter_turtle provides two class, TurtleView, which is your canvas and accepts a list of commands to draw your graphics, and AnimatedTurtleView, which is an animating version of TurtleView.

Just create an instance of TurtleView or AnimatedTurtleView, pass the commands in the commands parameter, and insert it to your widget tree.

Flutter Turtle DSL #

flutter_turtle a set of Dart classes to represents commands to control your turtle. Using them is quite alike to calling functions when you are doing turtle graphics in Logo language, however, you are still coding in Dart and actually composing a list of Dart objects, and then TurtleView runs them one by another.

In other words, flutter_turtle built a DSL over Dart and Flutter.

If you have code in Logo like this:

It would be like the following code in Flutter Turtle DSL:

[
  Repeat((_) => 5, [
    Forward((_) => 200),
    Right((_) => 144),
  ]),
];

Flow control #

There are some commands help you to do flow controls. You can use the IfElse class like using 'if..else..' in Dart, and you can use Repeat for loops.

An example of IfElse:

// If it is true, go forward for 10 pixels, otherwise go back for 10 pixels.
IfElse((_)=>true, [Forward((_)=>10.0)], [Back(()=>10.0)]),

An example of Repeat:

// Repeat 10 times.
Repeat((_)=>10, [Forward((_)=>10.0)]),

Macros #

Flutter Turtle DSL does not have somehow REAL functions, but somehow you can use macros instead. You can use the SetMacro to add macros to your turtle:

SetMacro('macro', [Forward((_)=>10.0)]), // A new macro named "macro".

Then you can use RunMacro to run the macro:

RunMacro('macro', (_)=>{'arg1': 'value1', 'arg2': 'value2'}),

You can pass arguments while calling a macro. These arguments would be a map sent to each Flutter Turtle command. For example:

SetMacro('macro', [Forward((_)=>_['arg1'])]), // A new macro named "macro".
RunMacro('macro', (_)=>{'arg1': 10.0}),

Commands #

Currently supported commands are including:

Turtle motion #

  • PenDown
  • PenUp
  • Left
  • Right
  • Forward
  • Back
  • SetColor
  • SetStrokeWidth
  • GoTo
  • ResetPosition
  • ResetHeading
  • Label
  • SetLabelHeight

Flow control #

  • If
  • IfElse
  • Repeat

Macros #

  • SetMacro
  • RunMacro

0.3.4 #

  • Adds a new command Log.

0.3.3 #

  • Adds a new class ControllableTurtleView.

0.3.2 #

  • Adds a new command If.
  • Prevents null exceptions.

0.3.1 #

  • Adds new commands: Label and SetLabelHeight.

0.3.0 #

  • Adds AnimatedTurtleView.

0.2.1 #

  • Updates documentation.

0.2.0 #

  • Adds new commands like Back, Exec, IfElse and so on.
  • Supports macros.

0.1.0 #

  • The first release

example/lib/main.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'pages/clock2_page.dart';
import 'pages/clock_page.dart';
import 'pages/fern_page.dart';
import 'pages/flower_page.dart';
import 'pages/label.dart';
import 'pages/logo_controller_page.dart';
import 'pages/logo_page.dart';
import 'pages/red_square_page.dart';
import 'pages/snowflake_page.dart';
import 'pages/spiral2page.dart';
import 'pages/spiralpage.dart';
import 'pages/star_page.dart';
import 'pages/tree_page.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(title: 'Examples'),
      routes: {
        'label': (_) => LabelPage(),
        'logo': (_) => LogoPage(),
        'logo_controller': (_) => LogoControllerPage(),
        'star': (_) => StarPage(),
        'flower': (_) => FlowerPage(),
        'red_square': (_) => RedSquarePage(),
        'tree': (_) => TreePage(),
        'fern': (_) => FernPage(),
        'clock1': (_) => ClockPage(),
        'clock2': (_) => Clock2Page(),
        'spiral': (_) => SpiralPage(),
        'spiral2': (_) => Spiral2Page(),
        'snowflake': (_) => SnowflakePage(),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    var tiles = <Widget>[
      MyTile(title: 'Logo', image: 'images/1.png', pageName: 'logo'),
      MyTile(
          title: 'Logo with Controller',
          image: 'images/1.png',
          pageName: 'logo_controller'),
      MyTile(title: 'Star', image: 'images/2.png', pageName: 'star'),
      MyTile(title: 'Flower', image: 'images/3.png', pageName: 'flower'),
      MyTile(
          title: 'Red Suqares', image: 'images/4.png', pageName: 'red_square'),
      MyTile(title: 'Tree', image: 'images/5.png', pageName: 'tree'),
      MyTile(title: 'Fern', image: 'images/6.png', pageName: 'fern'),
      MyTile(title: 'Label', image: 'images/7.png', pageName: 'label'),
      MyTile(title: 'Clock', image: 'images/8.png', pageName: 'clock1'),
      MyTile(title: 'Clock', image: 'images/9.png', pageName: 'clock2'),
      MyTile(title: 'Spiral', image: 'images/10.png', pageName: 'spiral'),
      MyTile(title: 'Spiral', image: 'images/11.png', pageName: 'spiral2'),
      MyTile(title: 'Snowflake', image: 'images/12.png', pageName: 'snowflake'),
    ];
    return Scaffold(
        appBar: AppBar(title: Text(widget.title)),
        body: SingleChildScrollView(
            child: SafeArea(
                child: Padding(
                    padding: const EdgeInsets.only(top: 10.0, bottom: 10),
                    child: Center(
                      child: Wrap(children: tiles),
                    )))));
  }
}

class MyTile extends StatelessWidget {
  final String title;
  final String pageName;
  final String image;

  const MyTile({
    Key key,
    this.title,
    this.pageName,
    this.image,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) => Card(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(10))),
        child: InkWell(
          onTap: () => Navigator.of(context).pushNamed(this.pageName),
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Column(
              children: <Widget>[
                Container(
                    width: 150,
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.black26),
                    ),
                    child: Image.asset(this.image)),
                SizedBox(height: 10),
                Text(title),
                SizedBox(height: 10),
              ],
            ),
          ),
        ),
      );
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_turtle: ^0.3.4

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_turtle/flutter_turtle.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
45
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]
72
Learn more about scoring.

We analyzed this package on Apr 7, 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

Health suggestions

Format lib/src/commands/set_label_height.dart.

Run flutter format to format lib/src/commands/set_label_height.dart.

Format lib/src/turtle_view.dart.

Run flutter format to format lib/src/turtle_view.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
meta ^1.1.8 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