scrumlab_pdf_flutter 1.2.1-nullsafety.0 copy "scrumlab_pdf_flutter: ^1.2.1-nullsafety.0" to clipboard
scrumlab_pdf_flutter: ^1.2.1-nullsafety.0 copied to clipboard

Displaying PDF from Network, File and assets easily like we display Image in Flutter Widget.

example/lib/main.dart

import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:scrumlab_pdf_flutter/scrumlab_pdf_flutter.dart';

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

class PdfApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('scrumlab_pdf_flutter demo'),
        ),
        body: PDFListBody(),
      ),
    );
  }
}

class PDFListBody extends StatefulWidget {
  @override
  _PDFListBodyState createState() => _PDFListBodyState();
}

class _PDFListBodyState extends State<PDFListBody> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          ElevatedButton(
            onPressed: () {
              _navigateToPage(
                title: 'Pdf from asset',
                child: PDF.asset(
                  'assets/pdf/demo.pdf',
                  placeHolder: Image.asset(
                    'assets/images/pdf.png',
                    height: 200,
                    width: 100,
                  ),
                ),
              );
            },
            child: const Text('Pdf from asset'),
          ),
          ElevatedButton(
            onPressed: () {
              _navigateToPage(
                title: 'Pdf from networkUrl',
                child: PDF.network(
                  'https://google-developer-training.github.io/android-developer-fundamentals-course-concepts/en/android-developer-fundamentals-course-concepts-en.pdf',
                ),
              );
            },
            child: const Text('Pdf from network'),
          ),
          Builder(
            builder: (context) {
              return ElevatedButton(
                onPressed: () async {
                  final file = await FilePicker.platform.pickFiles(allowedExtensions: ['pdf'], type: FileType.custom);
                  if (file?.files[0].path != null) {
                    _navigateToPage(
                      title: 'PDF from file',
                      child: PDF.file(
                        File(file!.files[0].path!),
                      ),
                    );
                  } else {
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(
                        content: Text('Failed to load Picked file'),
                      ),
                    );
                  }
                },
                child: const Text('PDF from file'),
              );
            },
          )
        ],
      ),
    );
  }

  void _navigateToPage({required String title, required Widget child}) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => Scaffold(
          appBar: AppBar(title: Text(title)),
          body: Center(child: child),
        ),
      ),
    );
  }
}
1
likes
110
pub points
0%
popularity

Publisher

unverified uploader

Displaying PDF from Network, File and assets easily like we display Image in Flutter Widget.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (LICENSE)

Dependencies

flutter, flutter_cache_manager, http, path_provider

More

Packages that depend on scrumlab_pdf_flutter