Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Single Page pdf is not viewing #11

Closed
manojeeva opened this issue Jan 25, 2021 · 12 comments
Closed

Single Page pdf is not viewing #11

manojeeva opened this issue Jan 25, 2021 · 12 comments

Comments

@manojeeva
Copy link

From Local and also from URL single page pdf is not view.
The view is empty.
Test Case.
Simply put single page pdf URL in the widget.
Sample URL
https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf

@manojeeva manojeeva changed the title One Page pdf is now viewing Single Page pdf is now viewing Jan 25, 2021
@manojeeva manojeeva changed the title Single Page pdf is now viewing Single Page pdf is not viewing Jan 25, 2021
@lohanidamodar
Copy link
Owner

Hi, @manojeeva thanks for reporting. I will test and let you know.

@JoeCiou
Copy link

JoeCiou commented Jan 26, 2021

It may be caused because of this.

@manojeeva
Copy link
Author

Is it possible to merge it?
Any idea when it will be available here?
Right now I'm updating count to 2 if it is 1.
I can see two same page.

@jags-aaron
Copy link

I changed some validations, and the issue seems to be fixed this

@irvincastellanos
Copy link

I changed some validations, and the issue seems to be fixed this

Awesome man, I have the same problem and this change solves it! 🙌🏽

@ararage
Copy link

ararage commented Jan 26, 2021

I changed some validations, and the issue seems to be fixed this

This solves the problem :D

@dinhthienkhai
Copy link

I changed some validations, and the issue seems to be fixed this

Hi All,
I got same issue, just view pdf file with more 2 pages. I want to ask that what is "changed some validations"?

@jags-aaron
Copy link

jags-aaron commented Jan 28, 2021

@dinhthienkhai

If you are using MacOS

just open a terminal and copy this:

1.- cd /Users/$USER/.pub-cache/hosted/pub.dartlang.org/advance_pdf_viewer-1.2.1+2/lib/src/viewer.dart
2.- open ./
3.- open the file viewer.dart

NOTES:
Change (advance_pdf_viewer-1.2.1+2) with your version

dependencies:
  advance_pdf_viewer: ^1.2.1+2

Then replace the file viewer.dart content with this:
https://github.com/lohanidamodar/pdf_viewer/blob/49b58134fc22e0464e7f7f66b82439527988c26c/lib/src/viewer.dart

Because you are modifying the local library file this could be overwritten.
you will need to do the same every time you update your pubspect.yaml until the fix is in a new version of the library

hope it helps :)

@devmgs
Copy link

devmgs commented Jan 29, 2021

I created a custom class

import 'package:flutter/material.dart';
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
import 'package:numberpicker/numberpicker.dart';

/// enum to describe indicator position
enum IndicatorPosition { topLeft, topRight, bottomLeft, bottomRight }

/// PDFViewer, a inbuild pdf viewer, you can create your own too.
/// [document] an instance of `PDFDocument`, document to be loaded
/// [indicatorText] color of indicator text
/// [indicatorBackground] color of indicator background
/// [pickerButtonColor] the picker button background color
/// [pickerIconColor] the picker button icon color
/// [indicatorPosition] position of the indicator position defined by `IndicatorPosition` enum
/// [showIndicator] show,hide indicator
/// [showPicker] show hide picker
/// [showNavigation] show hide navigation bar
/// [toolTip] tooltip, instance of `PDFViewerTooltip`
/// [enableSwipeNavigation] enable,disable swipe navigation
/// [scrollDirection] scroll direction horizontal or vertical
/// [lazyLoad] lazy load pages or load all at once
/// [controller] page controller to control page viewer
/// [zoomSteps] zoom steps for pdf page
/// [minScale] minimum zoom scale for pdf page
/// [maxScale] maximum zoom scale for pdf page
/// [panLimit] pan limit for pdf page
/// [onPageChanged] function called when page changes
///
class MyPDFViewer extends StatefulWidget {
  final PDFDocument document;
  final Color indicatorText;
  final Color indicatorBackground;
  final Color pickerButtonColor;
  final Color pickerIconColor;
  final IndicatorPosition indicatorPosition;
  final bool showIndicator;
  final bool showPicker;
  final bool showNavigation;
  final PDFViewerTooltip tooltip;
  final bool enableSwipeNavigation;
  final Axis scrollDirection;
  final bool lazyLoad;
  final PageController controller;
  final int zoomSteps;
  final double minScale;
  final double maxScale;
  final double panLimit;
  final ValueChanged<int> onPageChanged;

  final Widget Function(
      BuildContext,
      int pageNumber,
      int totalPages,
      void Function({int page}) jumpToPage,
      void Function({int page}) animateToPage,
      ) navigationBuilder;
  final Widget progressIndicator;

  MyPDFViewer({
    Key key,
    @required this.document,
    this.scrollDirection,
    this.lazyLoad = true,
    this.indicatorText = Colors.white,
    this.indicatorBackground = Colors.black54,
    this.showIndicator = true,
    this.showPicker = true,
    this.showNavigation = true,
    this.enableSwipeNavigation = true,
    this.tooltip = const PDFViewerTooltip(),
    this.navigationBuilder,
    this.controller,
    this.indicatorPosition = IndicatorPosition.topRight,
    this.zoomSteps,
    this.minScale,
    this.maxScale,
    this.panLimit,
    this.progressIndicator,
    this.pickerButtonColor,
    this.pickerIconColor,
    this.onPageChanged,
  }) : super(key: key);

  _MyPDFViewerState createState() => _MyPDFViewerState();
}

class _MyPDFViewerState extends State<MyPDFViewer> {
  bool _isLoading = true;
  int _pageNumber;
  bool _swipeEnabled = true;
  List<PDFPage> _pages;
  PageController _pageController;
  final Duration animationDuration = Duration(milliseconds: 200);
  final Curve animationCurve = Curves.easeIn;

  @override
  void initState() {
    super.initState();
    //if(widget.document.count==1)
   // widget.document.count=widget.document.count+1;
    _pages = List(widget.document.count);
    _pageController = widget.controller ?? PageController();
    _pageNumber = _pageController.initialPage + 1;
    if (!widget.lazyLoad)
      widget.document.preloadPages(
        onZoomChanged: onZoomChanged,
        zoomSteps: widget.zoomSteps,
        minScale: widget.minScale,
        maxScale: widget.maxScale,
        panLimit: widget.panLimit,
      );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _pageNumber = _pageController.initialPage + 1;
    _isLoading = true;
    _pages = List(widget.document.count);
    // _loadAllPages();
    _loadPage();
  }

  @override
  void didUpdateWidget(MyPDFViewer oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  onZoomChanged(double scale) {
    if (scale != 1.0) {
      setState(() {
        _swipeEnabled = false;
      });
    } else {
      setState(() {
        _swipeEnabled = true;
      });
    }
  }

  _loadPage() async {
    if (_pages[_pageNumber - 1] != null) return;
    setState(() {
      _isLoading = true;
    });
    final data = await widget.document.get(
      page: _pageNumber,
      onZoomChanged: onZoomChanged,
      zoomSteps: widget.zoomSteps,
      minScale: widget.minScale,
      maxScale: widget.maxScale,
      panLimit: widget.panLimit,
    );
    _pages[_pageNumber - 1] = data;
    if (mounted) {
      setState(() {
        _isLoading = false;
      });
    }
  }

  _animateToPage({int page}) {
    _pageController.animateToPage(page != null ? page : _pageNumber - 1,
        duration: animationDuration, curve: animationCurve);
  }

  _jumpToPage({int page}) {
    _pageController.jumpToPage(page != null ? page : _pageNumber - 1);
  }

  Widget _drawIndicator() {
    print("widget.document.count : "+widget.document.count.toString());
    Widget child = GestureDetector(
        onTap:
        widget.showPicker && widget.document.count >= 1 ? _pickPage : null,
        child: Container(
            padding:
            EdgeInsets.only(top: 4.0, left: 16.0, bottom: 4.0, right: 16.0),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(4.0),
                color: widget.indicatorBackground),
            child: Text("$_pageNumber/${widget.document.count}",
                style: TextStyle(
                    color: widget.indicatorText,
                    fontSize: 16.0,
                    fontWeight: FontWeight.w400))));

    switch (widget.indicatorPosition) {
      case IndicatorPosition.topLeft:
        return Positioned(top: 20, left: 20, child: child);
      case IndicatorPosition.topRight:
        return Positioned(top: 20, right: 20, child: child);
      case IndicatorPosition.bottomLeft:
        return Positioned(bottom: 20, left: 20, child: child);
      case IndicatorPosition.bottomRight:
        return Positioned(bottom: 20, right: 20, child: child);
      default:
        return Positioned(top: 20, right: 20, child: child);
    }
  }

  _pickPage() {
    showDialog<int>(
        context: context,
        builder: (BuildContext context) {
          return NumberPickerDialog.integer(
            title: Text(widget.tooltip.pick),
            minValue: 1,
            cancelWidget: Container(),
            maxValue: widget.document.count,
            initialIntegerValue: _pageNumber,
          );
        }).then((int value) {
      if (value != null) {
        _pageNumber = value;
        _jumpToPage();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          PageView.builder(
            physics:
            _swipeEnabled && widget.enableSwipeNavigation && !_isLoading
                ? null
                : NeverScrollableScrollPhysics(),
            onPageChanged: (page) {
              setState(() {
                _pageNumber = page + 1;
              });
              _loadPage();
              widget.onPageChanged?.call(page);
            },
            scrollDirection: widget.scrollDirection ?? Axis.horizontal,
            controller: _pageController,
            itemCount: _pages?.length ?? 0,
            itemBuilder: (context, index) => _pages[index] == null
                ? Center(
              child:
              widget.progressIndicator ?? CircularProgressIndicator(),
            )
                : _pages[index],
          ),
          (widget.showIndicator && !_isLoading)
              ? _drawIndicator()
              : Container(),
        ],
      ),
      floatingActionButton: widget.showPicker && widget.document.count >= 1
          ? FloatingActionButton(
        elevation: 4.0,
        tooltip: widget.tooltip.jump,
        child: Icon(
          Icons.view_carousel,
          color: widget.pickerIconColor ?? Colors.white,
        ),
        backgroundColor: widget.pickerButtonColor ?? Colors.blue,
        onPressed: () {
          _pickPage();
        },
      )
          : null,
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: (widget.showNavigation && widget.document.count > 0)
          ? widget.navigationBuilder != null
          ? widget.navigationBuilder(
        context,
        _pageNumber,
        widget.document.count,
        _jumpToPage,
        _animateToPage,
      )
          : BottomAppBar(
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: IconButton(
                icon: Icon(Icons.first_page),
                tooltip: widget.tooltip.first,
                onPressed: _pageNumber == 1 || _isLoading
                    ? null
                    : () {
                  _pageNumber = 1;
                  _jumpToPage();
                },
              ),
            ),
            Expanded(
              child: IconButton(
                icon: Icon(Icons.chevron_left),
                tooltip: widget.tooltip.previous,
                onPressed: _pageNumber == 1 || _isLoading
                    ? null
                    : () {
                  _pageNumber--;
                  if (1 > _pageNumber) {
                    _pageNumber = 1;
                  }
                  _animateToPage();
                },
              ),
            ),
            widget.showPicker
                ? Expanded(child: Text(''))
                : SizedBox(width: 1),
            Expanded(
              child: IconButton(
                icon: Icon(Icons.chevron_right),
                tooltip: widget.tooltip.next,
                onPressed:
                _pageNumber == widget.document.count || _isLoading
                    ? null
                    : () {
                  _pageNumber++;
                  if (widget.document.count < _pageNumber) {
                    _pageNumber = widget.document.count;
                  }
                  _animateToPage();
                },
              ),
            ),
            Expanded(
              child: IconButton(
                icon: Icon(Icons.last_page),
                tooltip: widget.tooltip.last,
                onPressed:
                _pageNumber == widget.document.count || _isLoading
                    ? null
                    : () {
                  _pageNumber = widget.document.count;
                  _jumpToPage();
                },
              ),
            ),
          ],
        ),
      )
          : Container(),
    );
  }
}

After this instead of using

MyPDFViewer(document: document,

          showIndicator: false,
          showPicker: false,

          )

using

MyPDFViewer(document: document,

          showIndicator: false,
          showPicker: false,

          )

This way not have to change anything until it is fixed even after pub get.

Also problem is corrected by
bottomNavigationBar: (widget.showNavigation && widget.document.count > 0)

@ruan65
Copy link

ruan65 commented Feb 1, 2021

@lohanidamodar
Merge this pr
#13
please

@lohanidamodar
Copy link
Owner

Thank you everyone, I'll verify the changes and publish the updates ASAP

@lohanidamodar
Copy link
Owner

Thank you everyone for your contribution, I have found the bug and published the fix in new version 1.2.2. If you again have the same issue feel free to reopen.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants