Flutter 颤振:如何在CustomPainter对象中设置动态颜色
为CustomPainter的构造函数动态设置绘制颜色不起作用 省道线Flutter 颤振:如何在CustomPainter对象中设置动态颜色,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,为CustomPainter的构造函数动态设置绘制颜色不起作用 省道线 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)
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;
}
飞镖
class Utils{
List<Color> getColorsArray (){
return [
Color(0xff5733),
Color(0xc70039),
Color(0x900c3e),
Color(0x571845),
Color(0x251e3e),
Color(0x051e3e),
];
}
}
预期结果:
当前结果:
正如@pskink在评论中提到的那样,我已经阅读了文档,我认为我在十六进制代码中缺少Alpha值 在utils.dart文件中进行如下更改,对我来说效果很好
class Utils{
List<Color> getColorsArray (){
return [
Color(0xffff5733),
Color(0xffc70039),
Color(0xff900c3e),
Color(0xff571845),
Color(0xff251e3e),
Color(0xff051e3e),
];
}
}
类Utils{
列出getColorsArray(){
返回[
颜色(0xffff5733),
颜色(0xffc70039),
颜色(0xff900c3e),
颜色(0xff571845),
颜色(0xFF251E),
颜色(0xff051e3e),
];
}
}
正如@pskink在评论中提到的那样,我已经阅读了文档,我认为我在十六进制代码中缺少Alpha值 在utils.dart文件中进行如下更改,对我来说效果很好
class Utils{
List<Color> getColorsArray (){
return [
Color(0xffff5733),
Color(0xffc70039),
Color(0xff900c3e),
Color(0xff571845),
Color(0xff251e3e),
Color(0xff051e3e),
];
}
}
类Utils{
列出getColorsArray(){
返回[
颜色(0xffff5733),
颜色(0xffc70039),
颜色(0xff900c3e),
颜色(0xff571845),
颜色(0xFF251E),
颜色(0xff051e3e),
];
}
}
add
print(customColor)
insidepaint()
方法,你看到了什么?print(customColor)//输出I/flatter(4320):Color(0x00ff5733),所以你需要Color(0xffff5733)
-数组中的其他颜色也是如此,我没有得到它。请详细说明一下好吗?请参见添加print(customColor)
insidepaint()
method,您看到了什么?print(customColor)//OutPut I/flatter(4320):Color(0x00ff5733),所以您需要Color(0xffff5733)
-而数组中的其他颜色也是如此,我没有得到它。你能详细说明一下吗?好吧……这是我开始使用flatter时得到的第一个aaaaaaaaaaa
东西。仅供参考,Colorr类有两个不错的方法使用不透明
和使用Alpha
你应该仔细检查这些……这是我开始使用颤振时得到的第一件aaaaaaaaaaaaaaaaa
东西。:)。仅供参考,Colorr类有两个不错的方法<代码>使用不透明度和使用Alpha
你也应该检查它们