View pdf flutter

Demo:

Link code demo:https://github.com/thaiviethai99/view_pdf_flutter.git

Tạo project flutter

flutter create viewpdf

Thêm permission ra internet edit file zoom_photo\android\app\src\main\AndroidManifest.xml
Thêm

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.viewpdf">
<uses-permission android:name="android.permission.INTERNET"/>

Cài đặt thư viện flutter_cached_pdfview link tham khảo https://pub.dev/packages/flutter_cached_pdfview

flutter pub add flutter_cached_pdfview

Edit file viewpdf\android\build.gradle thêm jCenter() vào repositories và allprojects

 repositories {
        google()
        mavenCentral()
        jcenter() // here
    }
allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // and here
    }
}

Import package:

import 'package:flutter_cached_pdfview/flutter_cached_pdfview.dart';

Edit file main.dart như sau

Giao diện như sau:

Thêm vào file main.dart

class PDFViewerFromUrl extends StatelessWidget {
  const PDFViewerFromUrl({Key? key, required this.title, required this.url})
      : super(key: key);

  final String url;
  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: const PDF().fromUrl(
        url,
        placeholder: (double progress) => Center(child: Text('$progress %')),
        errorWidget: (dynamic error) => Center(child: Text(error.toString())),
      ),
    );
  }
}

Cập nhật onPressed

TextButton(
                    onPressed: () => {
                      Navigator.push(
                        context,
                        MaterialPageRoute<dynamic>(
                          builder: (_) => PDFViewerFromUrl(
                            title: 'PDF'.toString(),
                            url: 'http://africau.edu/images/default/sample.pdf',
                          ),
                        ),
                      )
                    },
                    child: const Text('Xem pdf'),
                  )

File main.dart hoàn chỉnh: