Flutter 使用p5

时光毁灭记忆、已成空白 提交于 2019-11-27 21:48:09

install

dependencies:
  p5: ^0.0.5

main.dart

import 'package:flutter/material.dart';

import "package:p5/p5.dart";
import "sketch.dart";

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  MySketch sketch;
  PAnimator animator;

  @override
  void initState() {
    super.initState();
    sketch = new MySketch();
    // 需要动画师连续调用草图中的draw()方法,
    // 否则只有在检测到触摸事件时才会调用它。
    animator = new PAnimator(this);
    animator.addListener(() {
      setState(() {
        sketch.redraw();
      });
    });
    animator.run();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new PWidget(sketch),
      ),
    );
  }
}

sketch.dart

import 'package:flutter/material.dart';
import "package:p5/p5.dart";

class MySketch extends PPainter {
  void setup() {
    // size(300, 300);
    fullScreen();
  }

  void draw() {
    background(color(255, 255, 255));
    fill(Colors.blue);
    noStroke();
    ellipse(mouseX, mouseY, width / 2, height / 2);
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!