我正在尝试使用CustomPainter类绘制半环,您可以在下图中看到它(浅紫色和浅橙色),但无法绘制。 我只能画圆,正方形和线。
代码:
class MakeCircle extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint();
paint.color = Colors.white12;
var position = Offset(size.width /10, size.height / 10);
canvas.drawCircle(position, 40.0, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
这只能画圆。
答案 0 :(得分:1)
您必须为 Paint 对象使用 PaintingStyle.stroke ,还必须使用 Path 类绘制弧形 >
我为您创建了一个示例,请对其进行测试:
import 'dart:math' as math;
import 'package:flutter/material.dart';
main() {
runApp(MaterialApp(
home: Home(),
));
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 300,
height: 300,
child: CustomPaint(
painter: MakeCircle(strokeWidth: 50,strokeCap: StrokeCap.round),
),
),
),
);
}
}
class MakeCircle extends CustomPainter {
final double strokeWidth;
final StrokeCap strokeCap;
MakeCircle({this.strokeCap = StrokeCap.square, this.strokeWidth = 10.0});
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..strokeCap = StrokeCap.round
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke; //important set stroke style
final path = Path()
..moveTo(strokeWidth, strokeWidth)
..arcToPoint(Offset(size.width - strokeWidth, size.height - strokeWidth),
radius: Radius.circular(math.max(size.width, size.height)));
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}