fix(player): wait for the Material full player cover's first frame

The full Material player cover crossfaded through its own switcher as
soon as the song changed, so a slow cover faded in as the placeholder.
It now uses PlayerArtwork's crossfade, which keeps the old cover until
the new one has a frame (or the store answers that it has none), with a
new optional growth: the cover still fades in on the effects spring and
grows from 0.94 on the slow spatial spring. Reduced motion swaps it in
place.
This commit is contained in:
zarzet committed 2026-11-07 20:14:26 +07:00
1 parent 3407539f3b
commit 3590f9cf8e
4 files changed
+152 -62

No files matched your search

+15 -47
View File
@@ -114,17 +114,21 @@ class MaterialPlayerLayout extends StatelessWidget {
child: SizedBox(
width: artSize,
height: artSize,
child: _CoverSwitcher(
child: PlayerArtwork(
key: ValueKey(
mediaItem.artUri?.toString() ?? mediaItem.id,
),
artUri: mediaItem.artUri?.toString(),
colorScheme: colorScheme,
cacheWidth:
(artSize *
MediaQuery.devicePixelRatioOf(context))
.round(),
// A track change keeps the old cover until the new
// one has a frame. It then fades in on the effects
// spring and grows from 0.94 on the slow spatial
// spring, which does not overshoot because the app
// started this motion.
child: PlayerArtwork(
artUri: mediaItem.artUri?.toString(),
colorScheme: colorScheme,
cacheWidth:
(artSize * MediaQuery.devicePixelRatioOf(context))
.round(),
crossfade: playerTrackChangeMotion(context),
crossfadeScale: AppMotion.spatialSlow(
context,
playerTrackChangeMotion(context),
),
),
),
@@ -393,39 +397,3 @@ class MaterialPlayerLayout extends StatelessWidget {
];
}
}
/// The cover's track change: the new cover fades in on the effects spring
/// and grows from 0.94 on the slow spatial spring, which does not overshoot
/// because the app started this motion. The previous cover stays opaque
/// underneath until the new one covers it.
class _CoverSwitcher extends StatelessWidget {
const _CoverSwitcher({required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
final fade = playerTrackChangeMotion(context);
final scale = AppMotion.spatialSlow(context, fade);
final total = fade.duration > scale.duration
? fade.duration
: scale.duration;
double share(Duration part) =>
total == Duration.zero ? 1 : part.inMicroseconds / total.inMicroseconds;
final fadeCurve = Interval(0, share(fade.duration), curve: fade.curve);
final scaleCurve = Interval(0, share(scale.duration), curve: scale.curve);
return AnimatedSwitcher(
duration: total,
transitionBuilder: (child, animation) => FadeTransition(
opacity: animation.drive(CurveTween(curve: fadeCurve)),
child: ScaleTransition(
scale: animation.drive(
Tween(begin: 0.94, end: 1.0).chain(CurveTween(curve: scaleCurve)),
),
child: child,
),
),
child: child,
);
}
}
+49 -14
View File
@@ -13,6 +13,8 @@ class PlayerArtwork extends StatelessWidget {
/// Shared decode for the mini player and the full player's blurred cover.
static const miniCacheWidth = 132;
static const _grownFrom = 0.94;
/// An HTTP(S) URL, file URI, absolute local artwork path, or `network://`
/// cover reference.
final String? artUri;
@@ -25,6 +27,10 @@ class PlayerArtwork extends StatelessWidget {
/// Null, a zero duration or reduced motion swap the cover at once.
final AppMotionSpec? crossfade;
/// With [crossfade], the new cover also grows from 0.94 on this motion
/// while it fades in.
final AppMotionSpec? crossfadeScale;
const PlayerArtwork({
super.key,
required this.artUri,
@@ -32,6 +38,7 @@ class PlayerArtwork extends StatelessWidget {
this.cacheWidth,
this.iconSize = 40,
this.crossfade,
this.crossfadeScale,
});
/// Share one full-size decode while artwork moves between player panels.
@@ -55,7 +62,12 @@ class PlayerArtwork extends StatelessWidget {
if (motion != null &&
motion.duration > Duration.zero &&
!MediaQuery.disableAnimationsOf(context)) {
return _ArtworkCrossfade(artUri: artUri, motion: motion, builder: _layer);
return _ArtworkCrossfade(
artUri: artUri,
motion: motion,
scale: crossfadeScale,
builder: _layer,
);
}
final placeholder = _placeholder();
@@ -130,11 +142,13 @@ class _ArtworkCrossfade extends StatefulWidget {
const _ArtworkCrossfade({
required this.artUri,
required this.motion,
required this.scale,
required this.builder,
});
final String? artUri;
final AppMotionSpec motion;
final AppMotionSpec? scale;
final Widget Function(BuildContext, String?, VoidCallback onShown) builder;
@override
@@ -146,11 +160,23 @@ class _ArtworkCrossfadeState extends State<_ArtworkCrossfade>
late _ArtworkLayer _current = _ArtworkLayer(widget.artUri, _opacity(1));
_ArtworkLayer? _previous;
AnimationController _opacity(double value) => AnimationController(
vsync: this,
value: value,
duration: widget.motion.duration,
);
/// One linear timeline per cover: the fade and the growth each run their
/// own curve over their own share of it.
Duration get _total {
final scale = widget.scale?.duration ?? Duration.zero;
return scale > widget.motion.duration ? scale : widget.motion.duration;
}
Animation<double> _part(_ArtworkLayer layer, AppMotionSpec motion) {
final total = _total.inMicroseconds;
final share = total == 0 ? 1.0 : motion.duration.inMicroseconds / total;
return layer.opacity.drive(
CurveTween(curve: Interval(0, share, curve: motion.curve)),
);
}
AnimationController _opacity(double value) =>
AnimationController(vsync: this, value: value, duration: _total);
@override
void didUpdateWidget(_ArtworkCrossfade oldWidget) {
@@ -193,7 +219,7 @@ class _ArtworkCrossfadeState extends State<_ArtworkCrossfade>
// Runs during build: start the fade once this frame is done.
SchedulerBinding.instance.addPostFrameCallback((_) {
if (!mounted || !identical(layer, _current)) return;
final fade = layer.opacity.animateTo(1, curve: widget.motion.curve);
final fade = layer.opacity.animateTo(1);
fade.whenCompleteOrCancel(() {
if (!mounted || !identical(layer, _current) || _previous == null) {
return;
@@ -214,13 +240,22 @@ class _ArtworkCrossfadeState extends State<_ArtworkCrossfade>
super.dispose();
}
Widget _build(BuildContext context, _ArtworkLayer layer) => KeyedSubtree(
key: layer.key,
child: FadeTransition(
opacity: layer.opacity,
child: widget.builder(context, layer.uri, () => _show(layer)),
),
);
Widget _build(BuildContext context, _ArtworkLayer layer) {
Widget child = widget.builder(context, layer.uri, () => _show(layer));
if (widget.scale case final scale?) {
child = ScaleTransition(
scale: _part(
layer,
scale,
).drive(Tween(begin: PlayerArtwork._grownFrom, end: 1.0)),
child: child,
);
}
return KeyedSubtree(
key: layer.key,
child: FadeTransition(opacity: _part(layer, widget.motion), child: child),
);
}
@override
Widget build(BuildContext context) {
+5 -1
View File
@@ -2940,7 +2940,11 @@ void main() {
matching: find.byType(PlayerArtwork),
);
expect(covers, findsOneWidget);
expect(tester.widget<PlayerArtwork>(covers).key, const ValueKey('second'));
// Reduced motion swaps the cover in place, with no fading layers.
expect(
find.descendant(of: covers, matching: find.byType(FadeTransition)),
findsNothing,
);
expect(tester.takeException(), isNull);
});
+83
View File
@@ -178,6 +178,89 @@ void main() {
});
}
testWidgets('a growing crossfade waits for the frame, then fades and grows', (
tester,
) async {
final cache = PaintingBinding.instance.imageCache;
addTearDown(() {
cache.clear();
cache.clearLiveImages();
});
final image = await tester.runAsync(() async {
final recorder = ui.PictureRecorder();
Canvas(recorder).drawColor(Colors.green, BlendMode.src);
final picture = recorder.endRecording();
final result = await picture.toImage(1, 1);
picture.dispose();
return result;
});
final pending = Completer<ImageInfo>();
cache.putIfAbsent(
FileImage(File('/grow-first.png')),
() => OneFrameImageStreamCompleter(
Future.value(ImageInfo(image: image!.clone())),
),
);
cache.putIfAbsent(
FileImage(File('/grow-second.png')),
() => OneFrameImageStreamCompleter(pending.future),
);
Widget app(String path) => MaterialApp(
home: PlayerArtwork(
artUri: path,
colorScheme: const ColorScheme.dark(),
crossfade: const AppMotionSpec(
Duration(milliseconds: 100),
Curves.linear,
),
crossfadeScale: const AppMotionSpec(
Duration(milliseconds: 400),
Curves.linear,
),
),
);
({double opacity, double scale}) top() {
final layer = find.byType(KeyedSubtree).last;
return (
opacity: tester
.widget<FadeTransition>(
find.descendant(of: layer, matching: find.byType(FadeTransition)),
)
.opacity
.value,
scale: tester
.widget<ScaleTransition>(
find
.descendant(of: layer, matching: find.byType(ScaleTransition))
.first,
)
.scale
.value,
);
}
await tester.pumpWidget(app('/grow-first.png'));
await tester.pump();
expect(top(), (opacity: 1.0, scale: 1.0));
await tester.pumpWidget(app('/grow-second.png'));
await tester.pump(const Duration(milliseconds: 300));
// No frame yet: the new cover waits, hidden and small.
expect(top(), (opacity: 0.0, scale: 0.94));
pending.complete(ImageInfo(image: image!));
for (var frame = 0; frame < 10; frame++) {
await tester.pump(const Duration(milliseconds: 16));
}
// The fade is done well before the growth.
expect(top().opacity, 1);
expect(top().scale, inExclusiveRange(0.94, 1));
await tester.pump(const Duration(milliseconds: 400));
expect(top(), (opacity: 1.0, scale: 1.0));
expect(find.byType(RawImage), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
});
testWidgets('covers with no file show the placeholder without throwing', (
tester,
) async {