2025-11-13 17:53:23 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2025-12-08 17:54:08 +08:00
|
|
|
import 'package:vaani/features/player/providers/abs_provider.dart'
|
|
|
|
|
hide PlayerState;
|
2025-12-09 17:26:04 +08:00
|
|
|
import 'package:vaani/features/player/core/abs_audio_player.dart';
|
2025-11-13 17:53:23 +08:00
|
|
|
|
|
|
|
|
class AudiobookPlayerPlayPauseButton extends HookConsumerWidget {
|
|
|
|
|
const AudiobookPlayerPlayPauseButton({
|
|
|
|
|
super.key,
|
|
|
|
|
this.iconSize = 48.0,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final double iconSize;
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2025-12-08 17:54:08 +08:00
|
|
|
final playerState = ref.watch(playerStateProvider);
|
2025-11-22 15:54:29 +08:00
|
|
|
|
2025-12-08 17:54:08 +08:00
|
|
|
return IconButton(
|
|
|
|
|
icon: _getIcon(playerState, context),
|
|
|
|
|
onPressed: () => _actionButtonPressed(playerState, ref),
|
2025-11-13 17:53:23 +08:00
|
|
|
);
|
2025-11-22 15:54:29 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-08 23:46:43 +08:00
|
|
|
Widget _getIcon(AbsPlayerState playerState, BuildContext context) {
|
2025-12-08 17:54:08 +08:00
|
|
|
if (playerState.playing) {
|
|
|
|
|
return Icon(size: iconSize, Icons.pause);
|
|
|
|
|
} else {
|
|
|
|
|
switch (playerState.processingState) {
|
2025-12-08 23:46:43 +08:00
|
|
|
case AbsProcessingState.loading || AbsProcessingState.buffering:
|
2025-12-08 17:54:08 +08:00
|
|
|
return CircularProgressIndicator();
|
|
|
|
|
default:
|
|
|
|
|
return Icon(size: iconSize, Icons.play_arrow);
|
|
|
|
|
}
|
2025-11-22 15:54:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 23:46:43 +08:00
|
|
|
void _actionButtonPressed(AbsPlayerState playerState, WidgetRef ref) async {
|
2025-12-09 17:26:04 +08:00
|
|
|
final player = ref.read(audioPlayerProvider);
|
2025-12-08 17:54:08 +08:00
|
|
|
if (playerState.playing) {
|
|
|
|
|
await player.pause();
|
|
|
|
|
} else {
|
|
|
|
|
switch (playerState.processingState) {
|
2025-12-08 23:46:43 +08:00
|
|
|
case AbsProcessingState.completed:
|
2025-12-08 17:54:08 +08:00
|
|
|
await player.seekInBook(const Duration(seconds: 0));
|
|
|
|
|
await player.play();
|
|
|
|
|
default:
|
|
|
|
|
await player.play();
|
|
|
|
|
}
|
2025-11-13 17:53:23 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|