gpt4 book ai didi

flutter - Flutter:如何在CustomPainter对象中设置动态颜色

转载 作者:行者123 更新时间:2023-12-03 04:27:09 27 4
gpt4 key购买 nike

为CustomPainter的构造函数动态设置绘画颜色不起作用。

lines_painter.dart

class LinesPainter extends CustomPainter {
final double lineHeight = 8;
final int maxLines = 60;
final Color customColor;

LinesPainter(this.customColor);

@override
void paint(Canvas canvas, Size size) {
canvas.translate(size.width / 2, size.height / 2);

canvas.save();
final Paint linePainter = Paint()
..color = customColor
..style = PaintingStyle.stroke
..strokeWidth = 1.5;
final radius = size.width / 2;

List.generate(maxLines, (i) {
var newRadius = (i % 5 == 0) ? radius - 15 : radius - 5;
canvas.drawLine(Offset(0, radius), Offset(0, newRadius), linePainter);
canvas.rotate(2 * pi / maxLines);
});

canvas.restore();
}

@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}

utils.dart
class Utils{

List<Color> getColorsArray (){
return [
Color(0xff5733),
Color(0xc70039),
Color(0x900c3e),
Color(0x571845),
Color(0x251e3e),
Color(0x051e3e),

];
}
}

下面的代码应该用线条绘制圆形
LinesPainter(Utils().getColorsArray()[0])

预期结果:

enter image description here

当前结果:

enter image description here

最佳答案

正如@pskink在评论中提到的,我已经阅读了文档,并且我想到了我缺少十六进制代码中的Alpha值。

如下更改utils.dart文件,它对我来说很好。

class Utils{

List<Color> getColorsArray (){
return [
Color(0xffff5733),
Color(0xffc70039),
Color(0xff900c3e),
Color(0xff571845),
Color(0xff251e3e),
Color(0xff051e3e),

];
}
}

关于flutter - Flutter:如何在CustomPainter对象中设置动态颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58972965/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com