fix(player): 使用根Overlay渲染飘心

This commit is contained in:
Zhou Weidong committed 2026-10-26 18:35:30 +08:00
1 parent 30b43878ab
commit 9039b27c7f
3 files changed
+63 -81

No files matched your search

@@ -17,10 +17,6 @@ class PlayerDoubleTapHeartBurst extends StatefulWidget {
this.origin = Offset.zero,
});
static const int minParticleCount = 4;
static const int maxParticleCount = 5;
static const int minimumBurstDurationMs = 2000;
final int burstId;
final VoidCallback onCompleted;
final Random? random;
@@ -55,43 +51,22 @@ class _PlayerDoubleTapHeartBurstState extends State<PlayerDoubleTapHeartBurst>
_lifetime.start();
final random = widget.random ?? Random();
var delay = 0;
_particles = List.generate(
PlayerDoubleTapHeartBurst.minParticleCount +
random.nextInt(
PlayerDoubleTapHeartBurst.maxParticleCount -
PlayerDoubleTapHeartBurst.minParticleCount +
1,
),
(index) {
if (index > 0) delay += 80 + random.nextInt(41);
return _HeartParticle(
delay: delay,
duration: 1500 + random.nextInt(301),
travel: 100 + random.nextInt(31),
drift: -30 + random.nextInt(61),
size: (22 + random.nextInt(13)).toDouble(),
color: random.nextBool()
? AppColors.doubleTapHeartRed
: AppColors.doubleTapHeartPink,
);
},
);
var total = _particles
_particles = List.generate(6 + random.nextInt(3), (index) {
if (index > 0) delay += 40 + random.nextInt(21);
return _HeartParticle(
delay: delay,
duration: 700 + random.nextInt(201),
travel: 120 + random.nextInt(41),
drift: -30 + random.nextInt(61),
size: (22 + random.nextInt(13)).toDouble(),
color: random.nextBool()
? AppColors.doubleTapHeartRed
: AppColors.doubleTapHeartPink,
);
});
final total = _particles
.map((particle) => particle.delay + particle.duration)
.reduce(max);
if (total < PlayerDoubleTapHeartBurst.minimumBurstDurationMs) {
final last = _particles.last;
final requiredDuration =
PlayerDoubleTapHeartBurst.minimumBurstDurationMs - last.delay;
final adjustedDuration = max(
last.duration,
requiredDuration,
).clamp(1500, 1800).toInt();
_particles[_particles.length - 1] = last.withDuration(adjustedDuration);
total = _particles
.map((particle) => particle.delay + particle.duration)
.reduce(max);
}
if (kDebugMode) {
debugPrint('[player] phase=heart-burst-init id=${widget.burstId}');
debugPrint(
@@ -143,21 +118,20 @@ class _PlayerDoubleTapHeartBurstState extends State<PlayerDoubleTapHeartBurst>
@override
Widget build(BuildContext context) {
return IgnorePointer(
child: SizedBox.shrink(
child: AnimatedBuilder(
animation: _controller,
builder: (context, _) {
final elapsed =
_controller.value * _controller.duration!.inMilliseconds;
return Stack(
clipBehavior: Clip.none,
children: [
for (var index = 0; index < _particles.length; index++)
_particle(_particles[index], elapsed, index),
],
);
},
),
child: AnimatedBuilder(
animation: _controller,
builder: (context, _) {
final elapsed =
_controller.value * _controller.duration!.inMilliseconds;
return Stack(
fit: StackFit.expand,
clipBehavior: Clip.none,
children: [
for (var index = 0; index < _particles.length; index++)
_particle(_particles[index], elapsed, index),
],
);
},
),
);
}
@@ -171,8 +145,8 @@ class _PlayerDoubleTapHeartBurstState extends State<PlayerDoubleTapHeartBurst>
final move = Curves.easeOutCubic.transform(progress);
final scale = PlayerDoubleTapHeartBurst.scaleForProgress(progress);
return Positioned(
left: particle.drift * move - particle.size / 2,
top: -particle.size / 2 - particle.travel * move,
left: widget.origin.dx + particle.drift * move - particle.size / 2,
top: widget.origin.dy - particle.size / 2 - particle.travel * move,
child: Opacity(
opacity: PlayerDoubleTapHeartBurst.opacityForProgress(progress),
child: Transform.scale(
@@ -205,15 +179,4 @@ class _HeartParticle {
final int drift;
final double size;
final Color color;
_HeartParticle withDuration(int nextDuration) {
return _HeartParticle(
delay: delay,
duration: nextDuration,
travel: travel,
drift: drift,
size: size,
color: color,
);
}
}
@@ -170,9 +170,7 @@ class _PlayerTapGestureRegionState extends State<PlayerTapGestureRegion> {
final id = _nextHeartBurstId++;
late final OverlayEntry entry;
entry = OverlayEntry(
builder: (context) => Positioned(
left: origin.dx,
top: origin.dy,
builder: (context) => Positioned.fill(
child: IgnorePointer(
child: PlayerDoubleTapHeartBurst(
key: ValueKey('heart-overlay-$id'),