通过CustomPainter进行绘制,可实现一些复杂的自定义绘制组件,是Flutter中自定义组件的灵魂人物。
CustomPaint绘线图形
【painter】 : 绘画器 【CustomPainter】
import 'package:flutter/material.dart';
class ClockPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
height: 100,
child:CustomPaint(//使用CustomPaint盛放画布
painter: ClockPainter(),
),
)
;
}
}
class ClockPainter extends CustomPainter {
Paint _paint;
var _radius = 3.0; //小球半径
Path _path = Path(); //画笔对象
ClockPainter () {
_paint = Paint()..color= Color(0xff45d0fd)..isAntiAlias=true;
_path.addOval(Rect.fromCircle(radius: _radius, center: Offset(0, 0))); //小球路径
}
@override
void paint(Canvas canvas, Size size) {
print(size);
canvas.clipRect(Offset.zero & size);
canvas.translate(size.width/2-65*2, 0);
renderDigit(1, canvas);//渲染数字
canvas.translate(65, 0);//平移画布
renderDigit(9, canvas);
canvas.translate(65, 0); renderDigit(9, canvas);
canvas.translate(65, 0); renderDigit(4, canvas);
}
//渲染数字 num :要显示的数字 canvas :画布
void renderDigit(int num, Canvas canvas) {
if (num > 10) { return; }
for (int i = 0; i < digit[num].length; i++) {
for (int j = 0; j < digit[num][j].length; j++) {
if (digit[num][i][j] == 1) {
canvas.save();
double rX = j * 2 * (_radius + 1) + (_radius + 1); //第(i,j)个点圆心横坐标
double rY = i * 2 * (_radius + 1) + (_radius + 1); //第(i,j)个点圆心纵坐标
canvas.translate(rX, rY);
canvas.drawPath(_path, _paint);
canvas.restore();
}
}
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate)=> false;
}
const digit = [
[
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 0],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[0, 1, 1, 0, 1, 1, 0],
[0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0, 0]
], //9
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
] //:
];
CustomPaint绘线贝塞尔曲线
Flutter也支持贝塞尔曲线等复杂绘制。
import 'dart:ui';
import 'package:flutter/material.dart';
class PlayBezier3Page extends StatefulWidget {
@override
_PlayBezier3PageState createState() => _PlayBezier3PageState();
}
class _PlayBezier3PageState extends State<PlayBezier3Page> {
List<Offset> _pos = <Offset>[];
int selectPos;
@override
void initState() {
_initPoints();
super.initState();
}
void _initPoints() {
_pos = List<Offset>();
_pos.add(Offset(0, 0));
_pos.add(Offset(60, -60));
_pos.add(Offset(-90, -90));
_pos.add(Offset(-120, -40));
}
@override
Widget build(BuildContext context) {
return Container(
height: 200,
width: MediaQuery.of(context).size.width,
child: CustomPaint(
painter: BezierPainter(pos: _pos, selectPos: selectPos),
),
);
}
}
class BezierPainter extends CustomPainter {
Paint _gridPaint;
Path _gridPath;
Paint _mainPaint;
Path _mainPath;
int selectPos;
Paint _helpPaint;
List<Offset> pos;
BezierPainter({this.pos, this.selectPos}) {
_gridPaint = Paint()..style = PaintingStyle.stroke;
_gridPath = Path();
_mainPaint = Paint()
..color = Colors.orange
..style = PaintingStyle.stroke
..strokeWidth = 2;
_mainPath = Path();
_helpPaint = Paint()
..color = Colors.purple
..style = PaintingStyle.stroke
..strokeWidth = 2
..strokeCap = StrokeCap.round;
}
@override
void paint(Canvas canvas, Size size) {
canvas.clipRect(Offset.zero & size);
canvas.translate(size.width / 2, size.height / 2);
_drawGrid(canvas, size); //绘制格线
_drawAxis(canvas, size); //绘制轴线
_mainPath.moveTo(pos[0].dx, pos[0].dy);
_mainPath.cubicTo(pos[1].dx, pos[1].dy, pos[2].dx, pos[2].dy, pos[3].dx, pos[3].dy);
canvas.drawPath(_mainPath, _mainPaint);
_drawHelp(canvas);
_drawSelectPos(canvas);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
void _drawGrid(Canvas canvas, Size size) {
_gridPaint
..color = Colors.grey
..strokeWidth = 0.5;
_gridPath = _buildGridPath(_gridPath, size);
canvas.drawPath(_buildGridPath(_gridPath, size), _gridPaint);
canvas.save();
canvas.scale(1, -1); //沿x轴镜像
canvas.drawPath(_gridPath, _gridPaint);
canvas.restore();
canvas.save();
canvas.scale(-1, 1); //沿y轴镜像
canvas.drawPath(_gridPath, _gridPaint);
canvas.restore();
canvas.save();
canvas.scale(-1, -1); //沿原点镜像
canvas.drawPath(_gridPath, _gridPaint);
canvas.restore();
}
void _drawAxis(Canvas canvas, Size size) {
canvas.drawPoints(
PointMode.lines,
[
Offset(-size.width / 2, 0),
Offset(size.width / 2, 0),
Offset(0, -size.height / 2),
Offset(0, size.height / 2),
Offset(0, size.height / 2),
Offset(0 - 7.0, size.height / 2 - 10),
Offset(0, size.height / 2),
Offset(0 + 7.0, size.height / 2 - 10),
Offset(size.width / 2, 0),
Offset(size.width / 2 - 10, 7),
Offset(size.width / 2, 0),
Offset(size.width / 2 - 10, -7),
],
_gridPaint
..color = Colors.blue
..strokeWidth = 1.5);
}
Path _buildGridPath(Path path, Size size, {step = 20.0}) {
for (int i = 0; i < size.height / 2 / step; i++) {
path.moveTo(0, step * i);
path.relativeLineTo(size.width / 2, 0);
}
for (int i = 0; i < size.width / 2 / step; i++) {
path.moveTo(step * i, 0);
path.relativeLineTo(
0,
size.height / 2,
);
}
return path;
}
void _drawHelp(Canvas canvas) {
canvas.drawPoints(PointMode.lines, pos, _helpPaint..strokeWidth = 1);
canvas.drawPoints(PointMode.points, pos, _helpPaint..strokeWidth = 8);
}
void _drawSelectPos(Canvas canvas) {
if (selectPos == null) return;
canvas.drawCircle(
pos[selectPos],
10,
_helpPaint
..color = Colors.green
..strokeWidth = 2);
}
}