我有一个自定义画家:
class _Painter extends CustomPainter {
_Painter({
@required this.animation,
this.strokeWidth,
@required this.valuePercentage,
@required this.color,
}) : super(repaint: animation);
final Animation<double> animation;
final double strokeWidth;
final double valuePercentage;
final Color color;
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.black12
..strokeWidth = strokeWidth ?? 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;
canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
paint.color = color;
double progressRadians =
(valuePercentage != 0 ? valuePercentage : 1.0) * 2 * pi;
canvas.drawArc(Offset.zero & size, pi * 1.5, progressRadians, false, paint);
}
@override
bool shouldRepaint(_Painter other) {
return valuePercentage != other.valuePercentage;
}
}
在 StatelessWidget 中像这样使用:
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
SizedBox(
height: radius * 2,
width: radius * 2,
child: Center(
child: CustomPaint(
size: Size((radius - 3) * 2, (radius - 3) * 2),
painter: _Painter(
animation: animationController,
strokeWidth: strokeWidth,
valuePercentage: _valuePercentage,
color: color,
),
),
),
),
// ...
],
);
}
结果:
现在,这种情况发生了:
Flutter v1.0.0, channel 稳定。
最佳答案
经过一番调试,我意识到这是 Flutter 本身造成的。从稳定的 v1 channel 切换到主 channel (讽刺的是)解决了这个问题。
关于Flutter Custom Painter 在点击/滚动时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53928729/