123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import 'package:file_picker/file_picker.dart';
- import 'package:flutter/material.dart';
- import 'package:kmpdfkit_demo/compdfkit.dart';
- import 'package:kmpdfkit_demo/widgets/page/pdf_page_widget.dart';
- import 'package:kmpdfkit_demo/widgets/page/setting_page_widget.dart';
- import 'package:kmpdfkit_demo/widgets/page_routes.dart';
- import 'package:permission_handler/permission_handler.dart';
- void main() {
- runApp(MaterialApp(
- home: const MyApp(),
- initialRoute: PageRoutes.home,
- routes: {
- PageRoutes.home: (context) => const MyApp(),
- PageRoutes.settings: (context) => const SettingPageWidget()
- },
- ));
- }
- class MyApp extends StatefulWidget {
- const MyApp({Key? key}) : super(key: key);
- @override
- State<MyApp> createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- String _comPDFKitVersion = '';
- @override
- void initState() {
- super.initState();
- initPlatformState();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(comPDFKitVersion()),
- ),
- body: Padding(
- padding: const EdgeInsets.all(8),
- child: Column(
- children: [
- Builder(
- builder: (BuildContext context) => SizedBox(
- width: double.infinity,
- child: ElevatedButton(
- onPressed: () {
- showDocument(context);
- },
- child: const Text('Simple PDF Viewer'),
- ))),
- ],
- ),
- ));
- }
- void initPlatformState() async {
- String? comPDFKitVersion = await ComPDFKit.getComPDFKitVersionCode();
- setState(() {
- _comPDFKitVersion = comPDFKitVersion ?? '';
- });
- }
- String comPDFKitVersion() {
- return 'ComPDFKit $_comPDFKitVersion';
- }
- ///request storage permission
- Future<bool> getPermission() async {
- var status = await Permission.storage.request();
- return status.isGranted;
- }
- Future<String?> getPDFFile() async {
- bool isGranted = await getPermission();
- if (!isGranted) {
- return null;
- }
- FilePickerResult? result = await FilePicker.platform.pickFiles(
- type: FileType.custom,
- allowedExtensions: ['pdf'],
- allowMultiple: false);
- if (result != null) {
- List<String?> files = result.paths;
- return files[0];
- } else {
- return null;
- }
- }
- void showDocument(BuildContext context) async {
- var documentPath = await getPDFFile();
- if (documentPath != null) {
- await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
- builder: (_) => PDFPageWidget(
- documentPath: documentPath,
- )));
- }
- }
- }
|