binSaed / flutter_cached_pdfview

Enhanced PDF view for Flutter.
https://pub.dev/packages/flutter_cached_pdfview
MIT License
121 stars 70 forks source link

Getting page count return 0 #34

Closed pishguy closed 3 years ago

pishguy commented 3 years ago

i this code that i used in our application i get zero for getting page count

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_cached_pdfview/flutter_cached_pdfview.dart';
import 'package:velocity_x/velocity_x.dart';

class ShowPdfFromUrl extends StatefulWidget {
  final String pdfUrl;
  final String productName;

  ShowPdfFromUrl({@required this.pdfUrl, @required this.productName});

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

class _ShowPdfFromUrlState extends State<ShowPdfFromUrl> {
  final Completer<PDFViewController> _pdfViewController = Completer<PDFViewController>();
  final ValueNotifier<int> _pageCount = ValueNotifier<int>(0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '${widget.productName}',
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
        ),
      ),
      body: Stack(
        children: [
          PDF(
            onViewCreated: (PDFViewController pdfViewController) async {
              _pdfViewController.complete(pdfViewController);
              _pageCount.value = await pdfViewController.getPageCount();
            },
          ).cachedFromUrl(
            '${widget.pdfUrl}',
            placeholder: (progress) => Center(child: Text('Yükleniyor $progress % ...')),
            errorWidget: (error) {
              return Center(
                  child: Container(
                    width: context.screenWidth,
                    height: context.screenHeight,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text('PDF hatası indiriliyor'),
                        Padding(
                          padding: const EdgeInsets.only(top: 16.0),
                          child: OutlineButton(
                            onPressed: () {
                              setState(() {});
                            },
                            child: Text('lütfen tekrar deneyin'),
                          ),
                        ),
                      ],
                    ),
                  ));
            },
          ),

          Positioned(
            top: 8.0,
            left: 8.0,
            child: ValueListenableBuilder(
              valueListenable: _pageCount,
              builder: (_,value,__){
                return Container(
                  padding: EdgeInsets.all(8.0),
                  color: Colors.black,
                  child: Text('($value)',style: TextStyle(
                    color: Colors.white,
                  ),),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
binSaed commented 3 years ago

hi @MahdiPishguy try this

   PDF(
           onPageChanged: (page, total) {
            _pageCount.value = total;
             },
            onViewCreated: (PDFViewController pdfViewController) async {
              _pdfViewController.complete(pdfViewController);
              _pageCount.value = await pdfViewController.getPageCount();
            },
          )

Screenshot_1607165341

binSaed commented 3 years ago

explain the prob pdfViewController was created but pdf file still downloading or get from cache

binSaed commented 3 years ago

if this solution worked with you please let me know and close issue

pishguy commented 3 years ago

if this solution worked with you please let me know and close issue

Yes, thanks a lot, 🥇