Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Car mode: responsive multi-window layout, replace sleep with speed/bookmark

Nathan Poulsonfe8347135088
diff --git a/lib/screens/car_mode_screen.dart b/lib/screens/car_mode_screen.dart
index d3da24b6..17694469 100644
--- a/lib/screens/car_mode_screen.dart
+++ b/lib/screens/car_mode_screen.dart
@@ -7,8 +7,8 @@ import 'package:cached_network_image/cached_network_image.dart';
import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
import '../services/audio_player_service.dart';
-import '../services/sleep_timer_service.dart';
-import '../widgets/sleep_timer_sheet.dart';
+import '../services/bookmark_service.dart';
+import '../widgets/card_buttons.dart';
class CarModeScreen extends StatefulWidget {
final AudioPlayerService player;
@@ -174,163 +174,73 @@ class _CarModeScreenState extends State<CarModeScreen>
final coverUrl = _getCoverUrl(context);
final auth = context.read<AuthProvider>();
final chapterTitle = _currentChapterTitle();
- final sleepTimer = SleepTimerService();
final hasChapters = player.chapters.length > 1;
return Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
- child: Column(
- children: [
- // Top bar
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
- child: Row(
- children: [
- IconButton(
- icon: const Icon(Icons.close_rounded, color: Colors.white, size: 32),
- onPressed: () => Navigator.of(context).pop(),
- ),
- const Spacer(),
- const Icon(Icons.directions_car_rounded, color: Colors.white54, size: 22),
- const SizedBox(width: 6),
- const Text('Car Mode', style: TextStyle(color: Colors.white54, fontSize: 16, fontWeight: FontWeight.w600)),
- const SizedBox(width: 16),
- ],
- ),
- ),
-
- const Spacer(flex: 1),
-
- // Book progress bar (above cover)
- StreamBuilder<Duration>(
- stream: player.positionStream,
- builder: (context, snapshot) {
- final pos = snapshot.data ?? player.position;
- final total = Duration(seconds: player.totalDuration.round());
- final bookProgress = total.inMilliseconds > 0
- ? (pos.inMilliseconds / total.inMilliseconds).clamp(0.0, 1.0)
- : 0.0;
- final bookRemaining = total - pos;
-
- return Padding(
- padding: const EdgeInsets.symmetric(horizontal: 32),
- child: Column(
- children: [
- const Text('Book', style: TextStyle(color: Colors.white70, fontSize: 14, fontWeight: FontWeight.w700)),
- const SizedBox(height: 6),
- ClipRRect(
- borderRadius: BorderRadius.circular(4),
- child: LinearProgressIndicator(
- value: bookProgress,
- minHeight: 6,
- backgroundColor: Colors.white12,
- valueColor: const AlwaysStoppedAnimation(Colors.white70),
- ),
- ),
- const SizedBox(height: 4),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(_formatDuration(pos),
- style: const TextStyle(color: Colors.white70, fontSize: 18, fontWeight: FontWeight.w700)),
- Text(_formatRemaining(bookRemaining),
- style: const TextStyle(color: Colors.white70, fontSize: 18, fontWeight: FontWeight.w700)),
- ],
- ),
- ],
- ),
- );
- },
- ),
-
- const SizedBox(height: 16),
-
- // Cover art
- if (coverUrl != null)
+ child: LayoutBuilder(builder: (context, constraints) {
+ final h = constraints.maxHeight;
+ // Compact when in multi-window / small height
+ final compact = h < 500;
+ final titleSize = compact ? 20.0 : 28.0;
+ final authorSize = compact ? 15.0 : 20.0;
+ final chapterTitleSize = compact ? 13.0 : 17.0;
+ final timeSize = compact ? 14.0 : 18.0;
+ final labelSize = compact ? 12.0 : 14.0;
+ final playSize = compact ? 72.0 : 96.0;
+ final playIconSize = compact ? 36.0 : 48.0;
+ final skipSize = compact ? 60.0 : 80.0;
+ final skipIconSize = compact ? 40.0 : 52.0;
+ final skipGap = compact ? 16.0 : 24.0;
+ final hPad = compact ? 24.0 : 32.0;
+ final coverPad = compact ? 32.0 : 48.0;
+ final bottomIconSize = compact ? 28.0 : 36.0;
+ final closeSize = compact ? 26.0 : 32.0;
+
+ return Column(
+ children: [
+ // Top bar
Padding(
- padding: const EdgeInsets.symmetric(horizontal: 48),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(16),
- child: AspectRatio(
- aspectRatio: 1,
- child: _isLocalCover
- ? Image.file(File(coverUrl), fit: BoxFit.cover,
- errorBuilder: (_, __, ___) => _placeholderCover(cs))
- : CachedNetworkImage(
- imageUrl: coverUrl,
- fit: BoxFit.cover,
- httpHeaders: auth.apiService?.mediaHeaders ?? {},
- errorWidget: (_, __, ___) => _placeholderCover(cs),
- ),
- ),
- ),
- ),
-
- const SizedBox(height: 24),
-
- // Title
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 32),
- child: Text(
- title,
- style: const TextStyle(
- color: Colors.white,
- fontSize: 28,
- fontWeight: FontWeight.bold,
- ),
- textAlign: TextAlign.center,
- maxLines: 2,
- overflow: TextOverflow.ellipsis,
- ),
- ),
- if (author.isNotEmpty) ...[
- const SizedBox(height: 6),
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 32),
- child: Text(
- author,
- style: const TextStyle(color: Colors.white70, fontSize: 20, fontWeight: FontWeight.w600),
- textAlign: TextAlign.center,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- ),
- ),
- ],
- if (chapterTitle.isNotEmpty) ...[
- const SizedBox(height: 8),
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 32),
- child: Text(
- chapterTitle,
- style: const TextStyle(color: Colors.white54, fontSize: 17, fontWeight: FontWeight.w600),
- textAlign: TextAlign.center,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
+ padding: EdgeInsets.symmetric(horizontal: 8, vertical: compact ? 0 : 4),
+ child: Row(
+ children: [
+ IconButton(
+ icon: Icon(Icons.close_rounded, color: Colors.white, size: closeSize),
+ onPressed: () => Navigator.of(context).pop(),
+ ),
+ const Spacer(),
+ Icon(Icons.directions_car_rounded, color: Colors.white54, size: compact ? 18 : 22),
+ const SizedBox(width: 6),
+ Text('Car Mode', style: TextStyle(color: Colors.white54, fontSize: compact ? 13 : 16, fontWeight: FontWeight.w600)),
+ const SizedBox(width: 16),
+ ],
),
),
- ],
-
- const SizedBox(height: 20),
- // Chapter progress
- if (hasChapters)
+ // Book progress bar
StreamBuilder<Duration>(
stream: player.positionStream,
builder: (context, snapshot) {
- final (chProgress, chElapsed, chRemaining) = _chapterProgress();
+ final pos = snapshot.data ?? player.position;
+ final total = Duration(seconds: player.totalDuration.round());
+ final bookProgress = total.inMilliseconds > 0
+ ? (pos.inMilliseconds / total.inMilliseconds).clamp(0.0, 1.0)
+ : 0.0;
+ final bookRemaining = total - pos;
return Padding(
- padding: const EdgeInsets.symmetric(horizontal: 32),
+ padding: EdgeInsets.symmetric(horizontal: hPad),
child: Column(
children: [
- const Text('Chapter', style: TextStyle(color: Colors.white70, fontSize: 14, fontWeight: FontWeight.w700)),
- const SizedBox(height: 6),
+ if (!compact)
+ Text('Book', style: TextStyle(color: Colors.white70, fontSize: labelSize, fontWeight: FontWeight.w700)),
+ SizedBox(height: compact ? 2 : 6),
ClipRRect(
borderRadius: BorderRadius.circular(4),
child: LinearProgressIndicator(
- value: chProgress,
- minHeight: 6,
+ value: bookProgress,
+ minHeight: compact ? 4 : 6,
backgroundColor: Colors.white12,
valueColor: const AlwaysStoppedAnimation(Colors.white70),
),
@@ -339,10 +249,10 @@ class _CarModeScreenState extends State<CarModeScreen>
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
- Text(_formatDuration(chElapsed),
- style: const TextStyle(color: Colors.white70, fontSize: 18, fontWeight: FontWeight.w700)),
- Text(_formatRemaining(chRemaining),
- style: const TextStyle(color: Colors.white70, fontSize: 18, fontWeight: FontWeight.w700)),
+ Text(_formatDuration(pos),
+ style: TextStyle(color: Colors.white70, fontSize: timeSize, fontWeight: FontWeight.w700)),
+ Text(_formatRemaining(bookRemaining),
+ style: TextStyle(color: Colors.white70, fontSize: timeSize, fontWeight: FontWeight.w700)),
],
),
],
@@ -351,152 +261,272 @@ class _CarModeScreenState extends State<CarModeScreen>
},
),
- const Spacer(flex: 1),
-
- // Playback controls
- StreamBuilder<PlayerState>(
- stream: player.hasBook ? player.playerStateStream : const Stream.empty(),
- builder: (_, snapshot) {
- final playing = snapshot.data?.playing ?? player.isPlaying;
- final processingState = snapshot.data?.processingState ?? ProcessingState.ready;
- final isLoading = _isStarting || (player.hasBook &&
- (processingState == ProcessingState.loading ||
- processingState == ProcessingState.buffering));
-
- if (playing) {
- _playPauseController.forward();
- } else {
- _playPauseController.reverse();
- }
-
- return Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- GestureDetector(
- onTap: player.hasBook
- ? () => player.skipBackward(_backSkip)
- : null,
- child: SizedBox(
- width: 80,
- height: 80,
- child: Center(
- child: _buildSkipIcon(_backSkip, false, player.hasBook),
- ),
- ),
+ // Cover art - fills remaining space
+ Expanded(
+ child: Padding(
+ padding: EdgeInsets.symmetric(horizontal: coverPad, vertical: compact ? 4 : 8),
+ child: Center(
+ child: AspectRatio(
+ aspectRatio: 1,
+ child: coverUrl != null
+ ? ClipRRect(
+ borderRadius: BorderRadius.circular(compact ? 10 : 16),
+ child: _isLocalCover
+ ? Image.file(File(coverUrl), fit: BoxFit.cover,
+ errorBuilder: (_, __, ___) => _placeholderCover(cs))
+ : CachedNetworkImage(
+ imageUrl: coverUrl,
+ fit: BoxFit.cover,
+ httpHeaders: auth.apiService?.mediaHeaders ?? {},
+ errorWidget: (_, __, ___) => _placeholderCover(cs),
+ ),
+ )
+ : _placeholderCover(cs),
),
- const SizedBox(width: 24),
- GestureDetector(
- onTap: player.hasBook
- ? player.togglePlayPause
- : widget.itemId != null ? _startPlayback : null,
- child: Container(
- width: 96,
- height: 96,
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- color: Colors.white,
- boxShadow: [
- BoxShadow(
- color: Colors.white.withValues(alpha: 0.15),
- blurRadius: 30,
- spreadRadius: -5,
+ ),
+ ),
+ ),
+
+ // Title
+ Padding(
+ padding: EdgeInsets.symmetric(horizontal: hPad),
+ child: Text(
+ title,
+ style: TextStyle(color: Colors.white, fontSize: titleSize, fontWeight: FontWeight.bold),
+ textAlign: TextAlign.center,
+ maxLines: compact ? 1 : 2,
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ if (author.isNotEmpty) ...[
+ SizedBox(height: compact ? 2 : 6),
+ Padding(
+ padding: EdgeInsets.symmetric(horizontal: hPad),
+ child: Text(
+ author,
+ style: TextStyle(color: Colors.white70, fontSize: authorSize, fontWeight: FontWeight.w600),
+ textAlign: TextAlign.center,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ ],
+ if (chapterTitle.isNotEmpty) ...[
+ SizedBox(height: compact ? 2 : 8),
+ Padding(
+ padding: EdgeInsets.symmetric(horizontal: hPad),
+ child: Text(
+ chapterTitle,
+ style: TextStyle(color: Colors.white54, fontSize: chapterTitleSize, fontWeight: FontWeight.w600),
+ textAlign: TextAlign.center,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ ],
+
+ SizedBox(height: compact ? 4 : 12),
+
+ // Chapter progress
+ if (hasChapters && !compact)
+ StreamBuilder<Duration>(
+ stream: player.positionStream,
+ builder: (context, snapshot) {
+ final (chProgress, chElapsed, chRemaining) = _chapterProgress();
+
+ return Padding(
+ padding: EdgeInsets.symmetric(horizontal: hPad),
+ child: Column(
+ children: [
+ Text('Chapter', style: TextStyle(color: Colors.white70, fontSize: labelSize, fontWeight: FontWeight.w700)),
+ const SizedBox(height: 6),
+ ClipRRect(
+ borderRadius: BorderRadius.circular(4),
+ child: LinearProgressIndicator(
+ value: chProgress,
+ minHeight: 6,
+ backgroundColor: Colors.white12,
+ valueColor: const AlwaysStoppedAnimation(Colors.white70),
),
- ],
+ ),
+ const SizedBox(height: 4),
+ Row(
+ mainAxisAlignment: MainAxisAlignment.spaceBetween,
+ children: [
+ Text(_formatDuration(chElapsed),
+ style: TextStyle(color: Colors.white70, fontSize: timeSize, fontWeight: FontWeight.w700)),
+ Text(_formatRemaining(chRemaining),
+ style: TextStyle(color: Colors.white70, fontSize: timeSize, fontWeight: FontWeight.w700)),
+ ],
+ ),
+ ],
+ ),
+ );
+ },
+ ),
+
+ SizedBox(height: compact ? 4 : 16),
+
+ // Playback controls
+ StreamBuilder<PlayerState>(
+ stream: player.hasBook ? player.playerStateStream : const Stream.empty(),
+ builder: (_, snapshot) {
+ final playing = snapshot.data?.playing ?? player.isPlaying;
+ final processingState = snapshot.data?.processingState ?? ProcessingState.ready;
+ final isLoading = _isStarting || (player.hasBook &&
+ (processingState == ProcessingState.loading ||
+ processingState == ProcessingState.buffering));
+
+ if (playing) {
+ _playPauseController.forward();
+ } else {
+ _playPauseController.reverse();
+ }
+
+ return Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ GestureDetector(
+ onTap: player.hasBook
+ ? () => player.skipBackward(_backSkip)
+ : null,
+ child: SizedBox(
+ width: skipSize,
+ height: skipSize,
+ child: Center(
+ child: _buildSkipIcon(_backSkip, false, player.hasBook, skipIconSize),
+ ),
),
- child: isLoading
- ? const Center(
- child: SizedBox(
- width: 36,
- height: 36,
- child: CircularProgressIndicator(
- strokeWidth: 3,
+ ),
+ SizedBox(width: skipGap),
+ GestureDetector(
+ onTap: player.hasBook
+ ? player.togglePlayPause
+ : widget.itemId != null ? _startPlayback : null,
+ child: Container(
+ width: playSize,
+ height: playSize,
+ decoration: BoxDecoration(
+ shape: BoxShape.circle,
+ color: Colors.white,
+ boxShadow: [
+ BoxShadow(
+ color: Colors.white.withValues(alpha: 0.15),
+ blurRadius: 30,
+ spreadRadius: -5,
+ ),
+ ],
+ ),
+ child: isLoading
+ ? Center(
+ child: SizedBox(
+ width: playIconSize * 0.75,
+ height: playIconSize * 0.75,
+ child: const CircularProgressIndicator(
+ strokeWidth: 3,
+ color: Colors.black,
+ ),
+ ),
+ )
+ : Center(
+ child: AnimatedIcon(
+ icon: AnimatedIcons.play_pause,
+ progress: _playPauseController,
+ size: playIconSize,
color: Colors.black,
),
),
- )
- : Center(
- child: AnimatedIcon(
- icon: AnimatedIcons.play_pause,
- progress: _playPauseController,
- size: 48,
- color: Colors.black,
- ),
- ),
- ),
- ),
- const SizedBox(width: 24),
- GestureDetector(
- onTap: player.hasBook
- ? () => player.skipForward(_forwardSkip)
- : null,
- child: SizedBox(
- width: 80,
- height: 80,
- child: Center(
- child: _buildSkipIcon(_forwardSkip, true, player.hasBook),
),
),
- ),
- ],
- );
- },
- ),
-
- const SizedBox(height: 16),
-
- // Bottom row
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- IconButton(
- icon: const Icon(Icons.skip_previous_rounded, size: 36),
- color: Colors.white70,
- onPressed: player.hasBook ? player.skipToPreviousChapter : null,
- ),
- const SizedBox(width: 24),
- ListenableBuilder(
- listenable: sleepTimer,
- builder: (_, __) {
- return IconButton(
- icon: Icon(
- sleepTimer.isActive
- ? Icons.bedtime_rounded
- : Icons.bedtime_outlined,
- size: 32,
- ),
- color: sleepTimer.isActive ? Colors.amber : Colors.white70,
- onPressed: () {
- showModalBottomSheet(
- context: context,
- backgroundColor: Theme.of(context).bottomSheetTheme.backgroundColor,
- shape: const RoundedRectangleBorder(
- borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
- ),
- builder: (_) => const SleepTimerSheet(
- accent: Colors.white,
+ SizedBox(width: skipGap),
+ GestureDetector(
+ onTap: player.hasBook
+ ? () => player.skipForward(_forwardSkip)
+ : null,
+ child: SizedBox(
+ width: skipSize,
+ height: skipSize,
+ child: Center(
+ child: _buildSkipIcon(_forwardSkip, true, player.hasBook, skipIconSize),
),
- );
- },
- );
- },
- ),
- const SizedBox(width: 24),
- IconButton(
- icon: const Icon(Icons.skip_next_rounded, size: 36),
- color: Colors.white70,
- onPressed: player.hasBook ? player.skipToNextChapter : null,
- ),
- ],
- ),
+ ),
+ ),
+ ],
+ );
+ },
+ ),
- const Spacer(flex: 1),
- ],
- ),
+ SizedBox(height: compact ? 2 : 8),
+
+ // Bottom row
+ Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ IconButton(
+ icon: Icon(Icons.skip_previous_rounded, size: bottomIconSize),
+ color: Colors.white70,
+ onPressed: player.hasBook ? player.skipToPreviousChapter : null,
+ ),
+ SizedBox(width: compact ? 12 : 20),
+ IconButton(
+ icon: Icon(Icons.speed_rounded, size: bottomIconSize * 0.9),
+ color: Colors.white70,
+ onPressed: () {
+ showModalBottomSheet(
+ context: context,
+ backgroundColor: Colors.transparent,
+ useSafeArea: true,
+ builder: (_) => CardSpeedSheet(
+ player: player,
+ accent: Colors.white,
+ itemId: player.currentItemId,
+ ),
+ );
+ },
+ ),
+ SizedBox(width: compact ? 12 : 20),
+ IconButton(
+ icon: Icon(Icons.bookmark_add_outlined, size: bottomIconSize * 0.9),
+ color: Colors.white70,
+ onPressed: player.hasBook ? () {
+ final pos = player.position.inMilliseconds / 1000.0;
+ final chTitle = _currentChapterTitle();
+ final itemId = player.currentEpisodeId != null
+ ? '${player.currentItemId}-${player.currentEpisodeId}'
+ : player.currentItemId;
+ if (itemId == null) return;
+ BookmarkService().addBookmark(
+ itemId: itemId,
+ positionSeconds: pos,
+ title: chTitle.isNotEmpty ? chTitle : 'Bookmark',
+ );
+ ScaffoldMessenger.of(context).showSnackBar(
+ const SnackBar(
+ content: Text('Bookmark added'),
+ duration: Duration(seconds: 2),
+ behavior: SnackBarBehavior.floating,
+ ),
+ );
+ } : null,
+ ),
+ SizedBox(width: compact ? 12 : 20),
+ IconButton(
+ icon: Icon(Icons.skip_next_rounded, size: bottomIconSize),
+ color: Colors.white70,
+ onPressed: player.hasBook ? player.skipToNextChapter : null,
+ ),
+ ],
+ ),
+
+ SizedBox(height: compact ? 2 : 8),
+ ],
+ );
+ }),
),
);
}
- Widget _buildSkipIcon(int seconds, bool isForward, bool active) {
+ Widget _buildSkipIcon(int seconds, bool isForward, bool active, [double iconSize = 52]) {
final hasBuiltIn = [5, 10, 30].contains(seconds);
final color = active ? Colors.white70 : Colors.white24;
if (hasBuiltIn) {
@@ -514,12 +544,12 @@ class _CarModeScreenState extends State<CarModeScreen>
? Icons.replay_10_rounded
: Icons.replay_30_rounded;
}
- return Icon(icon, size: 52, color: color);
+ return Icon(icon, size: iconSize, color: color);
}
return Stack(alignment: Alignment.center, children: [
Icon(
isForward ? Icons.rotate_right_rounded : Icons.rotate_left_rounded,
- size: 52,
+ size: iconSize,
color: color,
),
Padding(
@@ -527,7 +557,7 @@ class _CarModeScreenState extends State<CarModeScreen>
child: Text(
'$seconds',
style: TextStyle(
- fontSize: 14,
+ fontSize: iconSize * 0.27,
fontWeight: FontWeight.w900,
color: color,
),