fix(player): 恢复develop飘心实现
This commit is contained in:
1 parent
9039b27c7f
commit
84b24775d6
6 files changed
+107
-225
No files matched your search
@@ -132,7 +132,6 @@ class _HomeFeedOverlayState extends State<HomeFeedOverlay> {
|
||||
child: PlayerTapGestureRegion(
|
||||
key: const ValueKey('home-feed-tap-gesture-region'),
|
||||
gestureKey: const Key('home-feed-gesture'),
|
||||
episodeId: widget.episode.id,
|
||||
enabled: !_scrubbing,
|
||||
doubleTapEnabled: !widget.locked,
|
||||
now: widget.now,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -61,7 +61,6 @@ class PlayerPlaybackOverlay extends StatelessWidget {
|
||||
child: PlayerTapGestureRegion(
|
||||
key: const ValueKey('player-tap-gesture-region'),
|
||||
gestureKey: const Key('player-video-tap'),
|
||||
episodeId: data.episode.id,
|
||||
enabled: !scrubbing,
|
||||
doubleTapEnabled: !data.locked,
|
||||
onSingleTap: actions.onTogglePlay,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'player_double_tap_hearts.dart';
|
||||
@@ -15,7 +14,6 @@ class PlayerTapGestureRegion extends StatefulWidget {
|
||||
const PlayerTapGestureRegion({
|
||||
super.key,
|
||||
required this.gestureKey,
|
||||
required this.episodeId,
|
||||
required this.onSingleTap,
|
||||
required this.onLongPressStart,
|
||||
required this.onInteractionEnd,
|
||||
@@ -26,7 +24,6 @@ class PlayerTapGestureRegion extends StatefulWidget {
|
||||
});
|
||||
|
||||
final Key gestureKey;
|
||||
final String episodeId;
|
||||
final VoidCallback onSingleTap;
|
||||
final ValueChanged<Offset>? onDoubleTapLike;
|
||||
final PlayerLongPressStartCallback onLongPressStart;
|
||||
@@ -50,18 +47,12 @@ class _PlayerTapGestureRegionState extends State<PlayerTapGestureRegion> {
|
||||
int _downSerial = 0;
|
||||
int _tapSeq = 0;
|
||||
int _nextHeartBurstId = 0;
|
||||
final Map<int, OverlayEntry> _heartOverlayEntries = {};
|
||||
final List<({int id, Offset position})> _heartBursts = [];
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tapSeq++;
|
||||
_singleTapTimer?.cancel();
|
||||
for (final entry in _heartOverlayEntries.values.toList()) {
|
||||
entry
|
||||
..remove()
|
||||
..dispose();
|
||||
}
|
||||
_heartOverlayEntries.clear();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -87,6 +78,25 @@ class _PlayerTapGestureRegionState extends State<PlayerTapGestureRegion> {
|
||||
onLongPressEnd: (_) => widget.onInteractionEnd(),
|
||||
onLongPressCancel: widget.onInteractionEnd,
|
||||
),
|
||||
if (_heartBursts.isNotEmpty)
|
||||
Positioned.fill(
|
||||
child: IgnorePointer(
|
||||
child: Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
for (final burst in _heartBursts)
|
||||
Positioned(
|
||||
left: burst.position.dx,
|
||||
top: burst.position.dy - 10,
|
||||
child: PlayerDoubleTapHeartBurst(
|
||||
key: ValueKey(burst.id),
|
||||
onCompleted: () => _removeHeartBurst(burst.id),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -128,7 +138,7 @@ class _PlayerTapGestureRegionState extends State<PlayerTapGestureRegion> {
|
||||
void _handleTapUp(TapUpDetails details) {
|
||||
final now = _now();
|
||||
if (_doubleTapDown || _singleTapBlocked(now)) {
|
||||
_handleDoubleTapLike(details.globalPosition);
|
||||
_handleDoubleTapLike(details.localPosition);
|
||||
return;
|
||||
}
|
||||
final serial = _downSerial;
|
||||
@@ -146,54 +156,19 @@ class _PlayerTapGestureRegionState extends State<PlayerTapGestureRegion> {
|
||||
_singleTapTimer = pending;
|
||||
}
|
||||
|
||||
void _handleDoubleTapLike(Offset globalPosition) {
|
||||
void _handleDoubleTapLike(Offset position) {
|
||||
_blockSingleTapUntil = _now().add(_singleTapDelay);
|
||||
_cancelPendingSingleTap();
|
||||
if (!widget.doubleTapEnabled || _likedThisPair) return;
|
||||
_likedThisPair = true;
|
||||
_showHeartBurst(globalPosition);
|
||||
if (kDebugMode) {
|
||||
debugPrint(
|
||||
'[player] phase=double-tap-like episodeId=${widget.episodeId}',
|
||||
);
|
||||
}
|
||||
widget.onDoubleTapLike?.call(globalPosition);
|
||||
}
|
||||
|
||||
void _showHeartBurst(Offset globalPosition) {
|
||||
final overlay = Overlay.maybeOf(context, rootOverlay: true);
|
||||
if (overlay == null) return;
|
||||
final overlayBox = overlay.context.findRenderObject();
|
||||
final origin = overlayBox is RenderBox
|
||||
? overlayBox.globalToLocal(globalPosition)
|
||||
: globalPosition;
|
||||
final id = _nextHeartBurstId++;
|
||||
late final OverlayEntry entry;
|
||||
entry = OverlayEntry(
|
||||
builder: (context) => Positioned.fill(
|
||||
child: IgnorePointer(
|
||||
child: PlayerDoubleTapHeartBurst(
|
||||
key: ValueKey('heart-overlay-$id'),
|
||||
burstId: id,
|
||||
origin: origin,
|
||||
onCompleted: () => _removeHeartOverlay(id),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
_heartOverlayEntries[id] = entry;
|
||||
overlay.insert(entry);
|
||||
setState(() => _heartBursts.add((id: id, position: position)));
|
||||
widget.onDoubleTapLike?.call(position);
|
||||
}
|
||||
|
||||
void _removeHeartOverlay(int id) {
|
||||
final entry = _heartOverlayEntries.remove(id);
|
||||
if (entry == null) return;
|
||||
entry
|
||||
..remove()
|
||||
..dispose();
|
||||
if (kDebugMode) {
|
||||
debugPrint('[player] phase=heart-burst-entry-removed id=$id');
|
||||
}
|
||||
void _removeHeartBurst(int id) {
|
||||
if (!mounted) return;
|
||||
setState(() => _heartBursts.removeWhere((burst) => burst.id == id));
|
||||
}
|
||||
|
||||
void _handleLongPressStart(LongPressStartDetails details, double width) {
|
||||
|
||||
Reference in new issue
Block a user