Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Open the ebook reader full screen instead of flipping the card

Barnabas4561962b58f8
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index f37d3a7b..b3fcad18 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -28,45 +28,7 @@ class AbsorbingCard extends StatefulWidget {
State<AbsorbingCard> createState() => AbsorbingCardState();
}
-class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
- late final AnimationController _flipController = AnimationController(
- vsync: this,
- duration: const Duration(milliseconds: 600),
- );
- bool get _showingBack => _flipController.value > 0.5;
- // Lazy-mount the back face so non-readers don't pay the WebView startup cost.
- // Once mounted it stays alive across flips (kept paginated in IndexedStack).
- bool _backInitialized = false;
- final GlobalKey<EbookReaderViewState> _embeddedReaderKey = GlobalKey<EbookReaderViewState>();
- // CFI to seed the embedded reader on mount. Updated when the full-screen
- // reader closes so the re-mounted embedded picks up wherever the user left off.
- String? _readerInitialCfi;
- // Shared layout size for the embedded + full-screen reader pair, locked on
- // first use. Both render the WebView at this exact logical size (the card
- // scales it down) so pagination matches word for word across the handoff.
- // Locked once because viewPadding flickers while immersive mode toggles.
- Size? _readerSizeLock;
- Size _lockReaderSize(BuildContext context) {
- if (_readerSizeLock != null) return _readerSizeLock!;
- final mq = MediaQuery.of(context);
- final vp = mq.viewPadding;
- return _readerSizeLock = Size(
- mq.size.width - vp.left - vp.right,
- mq.size.height - vp.top - vp.bottom,
- );
- }
- void _toggleFlip() {
- if (_flipController.isAnimating) return;
- if (!_backInitialized) {
- setState(() => _backInitialized = true);
- }
- if (_showingBack) {
- _flipController.reverse();
- } else {
- _flipController.forward();
- }
- }
-
+class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveClientMixin {
ColorScheme? _coverScheme;
Brightness? _coverBrightness; // brightness used to generate _coverScheme
ImageProvider? _coverProvider; // cached for re-deriving on theme change
@@ -378,7 +340,6 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
_chapterTrackSub?.cancel();
_blurredCover?.dispose();
_edgeBarExpanded.dispose();
- _flipController.dispose();
super.dispose();
}
@@ -524,38 +485,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
final showBookBar = (!_isPodcastEpisode || _chapters.isNotEmpty) && (!lib.isPodcastLibrary || _chapters.isNotEmpty);
- // Build both faces ONCE per parent rebuild (not per animation frame).
- // The AnimatedBuilder below only updates the rotation matrix; the heavy
- // child trees (front content + embedded reader) are stable references.
- final frontFace = _buildFront(context, cs, accent, isDark, l, lib, progress, chapterIdx, totalChapters, bookProgress, showBookBar);
- final backFace = _backInitialized
- ? Transform(
- alignment: Alignment.center,
- // Counter-rotate so back content reads correctly when card is fully flipped.
- transform: Matrix4.identity()..rotateY(3.1415926535),
- child: _buildBack(context, cs, accent, isDark),
- )
- : const SizedBox.shrink();
-
- return AnimatedBuilder(
- animation: _flipController,
- builder: (context, child) {
- final t = _flipController.value;
- final angle = t * 3.1415926535;
- return Transform(
- alignment: Alignment.center,
- transform: Matrix4.identity()
- ..setEntry(3, 2, 0.0015) // perspective
- ..rotateY(angle),
- child: IndexedStack(
- alignment: Alignment.center,
- sizing: StackFit.passthrough,
- index: t > 0.5 ? 1 : 0,
- children: [frontFace, backFace],
- ),
- );
- },
- );
+ return _buildFront(context, cs, accent, isDark, l, lib, progress, chapterIdx, totalChapters, bookProgress, showBookBar);
}
Widget _buildFront(
@@ -1060,75 +990,15 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
);
}
- Future<void> _openFullscreenReader() async {
+ void _openEbookReader() {
final ebookFile = _ebookFile;
- if (ebookFile == null) return;
- final handoffCfi = await _embeddedReaderKey.currentState?.getLiveCfi();
- debugPrint('[Handoff] small→big handoffCfi=$handoffCfi');
- String? latestCfi;
- // Unmount the embedded reader entirely while full-screen is open. This
- // sidesteps the live-seek-on-hidden-WebView problem — when the route
- // closes we re-mount fresh with the up-to-date CFI as initial position.
- setState(() => _backInitialized = false);
- await Navigator.of(context, rootNavigator: true).push(
+ if (ebookFile == null) return; // CardButtons already toasts when no ebook
+ Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(
builder: (_) => EbookReaderView(
itemId: _itemId,
title: _title,
ebookFile: ebookFile,
- initialCfi: handoffCfi,
- onPositionChanged: (cfi) => latestCfi = cfi,
- viewerSize: _readerSizeLock,
- ),
- ),
- );
- if (!mounted) return;
- setState(() {
- _readerInitialCfi = latestCfi ?? handoffCfi ?? _readerInitialCfi;
- _backInitialized = true;
- });
- }
-
- Widget _buildBack(BuildContext context, ColorScheme cs, Color accent, bool isDark) {
- final tt = Theme.of(context).textTheme;
- final ebookFile = _ebookFile;
- return Container(
- decoration: BoxDecoration(
- color: isDark ? Colors.black : Colors.white,
- borderRadius: BorderRadius.circular(24),
- border: Border.all(color: accent.withValues(alpha: 0.15), width: 1),
- ),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(23),
- child: Stack(
- children: [
- if (ebookFile != null)
- EbookReaderView(
- key: _embeddedReaderKey,
- itemId: _itemId,
- title: _title,
- ebookFile: ebookFile,
- embedded: true,
- initialCfi: _readerInitialCfi,
- onClose: _toggleFlip,
- onExpand: _openFullscreenReader,
- viewerSize: _lockReaderSize(context),
- )
- else
- Padding(
- padding: const EdgeInsets.all(24),
- child: Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(Icons.menu_book_rounded, size: 36, color: cs.onSurface.withValues(alpha: 0.4)),
- const SizedBox(height: 12),
- Text('No ebook attached', style: tt.titleSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.6))),
- ],
- ),
- ),
- ),
- ],
),
),
);
@@ -1265,7 +1135,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
},
hasEbook: _ebookFile != null,
isEbookPdf: _ebookExt == 'pdf',
- onEbookTap: _toggleFlip,
+ onEbookTap: _openEbookReader,
);
int get _visibleButtonCount => _buttonVisibleCount;
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index f0e9e54e..4609b3f2 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -16,40 +16,12 @@ class EbookReaderView extends StatefulWidget {
final String itemId;
final String title;
final Map<String, dynamic> ebookFile;
- /// When true, renders without a Scaffold wrapper / SafeArea / SystemChrome
- /// changes — for use inside another widget like the absorbing card back.
- final bool embedded;
- /// Replaces the back button's behavior. Defaults to Navigator.pop in
- /// full-screen mode; in embedded mode the host should provide one (e.g. to
- /// flip the card back to the front face).
- final VoidCallback? onClose;
- /// Only shown in embedded mode. Tap surfaces an "expand to full screen"
- /// affordance the host can wire up.
- final VoidCallback? onExpand;
- /// If provided, opens the reader at this exact CFI instead of the saved
- /// progress location. Used to hand off position between embedded and
- /// full-screen instances.
- final String? initialCfi;
- /// Fires whenever the reader's position changes. Used by hosts to mirror
- /// position across paired reader instances.
- final ValueChanged<String>? onPositionChanged;
- /// When set, the WebView is laid out at exactly this logical size and
- /// scaled to fit the available space. Paired embedded + full-screen readers
- /// pass the same size so epub.js paginates identically in both - a handed
- /// off CFI then lands on a page starting at the same word.
- final Size? viewerSize;
const EbookReaderView({
super.key,
required this.itemId,
required this.title,
required this.ebookFile,
- this.embedded = false,
- this.onClose,
- this.onExpand,
- this.initialCfi,
- this.onPositionChanged,
- this.viewerSize,
});
@override
@@ -57,22 +29,6 @@ class EbookReaderView extends StatefulWidget {
}
class EbookReaderViewState extends State<EbookReaderView> {
- /// Latest known position of the reader. Null until the first relocate fires.
- String? get currentCfi => _currentCfi;
- /// Asks the controller for the *live* current location. More accurate than
- /// [currentCfi] right after a page flip, since onRelocated may not have
- /// caught up yet. Returns null if the controller isn't ready or errors.
- Future<String?> getLiveCfi() async {
- try {
- final loc = await _epubController?.getCurrentLocation();
- return loc?.startCfi ?? _currentCfi;
- } catch (_) {
- return _currentCfi;
- }
- }
- /// Jump the reader to a specific CFI. Safe to call before the EPUB has loaded;
- /// the controller no-ops in that case.
- void seekTo(String cfi) => _epubController?.display(cfi: cfi);
EpubController? _epubController;
bool _loading = true;
String? _error;
@@ -196,7 +152,6 @@ class EbookReaderViewState extends State<EbookReaderView> {
}
void _setFullscreen(bool fullscreen) {
- if (widget.embedded) return; // host owns system chrome
if (fullscreen) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
} else {
@@ -214,11 +169,6 @@ class EbookReaderViewState extends State<EbookReaderView> {
}
void _loadInitialLocation() {
- // Explicit initialCfi (e.g. handoff from another reader instance) wins.
- if (widget.initialCfi != null && widget.initialCfi!.isNotEmpty) {
- _initialCfi = widget.initialCfi;
- return;
- }
final lib = context.read<LibraryProvider>();
final progressData = lib.getProgressData(widget.itemId);
final loc = progressData?['ebookLocation'] as String?;
@@ -1027,12 +977,8 @@ class EbookReaderViewState extends State<EbookReaderView> {
);
}
- /// Wraps a body in a Scaffold for full-screen mode, or returns it directly
- /// (sized to fill the parent) for embedded mode.
+ /// Wraps a body in a Scaffold.
Widget _wrap(Widget body, Color bg, {PreferredSizeWidget? appBar}) {
- if (widget.embedded) {
- return ColoredBox(color: bg, child: body);
- }
// Don't resize for the keyboard — epub.js reflows when the WebView
// resizes, which would shift the visible page when the search keyboard
// pops up. The bottom sheet positions itself above the keyboard via
@@ -1045,45 +991,13 @@ class EbookReaderViewState extends State<EbookReaderView> {
);
}
- /// SafeArea is only needed in full-screen mode — embedded callers manage
- /// their own insets.
- Widget _maybeSafeArea({required Widget child, bool top = true, bool bottom = true}) {
- if (widget.embedded) return child;
- return SafeArea(top: top, bottom: bottom, child: child);
- }
-
- /// Lays out the viewer. With a locked viewerSize the WebView always renders
- /// at that exact logical size: full screen shows it ~1:1, the embedded card
- /// scales it down. BoxFit.scaleDown never scales up, so the full-screen
- /// instance stays unscaled and sharp.
- ///
- /// Wrapped in SafeArea (full-screen only) so the page never slides under the
- /// camera cutout. Aligned to the top so any letterbox gap from the card's
- /// different aspect ratio lands BELOW the text - the first line stays at the
- /// top of the card rather than getting pushed to the bottom.
+ /// Wrapped in SafeArea so the page never slides under the camera cutout.
Widget _buildViewerArea(Widget viewer) {
- final lock = widget.viewerSize;
- if (lock != null) {
- return _maybeSafeArea(
- child: SizedBox.expand(
- child: FittedBox(
- fit: BoxFit.scaleDown,
- alignment: Alignment.topCenter,
- clipBehavior: Clip.hardEdge,
- child: SizedBox(width: lock.width, height: lock.height, child: viewer),
- ),
- ),
- );
- }
- return _maybeSafeArea(child: SizedBox.expand(child: viewer));
+ return SafeArea(child: SizedBox.expand(child: viewer));
}
void _handleClose() {
- if (widget.onClose != null) {
- widget.onClose!();
- } else {
- Navigator.pop(context);
- }
+ Navigator.pop(context);
}
@override
@@ -1173,7 +1087,6 @@ class EbookReaderViewState extends State<EbookReaderView> {
_updateBookmarkState();
setState(() => _progress = value.progress);
_syncProgress(value.startCfi, value.progress);
- widget.onPositionChanged?.call(value.startCfi);
}
},
onTouchDown: (x, y) {
@@ -1210,7 +1123,7 @@ class EbookReaderViewState extends State<EbookReaderView> {
colors: [bg.withValues(alpha: 1.0), bg.withValues(alpha: 0.6)],
),
),
- child: _maybeSafeArea(
+ child: SafeArea(
bottom: false,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
@@ -1218,17 +1131,11 @@ class EbookReaderViewState extends State<EbookReaderView> {
children: [
IconButton(
icon: Icon(
- widget.embedded ? Icons.flip_to_front_rounded : Icons.arrow_back_rounded,
+ Icons.arrow_back_rounded,
color: cs.onSurface,
),
onPressed: _handleClose,
),
- if (widget.embedded && widget.onExpand != null)
- IconButton(
- icon: Icon(Icons.fullscreen_rounded, color: cs.onSurface),
- tooltip: 'Expand',
- onPressed: widget.onExpand,
- ),
Expanded(
child: Text(
widget.title,
@@ -1292,7 +1199,7 @@ class EbookReaderViewState extends State<EbookReaderView> {
colors: [bg.withValues(alpha: 1.0), bg.withValues(alpha: 0.6)],
),
),
- child: _maybeSafeArea(
+ child: SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
@@ -1339,7 +1246,7 @@ class EbookReaderViewState extends State<EbookReaderView> {
Positioned(
left: 0,
right: 0,
- bottom: (widget.embedded ? 0 : MediaQuery.of(context).padding.bottom) + 16,
+ bottom: MediaQuery.of(context).padding.bottom + 16,
child: Center(
child: Material(
elevation: 8,