搜索插件、代码仓与 APP 分类

flutter_turtle 封面
插件详情v0.6.0

flutter_turtle

flutter_turtle 是一个简单的海龟绘图实现。它通过一系列类似 Logo 的命令,使用自定义绘制器将图形绘制到小部件中。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
117
喜欢数
50
Pub 分数
150

安装插件

flutter pub add flutter_turtle

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

flutter_turtle 是一个轻量级的 Flutter 包,将海龟绘图引入 Dart 生态系统,使用户能够使用简单、类似 Logo 的命令绘制形状和图案。它利用自定义绘图器在小部件内直接渲染图形,非常适合教育工具、可视化编程实验和创意编程项目。该包支持所有主要平台,包括移动设备、网页和桌面端,确保广泛的兼容性。其直观的 API 使初学者和教育工作者能够通过视觉反馈教授编程概念。

适合场景

  • 教授编程基础
  • 用代码创建视觉艺术
  • 儿童教育应用
  • 交互式编码教程
  • 生成几何图案

主要特点

  • 类 Logo 命令语法
  • 基于自定义绘制器的渲染
  • 跨平台支持
  • 面向初学者的简单 API
  • 实时绘图更新

适合对象

  • 教授编程的教育工作者
  • 初学者程序员
  • 创意编码者
  • 儿童学习编程
  • 可视化编程实验

使用建议

  • 限制为2D矢量图形
  • 未针对高性能动画进行优化
  • 最适合简单的绘图
  • 需要理解基本几何
  • 无内置的撤销/重做功能

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_turtle

pub package

flutter_turtle is a simple yet powerful implementation of turtle graphics for Flutter. It leverages Flutter's CustomPainter to draw graphics using a declarative, Logo-like Domain-Specific Language (DSL) written entirely in Dart.

What is Turtle Graphics?

Turtle graphics is a popular method for introducing programming to beginners. It involves a "turtle" (an on-screen cursor) that carries a pen. By giving commands like "move forward," "turn right," and "pen down," you can create complex geometric shapes and intricate patterns. It was a key feature of the Logo programming language and remains a foundational concept in computer science education.

For further information about turtle graphics, please refer to Wikipedia:

Why I made this?

Building a Domain-Specific Language (DSL) within Dart is an excellent exercise in API design, and turtle graphics are a universally engaging way to introduce programming concepts through visual feedback!

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 core visualization widgets:

  • TurtleView: Serves as your canvas and immediately renders the fully evaluated graphics based on the provided commands.
  • AnimatedTurtleView: An animated variant that sequentially visualizes the turtle's drawing process over time.

To use them, simply instantiate TurtleView or AnimatedTurtleView, pass your declarative commands via the commands parameter, and embed the widget directly into your Flutter widget tree.

Technical Details & Architecture

flutter_turtle is built to be efficient, extensible, and seamlessly integrated into Flutter's rendering pipeline. The architecture is primarily divided into three stages: the DSL Command Definition, the Compiler, and the Painter.

1. The Command DSL

The library provides a set of classes (e.g., Forward, Right, Repeat, IfElse) that represent Logo-like instructions. Unlike a traditional interpreter that reads a text script, the DSL is constructed using native Dart objects. This provides full type safety, code completion, and allows you to seamlessly mix Flutter logic (like using Flutter Color or BuildContext state) directly into your turtle commands.

2. Compilation

Before any graphics are drawn, the tree of nested TurtleCommand objects is processed by the TurtleCompiler. The compiler evaluates flow-control commands (Repeat, If, IfElse) and flattens the execution tree into a linear sequence of atomic Instruction objects.

  • Macros: Macros are managed during compilation. A SetMacro command registers a sub-tree of commands, which are injected and compiled in place whenever a RunMacro command is encountered.
  • State Management: While compiling, the framework does not draw anything. It simply prepares a highly optimized sequence of operations.
3. Rendering Pipeline

The actual drawing happens inside TurtlePainter, which is a CustomPainter implementation.

  • TurtlePainter receives the compiled list of Instruction objects.
  • It initializes a PaintContext (providing access to the Canvas and Paint objects) and a TurtleState (which tracks the turtle's position, degrees, isPenDown status, colors, and line widths).
  • As TurtlePainter iterates through the instructions, each instruction executes against the PaintContext, mutating the state and calling raw Canvas drawing methods.
4. Animation Mechanism

AnimatedTurtleView leverages Flutter's AnimationController and AnimatedBuilder. When built, it calculates the total number of compiled instructions. As the animation progresses from 0.0 to 1.0, the widget progressively passes a growing sublist of instructions (e.g., from index 0 to total_instructions * animation.value) to the TurtlePainter. This simple yet effective approach smoothly simulates real-time drawing without requiring complex frame-by-frame state serialization.

Flutter Turtle DSL

If you have Logo code like this:

The equivalent code in the Flutter Turtle DSL would be:

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

Flow control commands are evaluated dynamically during compilation. You can use the IfElse class just like an if...else statement in Dart, and the Repeat class for loops.

An example of IfElse:

// If the condition evaluates to true, move forward; otherwise, move backward.
IfElse((_) => true, [Forward((_) => 10.0)], [Back(() => 10.0)]),

An example of Repeat:

// Repeat the inner commands 10 times.
Repeat((_) => 10, [Forward((_) => 10.0)]),
Macros

While the DSL does not support traditional named functions natively, you can achieve reusable components using macros. Use the SetMacro command to register a parameterized block of commands:

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

You can then execute the macro dynamically using the RunMacro command:

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

You can pass arguments when calling a macro. These arguments are provided as a Dart Map and are accessible via the callback parameter _ to every command within the macro. For example:

SetMacro('macro', [Forward((_) => _['arg1'])]), // Uses the "arg1" value from the arguments map.
RunMacro('macro', (_) => {'arg1': 10.0}),

Supported Commands

The currently supported primitives and logic structures include:

Turtle motion & Styling
  • PenDown
  • PenUp
  • Left
  • Right
  • Forward
  • Back
  • SetColor
  • SetStrokeWidth
  • GoTo
  • ResetPosition
  • ResetHeading
  • Label
  • SetLabelHeight
Flow control
  • If
  • IfElse
  • Repeat
Macros
  • SetMacro
  • RunMacro

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。