Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add press feedback to all card and playback buttons

Nathan Poulson0df97e6361e5
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index 898819b1..88ee6753 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -778,7 +778,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
listenable: ChromecastService(),
builder: (context, _) {
final castActive = ChromecastService().isCasting && !_buttonOrder.take(_visibleButtonCount).contains('cast');
- return GestureDetector(
+ return Pressable(
behavior: HitTestBehavior.opaque,
onTap: () => _showMoreMenu(context, accent, tt),
child: Container(
diff --git a/lib/widgets/card_buttons.dart b/lib/widgets/card_buttons.dart
index a29b1ae2..9a7961eb 100644
--- a/lib/widgets/card_buttons.dart
+++ b/lib/widgets/card_buttons.dart
@@ -9,6 +9,41 @@ import 'audio_output_sheet.dart';
import 'card_button_config.dart';
import 'sleep_timer_sheet.dart';
+/// Wrapper that gives any child a press-down opacity+scale effect.
+class Pressable extends StatefulWidget {
+ final VoidCallback? onTap;
+ final VoidCallback? onLongPress;
+ final HitTestBehavior? behavior;
+ final Widget child;
+ const Pressable({super.key, this.onTap, this.onLongPress, this.behavior, required this.child});
+ @override State<Pressable> createState() => _PressableState();
+}
+
+class _PressableState extends State<Pressable> {
+ bool _pressed = false;
+
+ @override
+ Widget build(BuildContext context) {
+ return GestureDetector(
+ behavior: widget.behavior,
+ onTap: widget.onTap,
+ onLongPress: widget.onLongPress,
+ onTapDown: (_) => setState(() => _pressed = true),
+ onTapUp: (_) => setState(() => _pressed = false),
+ onTapCancel: () => setState(() => _pressed = false),
+ child: AnimatedScale(
+ scale: _pressed ? 0.90 : 1.0,
+ duration: Duration(milliseconds: _pressed ? 0 : 150),
+ child: AnimatedOpacity(
+ opacity: _pressed ? 0.4 : 1.0,
+ duration: Duration(milliseconds: _pressed ? 0 : 150),
+ child: widget.child,
+ ),
+ ),
+ );
+ }
+}
+
/// Show a toast when the user taps a button that requires active playback.
void showInactiveToast(BuildContext context) {
ScaffoldMessenger.of(context)
@@ -60,7 +95,7 @@ class CardWideButton extends StatelessWidget {
final fontSize = compact ? 10.0 : (large ? 13.0 : 11.0);
final vPad = compact ? 8.0 : (large ? 14.0 : 10.0);
final radius = compact ? 10.0 : (large ? 14.0 : 12.0);
- return GestureDetector(
+ return Pressable(
onTap: enabled ? onTap : () => showInactiveToast(context),
child: Container(
padding: EdgeInsets.symmetric(vertical: vPad),
@@ -103,7 +138,7 @@ class MoreMenuItem extends StatelessWidget {
@override Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
- return GestureDetector(
+ return Pressable(
onTap: enabled ? onTap : () => showInactiveToast(context),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 14, horizontal: 16),
@@ -162,7 +197,7 @@ class CardSleepButtonInline extends StatelessWidget {
final fontSize = compact ? 10.0 : (large ? (active && isTime ? 15.0 : 14.0) : (active && isTime ? 13.0 : 12.0));
final radius = compact ? 10.0 : (large ? 16.0 : 14.0);
- return GestureDetector(
+ return Pressable(
onTap: isActive ? () {
showSleepTimerSheet(context, accent);
} : () => showInactiveToast(context),
@@ -242,7 +277,7 @@ class _CardBookmarkButtonInlineState extends State<CardBookmarkButtonInline> {
final radius = cp ? 10.0 : (lg ? 14.0 : 12.0);
final sh = widget.short;
final label = cp ? (_count > 0 ? '$_count' : 'Bookmark') : sh ? 'Bookmarks' : (_count > 0 ? 'Bookmarks ($_count)' : 'Bookmark');
- return GestureDetector(
+ return Pressable(
onTap: enabled ? () => _showBookmarks(context) : () => showInactiveToast(context),
onLongPress: enabled ? () => _quickAdd(context) : null,
child: Container(
@@ -355,7 +390,7 @@ class _CardSpeedButtonInlineState extends State<CardSpeedButtonInline> {
builder: (context, _) {
final castNow = widget.itemId != null && cast.isCasting && cast.castingItemId == widget.itemId;
final speedNow = castNow ? cast.castSpeed : (widget.isActive ? widget.player.speed : _savedSpeed);
- return GestureDetector(
+ return Pressable(
onTap: () {
showModalBottomSheet(context: context, backgroundColor: Colors.transparent,
useSafeArea: true,
diff --git a/lib/widgets/card_playback_controls.dart b/lib/widgets/card_playback_controls.dart
index ce3d6349..b8c947d4 100644
--- a/lib/widgets/card_playback_controls.dart
+++ b/lib/widgets/card_playback_controls.dart
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import '../services/audio_player_service.dart';
import '../services/chromecast_service.dart';
+import 'card_buttons.dart';
// ─── PLAYBACK CONTROLS (card version) ───────────────────────
@@ -12,7 +13,8 @@ class CardPlaybackControls extends StatefulWidget {
final VoidCallback onStart;
final String? itemId;
final bool showPlayButton;
- const CardPlaybackControls({super.key, required this.player, required this.accent, required this.isActive, required this.isStarting, required this.onStart, this.itemId, this.showPlayButton = false});
+ final double playButtonSize;
+ const CardPlaybackControls({super.key, required this.player, required this.accent, required this.isActive, required this.isStarting, required this.onStart, this.itemId, this.showPlayButton = false, this.playButtonSize = 65});
@override State<CardPlaybackControls> createState() => _CardPlaybackControlsState();
}
@@ -72,18 +74,21 @@ class _CardPlaybackControlsState extends State<CardPlaybackControls> {
}
Widget _playPauseButton(ColorScheme cs, {required bool playing, required bool loading, required VoidCallback? onTap}) {
- return GestureDetector(
+ final s = widget.playButtonSize;
+ final iconSize = s * 0.49;
+ final spinnerSize = s * 0.4;
+ return Pressable(
onTap: onTap,
child: Container(
- width: 56, height: 56,
+ width: s, height: s,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: cs.onSurface,
boxShadow: [BoxShadow(color: widget.accent.withValues(alpha: 0.4), blurRadius: 25, spreadRadius: -5)],
),
child: loading
- ? Center(child: SizedBox(width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2, color: widget.accent)))
- : Icon(playing ? Icons.pause_rounded : Icons.play_arrow_rounded, size: 30, color: cs.surface),
+ ? Center(child: SizedBox(width: spinnerSize, height: spinnerSize, child: CircularProgressIndicator(strokeWidth: 2, color: widget.accent)))
+ : Icon(playing ? Icons.pause_rounded : Icons.play_arrow_rounded, size: iconSize, color: cs.surface),
),
);
}
@@ -95,23 +100,23 @@ class _CardPlaybackControlsState extends State<CardPlaybackControls> {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: cast.skipToPreviousChapter,
child: SizedBox(width: 52, height: 52, child: Center(
child: Icon(Icons.skip_previous_rounded, size: 34, color: cs.onSurfaceVariant),
)),
)),
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: () => cast.skipBackward(_backSkip),
child: SizedBox(width: 60, height: 60, child: Center(child: _skipIcon(_backSkip, false))),
)),
if (widget.showPlayButton)
Flexible(child: _playPauseButton(cs, playing: cast.isPlaying, loading: false, onTap: cast.togglePlayPause)),
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: () => cast.skipForward(_forwardSkip),
child: SizedBox(width: 60, height: 60, child: Center(child: _skipIcon(_forwardSkip, true))),
)),
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: cast.skipToNextChapter,
child: SizedBox(width: 52, height: 52, child: Center(
child: Icon(Icons.skip_next_rounded, size: 34, color: cs.onSurfaceVariant),
@@ -128,24 +133,24 @@ class _CardPlaybackControlsState extends State<CardPlaybackControls> {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: widget.isActive ? widget.player.skipToPreviousChapter : null,
child: SizedBox(width: 52, height: 52, child: Center(
child: Icon(Icons.skip_previous_rounded, size: 34, color: widget.isActive ? cs.onSurfaceVariant : cs.onSurface.withValues(alpha: 0.12)),
)),
)),
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: widget.isActive ? () => widget.player.skipBackward(_backSkip) : null,
child: SizedBox(width: 60, height: 60, child: Center(child: _skipIcon(_backSkip, false, active: widget.isActive))),
)),
if (widget.showPlayButton)
Flexible(child: _playPauseButton(cs, playing: widget.isActive && widget.player.isPlaying, loading: loading,
onTap: widget.isActive ? widget.player.togglePlayPause : widget.onStart)),
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: widget.isActive ? () => widget.player.skipForward(_forwardSkip) : null,
child: SizedBox(width: 60, height: 60, child: Center(child: _skipIcon(_forwardSkip, true, active: widget.isActive))),
)),
- Flexible(child: GestureDetector(
+ Flexible(child: Pressable(
onTap: widget.isActive ? widget.player.skipToNextChapter : null,
child: SizedBox(width: 52, height: 52, child: Center(
child: Icon(Icons.skip_next_rounded, size: 34, color: widget.isActive ? cs.onSurfaceVariant : cs.onSurface.withValues(alpha: 0.12)),
diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dart
index 0a90ec72..6962d315 100644
--- a/lib/widgets/expanded_card.dart
+++ b/lib/widgets/expanded_card.dart
@@ -807,7 +807,7 @@ class _ExpandedCardState extends State<ExpandedCard> {
listenable: ChromecastService(),
builder: (context, _) {
final castActive = ChromecastService().isCasting && !_buttonOrder.take(_visibleButtonCount).contains('cast');
- return GestureDetector(
+ return Pressable(
behavior: HitTestBehavior.opaque,
onTap: () => _showMoreMenu(context, accent, tt),
child: Container(