Skip to content

Instantly share code, notes, and snippets.

@anoochit
Created July 27, 2024 18:55
Show Gist options
  • Save anoochit/6699289b473b0982337377087441c68d to your computer and use it in GitHub Desktop.
Save anoochit/6699289b473b0982337377087441c68d to your computer and use it in GitHub Desktop.
Load epub data from assets
Future<EpubController> loadEpub() async {
var asset = await PlatformAssetBundle().load("assets/alice.epub");
return EpubController(
document: EpubDocument.openData(asset.buffer
.asUint8List(asset.offsetInBytes, asset.lengthInBytes)));
}
@override
Widget build(BuildContext context) => FutureBuilder<EpubController>(
future: loadEpub(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Waiting......");
} else if (snapshot.hasError) {
return Text("Error : ${snapshot.error}");
} else {
var epubController = snapshot.data!;
return Scaffold(
appBar: AppBar(
// Show actual chapter name
title: EpubViewActualChapter(
controller: epubController,
builder: (chapterValue) => Text(
'Chapter: ${chapterValue?.chapter?.Title?.replaceAll('\n', '').trim() ?? ''}',
textAlign: TextAlign.start,
)),
),
// Show table of contents
drawer: Drawer(
child: EpubViewTableOfContents(
controller: epubController,
),
),
// Show epub document
body: EpubView(
controller: epubController,
),
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment