2

I have a problem and I cannot solve it and I did not find a source to solve it on Google, I have a page where I view a PDF file through a link, and I have a CircularProgressIndicator and I want to replace it with a progress bar showing the percentage of downloading the file, can I do that? I have attached my code

import 'package:flutter/material.dart';
import 'package:flutter_plugin_pdf_viewer/flutter_plugin_pdf_viewer.dart';

class ReadPdf extends StatefulWidget {

  final String value;
  ReadPdf({Key key, this.value}) : super(key: key);

  @override
  _ReadPdfState createState() => _ReadPdfState();
}

class _ReadPdfState extends  State<ReadPdf>{
  bool _isloading = false, _isInit = true;
  PDFDocument document;

  @override
  void initState() {

    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
        body: Container(
          child: Column(
            children: <Widget>[
              Expanded(child:Center(
                child: _isInit?  MaterialButton(child: Text('Go'), onPressed: () {_loadFromURL(widget.value);},
                color: Color.fromRGBO(64, 75, 96, .9),
                textColor: Colors.white,
                ) : _isloading? Center(child: CircularProgressIndicator(),) : PDFViewer(document: document,indicatorBackground: Colors.deepPurple,),
              ),),
              Row(
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[

                ],
              ),
            ],
          ),
        ),
      ),
    );
  }


  _loadFromURL(String url) async{
    setState(() {
      _isInit = false;
      _isloading = true;
    });
    document = await PDFDocument.fromURL('${url}');    setState(() {
      _isloading = false;
    });

  }


}
Abdelrahman Saed
  • 382
  • 4
  • 13
Abdullrahman Wasfi
  • 401
  • 1
  • 7
  • 13

3 Answers3

2

I have an app with the same feature, I used Dio this package supports downloading a file to your phone.

All you need to do is

Dio dio = Dio();
  dio.download("*YOUR URL WHERE YOU WANT TO DOWNLOAD A FILE*",
      "*YOUR DESTINATION PATH*", onReceiveProgress: (rec, total) {
          print("Downloading " + ((rec / total) * 100).toStringAsFixed(0) + "%");
      });
Qonvex620
  • 3,819
  • 1
  • 8
  • 15
1

Never used this for pdf, but I've tried it for NetworkImage(). Not sure if it'll help. But you can just try it if there's a way to use loadingBuilder in your code.

Image.network(
  imageUrl,
  fit: BoxFit.cover,
  loadingBuilder: (BuildContext context, Widget child,
      ImageChunkEvent loadingProgress) {
    if (loadingProgress == null)
      return child;
    else {
      return Center(
        child: CircularProgressIndicator(
          value: loadingProgress.expectedTotalBytes != null
              ? loadingProgress.cumulativeBytesLoaded /
                  loadingProgress.expectedTotalBytes
              : null,
        ),
      );
    }
  },
);
Arsh Shaikh
  • 1,106
  • 5
  • 21
1

u can use flutter_cached_pdfview


and this an example to view a pdf from URL and cache it with placeholder
u can replace placeholder with any widget like CircularProgressIndicator
         PDF().cachedFromUrl(
          'http://africau.edu/images/default/sample.pdf',
            placeholder: (progress) => Center(child: Text('$progress %'))
             )

take a look https://pub.dev/packages/flutter_cached_pdfview

Abdelrahman Saed
  • 382
  • 4
  • 13