Files
SpotiFLAC-Mobile/lib/widgets/now_playing_indicator.dart
T
zarzet 6417d80428 feat(material): mark the current track in song lists
Material 3 Expressive puts expression in state changes: the playing row
should read at a glance, and selection should use its own solid role
instead of a faded primary container.

- Add NowPlayingIndicator, a 16dp three-bar equalizer that moves while
  playing, freezes while paused, stays still under reduced motion and
  stops its ticker under a disabled TickerMode.
- Desktop songs table (Material): rows with a media id follow
  mediaItemPlaybackUiProvider one row at a time. The current row fades
  to primaryContainer on the effects spring, takes onPrimaryContainer
  text with a bold title, and shows the glyph in place of the track
  number, or over the artwork when the table has no number column. Row
  height and columns stay put. A selected current row keeps the
  secondaryContainer fill. Hover is the 8% state layer of the content
  color.
- TrackCard (Material): the current row fills with primaryContainer,
  rounds its ends into a pill on the spatial spring through a clip
  inside its margin, shows the glyph before the trailing control and can
  round its artwork toward a circle. Other rows keep their connected
  corners. Selected rows use solid secondaryContainer.
- Library list and table rows and downloaded album rows pass their
  media id; downloaded album rows now show the real play and pause
  state and toggle the current track instead of restarting it. Local
  album table rows pass their media id too.
- Album and Library row play buttons use a tonal secondaryContainer
  container on Material.

Mornye keeps its rows as before.
2026-10-21 22:37:34 +07:00

141 lines
3.9 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
/// Three equalizer bars that mark the current track in a list.
///
/// The bars move while [playing] and freeze where they are once playback
/// pauses. Reduced motion keeps them still, and the ticker stops while a
/// disabled [TickerMode] hides the glyph, such as on a covered route.
///
/// The glyph is decorative: the row it sits in carries the label, so it
/// adds nothing to the semantics tree.
class NowPlayingIndicator extends StatefulWidget {
const NowPlayingIndicator({
super.key,
required this.playing,
this.color,
this.size = 16,
});
/// Whether playback is running. False holds the bars in place.
final bool playing;
/// Bar color. Defaults to [ColorScheme.onPrimaryContainer], the content
/// color of the current row.
final Color? color;
/// Width and height of the square glyph.
final double size;
@override
State<NowPlayingIndicator> createState() => _NowPlayingIndicatorState();
}
class _NowPlayingIndicatorState extends State<NowPlayingIndicator>
with SingleTickerProviderStateMixin {
late final _controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 2400),
);
bool _motionEnabled = false;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_motionEnabled =
!MediaQuery.disableAnimationsOf(context) &&
TickerMode.valuesOf(context).enabled;
_syncAnimation();
}
@override
void didUpdateWidget(NowPlayingIndicator oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.playing != widget.playing) _syncAnimation();
}
void _syncAnimation() {
if (_motionEnabled && widget.playing) {
if (!_controller.isAnimating) _controller.repeat();
} else {
_controller.stop();
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final color =
widget.color ?? Theme.of(context).colorScheme.onPrimaryContainer;
return ExcludeSemantics(
child: RepaintBoundary(
child: CustomPaint(
size: Size.square(widget.size),
painter: _EqualizerPainter(progress: _controller, color: color),
),
),
);
}
}
class _EqualizerPainter extends CustomPainter {
_EqualizerPainter({required this.progress, required this.color})
: super(repaint: progress);
final Animation<double> progress;
final Color color;
/// Two waves per bar, each a whole number of cycles per loop so the loop
/// has no seam. At rest (progress 0) the bars read tall, taller, short.
static const _waves = [
(cycles: (3, 5), phase: (0.0, 0.25)),
(cycles: (4, 7), phase: (0.4, 0.1)),
(cycles: (2, 6), phase: (0.7, 0.55)),
];
static const _minFraction = 0.25;
static double _fraction(int bar, double t) {
final wave = _waves[bar];
final primary = math.sin(
2 * math.pi * (wave.cycles.$1 * t + wave.phase.$1),
);
final secondary = math.sin(
2 * math.pi * (wave.cycles.$2 * t + wave.phase.$2),
);
final mix = 0.5 + 0.5 * (0.6 * primary + 0.4 * secondary);
return _minFraction + (1 - _minFraction) * mix;
}
@override
void paint(Canvas canvas, Size size) {
// Three bars with two equal gaps, rising from the baseline.
final barWidth = size.width / 5;
final paint = Paint()..color = color;
for (var bar = 0; bar < _waves.length; bar++) {
final height = size.height * _fraction(bar, progress.value);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(
bar * barWidth * 2,
size.height - height,
barWidth,
height,
),
Radius.circular(barWidth / 2),
),
paint,
);
}
}
@override
bool shouldRepaint(_EqualizerPainter oldDelegate) =>
color != oldDelegate.color || progress != oldDelegate.progress;
}