Last active
May 28, 2019 08:57
-
-
Save CaiJingLong/e63b8e29557a3b7e168422cb71a474f2 to your computer and use it in GitHub Desktop.
flutter painter 随机点
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Author
CaiJingLong
commented
May 28, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment