fix(motion): pause loading effects when hidden or motion is reduced

This commit is contained in:
zarzet
2026-09-28 11:58:50 +07:00
parent 987245de71
commit c5a262b865
4 changed files with 119 additions and 18 deletions
+2 -1
View File
@@ -38,6 +38,7 @@ import 'package:spotiflac_android/widgets/disc_separator_chip.dart';
import 'package:spotiflac_android/widgets/album_detail_header.dart';
import 'package:spotiflac_android/widgets/mornye_artist_header.dart';
import 'package:spotiflac_android/widgets/motion_header_banner.dart';
import 'package:spotiflac_android/widgets/app_loading_indicator.dart';
class DownloadedAlbumScreen extends ConsumerStatefulWidget {
final String albumName;
@@ -270,7 +271,7 @@ class _DownloadedAlbumScreenState extends ConsumerState<DownloadedAlbumScreen>
if (tracks.isEmpty && tracksValue.isLoading) {
return Scaffold(
appBar: AppBar(title: Text(widget.albumName)),
body: const Center(child: CircularProgressIndicator()),
body: const Center(child: AppLoadingIndicator()),
);
}
+18 -2
View File
@@ -28,7 +28,11 @@ class StaggeredListItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
if (!animate || index >= maxAnimatedItems) return child;
if (!animate ||
index >= maxAnimatedItems ||
MediaQuery.disableAnimationsOf(context)) {
return child;
}
final cappedIndex = index.clamp(0, maxAnimatedItems - 1);
final delay = staggerDelay * cappedIndex;
final totalDuration = duration + delay;
@@ -87,7 +91,18 @@ class _ShimmerLoadingState extends State<ShimmerLoading>
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1200),
)..repeat();
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (MediaQuery.disableAnimationsOf(context) ||
!TickerMode.valuesOf(context).enabled) {
_controller.stop();
} else if (!_controller.isAnimating) {
_controller.repeat();
}
}
@override
@@ -98,6 +113,7 @@ class _ShimmerLoadingState extends State<ShimmerLoading>
@override
Widget build(BuildContext context) {
if (MediaQuery.disableAnimationsOf(context)) return widget.child;
final colorScheme = Theme.of(context).colorScheme;
final isDark = Theme.of(context).brightness == Brightness.dark;
+25 -15
View File
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:material_3_expressive/material_3_expressive.dart';
import 'package:spotiflac_android/theme/material_expressive.dart';
import 'package:spotiflac_android/theme/mornye_theme.dart';
/// Indeterminate loading only. Downloads/analysis keep their real progress.
class AppLoadingIndicator extends StatelessWidget {
@@ -16,22 +17,31 @@ class AppLoadingIndicator extends StatelessWidget {
final String? semanticLabel;
@override
Widget build(BuildContext context) => SizedBox.square(
dimension: size,
child: materialExpressiveEnabled(context)
? MaterialExpressiveScope(
child: FittedBox(
child: M3ELoadingIndicator(
size: 48,
Widget build(BuildContext context) {
final animate =
!MediaQuery.disableAnimationsOf(context) &&
TickerMode.valuesOf(context).enabled;
return SizedBox.square(
dimension: size,
child: !context.isMornye
? MaterialExpressiveScope(
child: FittedBox(
child: M3ELoadingIndicator(
size: 48,
color: color,
rotationTurns: animate ? null : 0,
semanticLabel: semanticLabel,
),
),
)
: TickerMode(
enabled: animate,
child: CircularProgressIndicator(
color: color,
semanticLabel: semanticLabel,
strokeWidth: 3,
semanticsLabel: semanticLabel,
),
),
)
: CircularProgressIndicator(
color: color,
strokeWidth: 3,
semanticsLabel: semanticLabel,
),
);
);
}
}
+74
View File
@@ -0,0 +1,74 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:material_3_expressive/material_3_expressive.dart';
import 'package:spotiflac_android/theme/app_theme.dart';
import 'package:spotiflac_android/widgets/animation_utils.dart';
import 'package:spotiflac_android/widgets/app_loading_indicator.dart';
void main() {
for (final reduced in [false, true]) {
testWidgets(
'loading stops when ${reduced ? 'motion is reduced' : 'its tab is hidden'} and resumes',
(tester) async {
var stopped = false;
late StateSetter update;
await tester.pumpWidget(
MaterialApp(
theme: AppTheme.light(),
home: StatefulBuilder(
builder: (context, setState) {
update = setState;
return MediaQuery(
data: MediaQuery.of(
context,
).copyWith(disableAnimations: reduced && stopped),
child: TickerMode(
enabled: reduced || !stopped,
child: const Column(
children: [
AppLoadingIndicator(semanticLabel: 'Loading'),
ShimmerLoading(
child: SkeletonBox(width: 100, height: 40),
),
],
),
),
);
},
),
),
);
await tester.pump(const Duration(milliseconds: 100));
expect(tester.binding.hasScheduledFrame, isTrue);
update(() => stopped = true);
await tester.pump();
await tester.pump(const Duration(seconds: 2));
expect(tester.binding.hasScheduledFrame, isFalse);
expect(find.byType(M3ELoadingIndicator), findsOneWidget);
if (reduced) expect(find.byType(ShaderMask), findsNothing);
update(() => stopped = false);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(tester.binding.hasScheduledFrame, isTrue);
expect(find.byType(ShaderMask), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
expect(tester.takeException(), isNull);
},
);
}
testWidgets('reduced motion shows list content immediately', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: MediaQuery(
data: MediaQueryData(disableAnimations: true),
child: StaggeredListItem(index: 8, child: Text('Album')),
),
),
);
expect(find.text('Album').hitTestable(), findsOneWidget);
expect(find.byType(TweenAnimationBuilder<double>), findsNothing);
await tester.pump(const Duration(seconds: 1));
expect(tester.binding.hasScheduledFrame, isFalse);
});
}