Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting page count return 0 #34

Closed
pishguy opened this issue Dec 4, 2020 · 4 comments
Closed

Getting page count return 0 #34

pishguy opened this issue Dec 4, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@pishguy
Copy link

pishguy commented Dec 4, 2020

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,
                  ),),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
@pishguy pishguy added the bug Something isn't working label Dec 4, 2020
@binSaed
Copy link
Owner

binSaed commented Dec 5, 2020

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
Copy link
Owner

binSaed commented Dec 5, 2020

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

@binSaed
Copy link
Owner

binSaed commented Dec 5, 2020

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

@pishguy
Copy link
Author

pishguy commented Dec 5, 2020

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

Yes, thanks a lot, 🥇

@pishguy pishguy closed this as completed Dec 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants