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

[native_pdf_view] Provide a onPageChanged callback #19

Closed
IchordeDionysos opened this issue Dec 13, 2019 · 3 comments
Closed

[native_pdf_view] Provide a onPageChanged callback #19

IchordeDionysos opened this issue Dec 13, 2019 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@IchordeDionysos
Copy link
Contributor

Is your feature request related to a problem? Please describe.
We would like to show the user that he is on page 1/n,

Describe the solution you'd like
To achieve that I believe that a simple onPageChanged callback could be easily implemented and would give us a lot of flexibility.

Describe alternatives you've considered
A built-in page indicator would also work, but might not fit into the UI of every app.

Additional context

@IchordeDionysos IchordeDionysos added the enhancement New feature or request label Dec 13, 2019
@SergeShkurko
Copy link
Member

SergeShkurko commented Dec 13, 2019

Show code example
import 'package:flutter/material.dart';
import 'package:native_pdf_view/native_pdf_view.dart';
import 'package:native_pdf_view_example/has_support.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // Create page controller
  final _pageController = PageController();
  int _actualPageNumber = 1;

  @override
  void initState() {
    // listen page number change
    _pageController.addListener(() {
      final pageIndex = _pageController.page.round();
      final pageNumber = pageIndex + 1;
      // Set actual page
      if (pageNumber != _actualPageNumber) {
        setState(() {
          _actualPageNumber = pageNumber;
        });
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
        theme: ThemeData(primaryColor: Colors.white),
        home: Scaffold(
          appBar: AppBar(title: Text('PDFView example')),
          body: FutureBuilder<PDFDocument>(
            future: _getDocument(),
            builder: (_, snapshot) {
              if (snapshot.hasData) {
                return Stack(
                  children: <Widget>[
                    PDFView(
                      controller: _pageController, // set page controller
                      document: snapshot.data,
                    ),
                    Align(
                      alignment: Alignment.center,
                      child: Text(
                        '$_actualPageNumber/${snapshot.data.pagesCount}',
                        style: TextStyle(fontSize: 34),
                      ),
                    )
                  ],
                );
              }

              if (snapshot.hasError) {
                return Center(
                  child: Text(
                    'PDF Rendering does not '
                    'support on the system of this version',
                  ),
                );
              }

              return Center(child: CircularProgressIndicator());
            },
          ),
        ),
      );

  Future<PDFDocument> _getDocument() async {
    if (await hasSupport()) {
      return PDFDocument.openAsset('assets/sample.pdf');
    } else {
      throw Exception(
        'PDF Rendering does not '
        'support on the system of this version',
      );
    }
  }
}

Снимок экрана 2019-12-13 в 16 54 14 Снимок экрана 2019-12-13 в 16 54 36

@IchordeDionysos will this solve your problem?

@SergeShkurko
Copy link
Member

Got your idea, I agree
Added in 2.1.0

@IchordeDionysos
Copy link
Contributor Author

Yes, the example you provided worked, but is so think far less beautiful and less intuitive/obvious to us :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants