Sunday, November 8, 2026
HomeiOS Developmentandroid - Learn how to save Epub reader progress in Flutter?

android – Learn how to save Epub reader progress in Flutter?

[ad_1]

In my Flutter mission, I used to be implementing epub reader utilizing following library –

https://pub.dev/packages/epub_viewer

I’ve performed all the things in response to their library documentation. Downloading epub and opening the file works completely. However the issue is : I wish to save the final learn progress utilizing cfi talked about within the documentation nevertheless it’s not working.

Here is your complete code:

import 'dart:convert';
import 'dart:io';

import 'package deal:dio/dio.dart';
import 'package deal:epub_viewer/epub_viewer.dart';
import 'package deal:flutter/materials.dart';
import 'package deal:path_provider/path_provider.dart';
import 'package deal:permission_handler/permission_handler.dart';

void primary() async {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool loading = false;
  Dio dio = new Dio();

  closing imgUrl = "http://bbebooksthailand.com/phpscripts/bbdownload.php?ebookdownload=CrimePunishment-EPUB2";
  bool downloading = false;
  var progressString = "";


  @override
  void initState() {
    tremendous.initState();
    obtain();
  }

  obtain() async {
    if (Platform.isAndroid) {
      print('obtain');
      await downloadFile();
    } else {
      loading = false;
    }
  }

  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      dwelling: Scaffold(
        appBar: AppBar(
          title: const Textual content('Plugin instance app'),
        ),
        physique: Heart(
          little one: downloading
              ? Container(
            top: 120.0,
            width: 200.0,
            little one: Card(
              coloration: Colours.black,
              little one: Column(
                mainAxisAlignment: MainAxisAlignment.middle,
                youngsters: <Widget>[
                  CircularProgressIndicator(),
                  SizedBox(
                    height: 20.0,
                  ),
                  Text(
                    "Downloading File: $progressString",
                    style: TextStyle(
                      color: Colors.white,
                    ),
                  )
                ],
              ),
            ),
          )
              : FlatButton(
            onPressed: () async {
              Listing appDocDir =
              await getApplicationDocumentsDirectory();
              print('$appDocDir');

              EpubViewer.setConfig(
                  themeColor: Theme.of(context).primaryColor,
                  identifier: "androidBook",
                  scrollDirection: EpubScrollDirection.VERTICAL,
                  allowSharing: true,
                  enableTts: true,
                  nightMode: true);

              String androidBookPath="knowledge/person/0/com.instance.epub_flutter/app_flutter/e-book.epub";

                   EpubViewer.open(

                     Platform.isAndroid ? androidBookPath : androidBookPath,
                     lastLocation: EpubLocator.fromJson({
                       "bookId": "2239",
                       "href": "/OEBPS/ch06.xhtml",
                       "created": 1539934158390,
                       "areas": {
                         "cfi": "epubcfi(/0!/4/4[simple_book]/2/2/6)"
                       }
                     }),
                   );

              EpubViewer.locatorStream.pay attention((locator) {
                print(
                    'LOCATOR: ${locator.toString()}');
              });

            },
            little one: Container(
              little one: Textual content('open epub'),
            ),
          ),
        ),
      ),
    );
  }

  Future<void> downloadFile() async {
    Dio dio = Dio();

    attempt {
      var dir = await getApplicationDocumentsDirectory();
      print("path ${dir.path}");
      await dio.obtain(imgUrl, "${dir.path}/e-book.epub",
          onReceiveProgress: (rec, complete) {
            print("Rec: $rec , Whole: $complete");

            setState(() {
              downloading = true;
              progressString = ((rec / complete) * 100).toStringAsFixed(0) + "%";
            });
          });
    } catch (e) {
      print(e);
    }

    setState(() {
      downloading = false;
      progressString = "Accomplished";
    });
    print("Obtain accomplished");
  }

  startDownload() async {
    Listing appDocDir = Platform.isAndroid
        ? await getExternalStorageDirectory()
        : await getApplicationDocumentsDirectory();

    String path = appDocDir.path + '/chair.epub';
    File file = File(path);

    if (!File(path).existsSync()) {
      await file.create();
      await dio.obtain(
        'https://github.com/FolioReader/FolioReaderKit/uncooked/grasp/Instance/'
            'Shared/Samplepercent20eBooks/Thepercent20Silverpercent20Chair.epub',
        path,
        deleteOnError: true,
        onReceiveProgress: (receivedBytes, totalBytes) {
          print((receivedBytes / totalBytes * 100).toStringAsFixed(0));
          //Test if obtain is full and shut the alert dialog
          if (receivedBytes == totalBytes) {
            loading = false;
            setState(() {});
          }
        },
      );
    } else {
      loading = false;
      setState(() {});
    }
  }
}

The code snippet for locator can also be not printing:

 EpubViewer.locatorStream.pay attention((locator) {
                print(
                    'LOCATOR: ${locator.toString()}');
              });

So, I would like an answer to avoid wasting my final learn progress and open in that individual web page the place I left studying that epub.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments