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);
}
}