5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutterでブックリーダーを作る

Posted at

やりたいこと

Flutterで作ったAndroidアプリ上で電子書籍を読みたい。

やったこと

Flutterのepubパッケージを使って、インターネット上のepubファイルを画面表示させた。
横スクロールでページングさせたかったが、後述する別のパッケージを利用した方が良さそう。

対応

pubspec.yaml
  dependencies:
    epub:
    flutter_html:
main.dart
  import 'dart:async';
  import 'package:epub/epub.dart' as epub;
  import 'package:http/http.dart' as http;
  import 'package:flutter/material.dart';
  import 'package:flutter_html/flutter_html.dart';
  
  class Reader extends StatefulWidget {
    @override
    _ReaderState createState() => _ReaderState();
  }
  
  class _ReaderState extends State<Reader> {
    String content = 'no epub file';
  
    @override
    void initState() {
      _showReaderInfo();
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('ブックリーダー'),
        ),
        body: Center(
          child: SingleChildScrollView(
            child: Html(
              data: content,
            ),
          ),
        ),
      );
    }
  
    _showReaderInfo() async {
      epub.EpubBook book = await fetchBook('epubのURL');
      epub.EpubContent bookContent = book.Content;
      Map<String, epub.EpubTextContentFile> htmlFiles = bookContent.Html;
      setState(() {
        // インデックスを変えることで、epubの次のチャプターになる
        content = htmlFiles.values.toList()[0].Content;
      });
    }
  
    Future<epub.EpubBook> fetchBook(String url) async {
      final response = await http.get(url);
  
      if (response.statusCode == 200) {
        return epub.EpubReader.readBook(response.bodyBytes);
      } else {
        throw Exception('Failed to load epub');
      }
    }
  }

参考

今後参考にしたいもの

横スクロールでページング
https://github.com/creatint/light-old

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?