Created
July 27, 2024 18:55
-
-
Save anoochit/6699289b473b0982337377087441c68d to your computer and use it in GitHub Desktop.
Load epub data from assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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