Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Last active May 28, 2019 08:57
Show Gist options
  • Save CaiJingLong/e63b8e29557a3b7e168422cb71a474f2 to your computer and use it in GitHub Desktop.
Save CaiJingLong/e63b8e29557a3b7e168422cb71a474f2 to your computer and use it in GitHub Desktop.
flutter painter 随机点
import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.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: 'Flutter Demo Home Page'),
);
}
}
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) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
width: double.infinity,
height: double.infinity,
child: CustomPaint(
painter: MyPainter(),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class MyPainter extends CustomPainter {
Paint p = Paint()
..color = Colors.green
..strokeWidth = 2
..isAntiAlias = true;
@override
void paint(Canvas canvas, Size size) {
var points = <Offset>[];
var startPoint = size.centerLeft(Offset.zero).translate(15, 0);
points.add(startPoint);
for (var i = 0, lastPoint = startPoint; i < 200; i++) {
var y = Random().nextInt(15);
if (Random().nextBool()) {
y = -y;
} else {
y = y.abs();
}
var point = lastPoint.translate(1.5, y.toDouble());
points.add(point);
lastPoint = point;
}
p.strokeWidth = 1.5;
canvas.drawPoints(PointMode.points, points, p);
p.strokeWidth = 2;
canvas.drawPoints(PointMode.polygon, points, p);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
@CaiJingLong
Copy link
Author

Screenshot_1559033726

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment