Skip to content

Commit cc399e4

Browse files
committed
fix(ui): Stagger implosion animation to prevent early parent circle visibility
1 parent 652196c commit cc399e4

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

lib/flutter_circle_pack_chart_painter.dart

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,25 @@ class FlutterCirclePackChartPainter extends CustomPainter {
8080
void _drawImplodingNode(Canvas canvas, PackedNode node, Color parentColor) {
8181
final Color color = node.node.color ?? parentColor;
8282

83+
// Staggered threshold: children merge and stay solid until 85% of animation.
84+
// This prevents the parent circle from showing through while they are still separate.
85+
const double threshold = 0.85;
86+
87+
final double childOpacity = (1.0 - (animationValue - threshold) / (1.0 - threshold)).clamp(0.0, 1.0);
88+
final double parentOpacity = ((animationValue - threshold) / (1.0 - threshold)).clamp(0.0, 1.0);
89+
8390
for (final child in node.children) {
91+
// Move children from their packed positions back to parent center (node.x, node.y)
8492
final double x = child.x + (node.x - child.x) * animationValue;
8593
final double y = child.y + (node.y - child.y) * animationValue;
94+
// Grow from child.r to parent.r (node.r)
8695
final double r = lerpDouble(child.r, node.r, animationValue)!;
87-
final double opacity = 1.0 - animationValue;
88-
_drawLeaf(canvas, x, y, r, child.node, color, opacity: opacity);
96+
97+
_drawLeaf(canvas, x, y, r, child.node, color, opacity: childOpacity);
8998
}
9099

91-
_drawLeaf(canvas, node.x, node.y, node.r, node.node, color, opacity: animationValue);
100+
// Parent circle fades in only at the very end of the implosion
101+
_drawLeaf(canvas, node.x, node.y, node.r, node.node, color, opacity: parentOpacity);
92102
}
93103

94104
void _drawLeaf(
@@ -142,8 +152,8 @@ class FlutterCirclePackChartPainter extends CustomPainter {
142152
text: '${node.upperLabel}\n',
143153
style: TextStyle(
144154
color: Colors.white.withValues(alpha: opacity),
145-
fontSize: fontSize * 1.2, // Upper line is 20% larger
146-
fontWeight: FontWeight.bold, // Upper line is bold
155+
fontSize: fontSize * 1.2,
156+
fontWeight: FontWeight.bold,
147157
),
148158
),
149159
],
@@ -166,7 +176,6 @@ class FlutterCirclePackChartPainter extends CustomPainter {
166176
ellipsis: '...',
167177
);
168178

169-
// Allow some overflow for tiny circles but keep it tighter than before to avoid edges.
170179
textPainter.layout(maxWidth: radius * 2.5);
171180

172181
textPainter.paint(

0 commit comments

Comments
 (0)