epub_view 2.2.0 epub_view: ^2.2.0 copied to clipboard
Flutter widget for view EPUB documents on on Web, MacOs, Android and iOS. Based on dart-epub package.
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart'
show SystemChrome, SystemUiOverlayStyle, rootBundle;
import 'package:epub_view/epub_view.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangePlatformBrightness() {
_setSystemUIOverlayStyle();
}
Brightness get platformBrightness =>
MediaQueryData.fromWindow(WidgetsBinding.instance.window)
.platformBrightness;
void _setSystemUIOverlayStyle() {
if (platformBrightness == Brightness.light) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarBrightness: Brightness.light,
statusBarIconBrightness: Brightness.dark,
systemNavigationBarColor: Colors.grey[50],
systemNavigationBarIconBrightness: Brightness.dark,
));
} else {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.light,
systemNavigationBarColor: Colors.grey[850],
systemNavigationBarIconBrightness: Brightness.light,
));
}
}
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Epub demo',
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
),
darkTheme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.dark,
),
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
EpubController _epubReaderController;
@override
void initState() {
final loadedBook =
_loadFromAssets('assets/New-Findings-on-Shirdi-Sai-Baba.epub');
_epubReaderController = EpubController(
document: EpubReader.readBook(loadedBook),
// document: EpubReader,
// epubCfi:
// 'epubcfi(/6/26[id4]!/4/2/2[id4]/22)', // book.epub Chapter 3 paragraph 10
// epubCfi:
// 'epubcfi(/6/6[chapter-2]!/4/2/1612)', // book_2.epub Chapter 16 paragraph 3
);
super.initState();
}
@override
void dispose() {
_epubReaderController.dispose();
super.dispose();
}
Future<Uint8List> _loadFromAssets(String assetName) async {
final bytes = await rootBundle.load(assetName);
return bytes.buffer.asUint8List();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: EpubActualChapter(
controller: _epubReaderController,
builder: (chapterValue) => Text(
(chapterValue?.chapter?.Title?.trim() ?? '').replaceAll('\n', ''),
textAlign: TextAlign.start,
),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.save_alt),
color: Colors.white,
onPressed: () => _showCurrentEpubCfi(context),
),
],
),
drawer: Drawer(
child: EpubReaderTableOfContents(controller: _epubReaderController),
),
body: EpubView(
controller: _epubReaderController,
onDocumentLoaded: (document) {
print('isLoaded: $document');
},
dividerBuilder: (_) => Divider(),
),
);
void _showCurrentEpubCfi(context) {
final cfi = _epubReaderController.generateEpubCfi();
if (cfi != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(cfi),
action: SnackBarAction(
label: 'GO',
onPressed: () {
_epubReaderController.gotoEpubCfi(cfi);
},
),
),
);
}
}
}