fix(player): 恢复develop飘心实现
This commit is contained in:
1 parent
9039b27c7f
commit
84b24775d6
6 files changed
+107
-225
No files matched your search
@@ -1,37 +1,43 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/theme/app_colors.dart';
|
||||
|
||||
const int _heartDurationMs = 680;
|
||||
const double _heartPopPortion = 0.18;
|
||||
const double _heartStartScale = 0.7;
|
||||
const double _heartHoldPortion = 0.45;
|
||||
|
||||
const List<_HeartParticle> _hearts = [
|
||||
_HeartParticle(
|
||||
delay: 0,
|
||||
travel: 108,
|
||||
drift: 0,
|
||||
size: 48,
|
||||
color: AppColors.doubleTapHeartRed,
|
||||
),
|
||||
_HeartParticle(
|
||||
delay: 50,
|
||||
travel: 96,
|
||||
drift: -28,
|
||||
size: 36,
|
||||
color: AppColors.doubleTapHeartPink,
|
||||
),
|
||||
_HeartParticle(
|
||||
delay: 90,
|
||||
travel: 118,
|
||||
drift: 26,
|
||||
size: 40,
|
||||
color: AppColors.doubleTapHeartRed,
|
||||
),
|
||||
];
|
||||
|
||||
/// 双击点赞时,在点击位置飘出一小簇心。
|
||||
class PlayerDoubleTapHeartBurst extends StatefulWidget {
|
||||
const PlayerDoubleTapHeartBurst({
|
||||
super.key,
|
||||
required this.burstId,
|
||||
required this.onCompleted,
|
||||
this.random,
|
||||
this.origin = Offset.zero,
|
||||
});
|
||||
const PlayerDoubleTapHeartBurst({super.key, required this.onCompleted});
|
||||
|
||||
final int burstId;
|
||||
final VoidCallback onCompleted;
|
||||
final Random? random;
|
||||
final Offset origin;
|
||||
|
||||
@visibleForTesting
|
||||
static double opacityForProgress(double progress) =>
|
||||
1 - Curves.easeIn.transform(progress);
|
||||
|
||||
@visibleForTesting
|
||||
static double scaleForProgress(double progress) {
|
||||
return progress < 0.3
|
||||
? 0.55 + (1.15 - 0.55) * (progress / 0.3)
|
||||
: 1.15 - (1.15 - 0.2) * ((progress - 0.3) / 0.7);
|
||||
}
|
||||
|
||||
@override
|
||||
State<PlayerDoubleTapHeartBurst> createState() =>
|
||||
@@ -41,41 +47,14 @@ class PlayerDoubleTapHeartBurst extends StatefulWidget {
|
||||
class _PlayerDoubleTapHeartBurstState extends State<PlayerDoubleTapHeartBurst>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _controller;
|
||||
late final List<_HeartParticle> _particles;
|
||||
final Stopwatch _lifetime = Stopwatch();
|
||||
bool _completed = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_lifetime.start();
|
||||
final random = widget.random ?? Random();
|
||||
var delay = 0;
|
||||
_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 (kDebugMode) {
|
||||
debugPrint('[player] phase=heart-burst-init id=${widget.burstId}');
|
||||
debugPrint(
|
||||
'[player] phase=heart-burst-start '
|
||||
'id=${widget.burstId} count=${_particles.length} '
|
||||
'at=${widget.origin.dx.toStringAsFixed(1)},'
|
||||
'${widget.origin.dy.toStringAsFixed(1)}',
|
||||
);
|
||||
}
|
||||
final total = _hearts
|
||||
.map((heart) => heart.delay + _heartDurationMs)
|
||||
.reduce((left, right) => left > right ? left : right);
|
||||
_controller = AnimationController(
|
||||
vsync: this,
|
||||
duration: Duration(milliseconds: total),
|
||||
@@ -86,29 +65,11 @@ class _PlayerDoubleTapHeartBurstState extends State<PlayerDoubleTapHeartBurst>
|
||||
void _onStatus(AnimationStatus status) {
|
||||
if (status != AnimationStatus.completed || _completed) return;
|
||||
_completed = true;
|
||||
if (kDebugMode) {
|
||||
final elapsedMs = _controller.duration?.inMilliseconds ?? 0;
|
||||
debugPrint(
|
||||
'[player] phase=heart-burst-complete '
|
||||
'id=${widget.burstId} configuredMs=$elapsedMs',
|
||||
);
|
||||
}
|
||||
widget.onCompleted();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (kDebugMode) {
|
||||
final elapsedMs = _completed
|
||||
? _controller.duration?.inMilliseconds ?? 0
|
||||
: _lifetime.elapsedMilliseconds;
|
||||
debugPrint(
|
||||
'[player] phase=heart-burst-dispose '
|
||||
'id=${widget.burstId} elapsedMs=$elapsedMs '
|
||||
'completed=${_completed ? 1 : 0}',
|
||||
);
|
||||
}
|
||||
_lifetime.stop();
|
||||
_controller
|
||||
..removeStatusListener(_onStatus)
|
||||
..dispose();
|
||||
@@ -118,44 +79,51 @@ class _PlayerDoubleTapHeartBurstState extends State<PlayerDoubleTapHeartBurst>
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return IgnorePointer(
|
||||
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),
|
||||
],
|
||||
);
|
||||
},
|
||||
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 < _hearts.length; index++)
|
||||
_heart(_hearts[index], elapsed, index),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _particle(_HeartParticle particle, double elapsed, int index) {
|
||||
final progress = ((elapsed - particle.delay) / particle.duration).clamp(
|
||||
Widget _heart(_HeartParticle heart, double elapsed, int index) {
|
||||
final progress = ((elapsed - heart.delay) / _heartDurationMs).clamp(
|
||||
0.0,
|
||||
1.0,
|
||||
);
|
||||
if (progress <= 0) return const SizedBox.shrink();
|
||||
final move = Curves.easeOutCubic.transform(progress);
|
||||
final scale = PlayerDoubleTapHeartBurst.scaleForProgress(progress);
|
||||
final scale = progress < _heartPopPortion
|
||||
? _heartStartScale +
|
||||
(1 - _heartStartScale) * (progress / _heartPopPortion)
|
||||
: 1.0;
|
||||
final opacity = progress < _heartHoldPortion
|
||||
? 1.0
|
||||
: 1 - ((progress - _heartHoldPortion) / (1 - _heartHoldPortion));
|
||||
return Positioned(
|
||||
left: widget.origin.dx + particle.drift * move - particle.size / 2,
|
||||
top: widget.origin.dy - particle.size / 2 - particle.travel * move,
|
||||
left: heart.drift * move - heart.size / 2,
|
||||
top: -heart.size / 2 - heart.travel * move,
|
||||
child: Opacity(
|
||||
opacity: PlayerDoubleTapHeartBurst.opacityForProgress(progress),
|
||||
opacity: opacity.clamp(0.0, 1.0),
|
||||
child: Transform.scale(
|
||||
scale: scale,
|
||||
child: Icon(
|
||||
key: Key('double-tap-heart-$index'),
|
||||
Icons.favorite_rounded,
|
||||
color: particle.color,
|
||||
size: particle.size,
|
||||
color: heart.color,
|
||||
size: heart.size,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -166,7 +134,6 @@ class _PlayerDoubleTapHeartBurstState extends State<PlayerDoubleTapHeartBurst>
|
||||
class _HeartParticle {
|
||||
const _HeartParticle({
|
||||
required this.delay,
|
||||
required this.duration,
|
||||
required this.travel,
|
||||
required this.drift,
|
||||
required this.size,
|
||||
@@ -174,9 +141,8 @@ class _HeartParticle {
|
||||
});
|
||||
|
||||
final int delay;
|
||||
final int duration;
|
||||
final int travel;
|
||||
final int drift;
|
||||
final double travel;
|
||||
final double drift;
|
||||
final double size;
|
||||
final Color color;
|
||||
}
|
||||
Reference in new issue
Block a user