|
@@ -9,8 +9,16 @@ import 'dart:io';
|
|
|
|
|
|
import 'package:compdfkit_flutter/compdfkit.dart';
|
|
|
import 'package:compdfkit_flutter/cpdf_configuration.dart';
|
|
|
+import 'package:compdfkit_flutter_example/features.dart';
|
|
|
+import 'package:compdfkit_flutter_example/page/settings_page.dart';
|
|
|
+import 'package:compdfkit_flutter_example/theme/themes.dart';
|
|
|
+import 'package:compdfkit_flutter_example/utils/file_util.dart';
|
|
|
+import 'package:file_picker/file_picker.dart';
|
|
|
+import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
+import 'package:flutter_localizations/flutter_localizations.dart';
|
|
|
+import 'package:flutter_svg/svg.dart';
|
|
|
|
|
|
const String DOCUMENT_PATH = 'pdfs/PDF_Document.pdf';
|
|
|
|
|
@@ -18,14 +26,31 @@ void main() {
|
|
|
runApp(const MyApp());
|
|
|
}
|
|
|
|
|
|
-class MyApp extends StatefulWidget {
|
|
|
+class MyApp extends StatelessWidget {
|
|
|
const MyApp({super.key});
|
|
|
|
|
|
@override
|
|
|
- State<MyApp> createState() => _MyAppState();
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return MaterialApp(
|
|
|
+ title: 'ComPDFKit SDK for Flutter',
|
|
|
+ theme: lightTheme,
|
|
|
+ darkTheme: darkTheme,
|
|
|
+ themeMode: ThemeMode.system,
|
|
|
+ localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
+ supportedLocales: AppLocalizations.supportedLocales,
|
|
|
+ home: const HomePage(),
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-class _MyAppState extends State<MyApp> {
|
|
|
+class HomePage extends StatefulWidget {
|
|
|
+ const HomePage({super.key});
|
|
|
+
|
|
|
+ @override
|
|
|
+ State<HomePage> createState() => _HomePageState();
|
|
|
+}
|
|
|
+
|
|
|
+class _HomePageState extends State<HomePage> {
|
|
|
@override
|
|
|
void initState() {
|
|
|
super.initState();
|
|
@@ -44,60 +69,87 @@ class _MyAppState extends State<MyApp> {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ void showDocument() async {
|
|
|
+ File document = await extractAsset(context, DOCUMENT_PATH);
|
|
|
+ ComPDFKit.openDocument(document.path,
|
|
|
+ password: '', configuration: CPDFConfiguration());
|
|
|
+ }
|
|
|
+
|
|
|
+ void pickDocument() async {
|
|
|
+ FilePickerResult? result = await FilePicker.platform.pickFiles(
|
|
|
+ type: FileType.custom,
|
|
|
+ allowedExtensions: ['pdf'],
|
|
|
+ );
|
|
|
+ if (result != null) {
|
|
|
+ ComPDFKit.openDocument(result.files.first.path!,
|
|
|
+ password: '', configuration: CPDFConfiguration());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
- return MaterialApp(
|
|
|
- home: Scaffold(
|
|
|
- body: SafeArea(
|
|
|
- child: Center(
|
|
|
- child: ElevatedButton(
|
|
|
- onPressed: () async {
|
|
|
- showDocument(context);
|
|
|
- },
|
|
|
- child: const Text('Open Document')),
|
|
|
- ))),
|
|
|
- );
|
|
|
+ final exampleList = <Widget>[
|
|
|
+ Text(
|
|
|
+ AppLocalizations.of(context)!.features,
|
|
|
+ style: Theme.of(context).textTheme.bodyLarge,
|
|
|
+ ),
|
|
|
+ FeatureItem(
|
|
|
+ title: AppLocalizations.of(context)!.open_document_title,
|
|
|
+ description: AppLocalizations.of(context)!.open_document_desc,
|
|
|
+ onTap: () => showDocument()),
|
|
|
+ FeatureItem(
|
|
|
+ title: AppLocalizations.of(context)!.pick_document_title,
|
|
|
+ description: AppLocalizations.of(context)!.pick_document_desc,
|
|
|
+ onTap: () => pickDocument()),
|
|
|
+ ];
|
|
|
+
|
|
|
+ return Scaffold(
|
|
|
+ appBar: const CAppBar(), body: ExampleListView(widgets: exampleList));
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- void showDocument(BuildContext context) async {
|
|
|
- final bytes = await DefaultAssetBundle.of(context).load(DOCUMENT_PATH);
|
|
|
- final list = bytes.buffer.asUint8List();
|
|
|
- final tempDir = await ComPDFKit.getTemporaryDirectory();
|
|
|
- var pdfsDir = Directory('${tempDir.path}/pdfs');
|
|
|
- pdfsDir.createSync(recursive: true);
|
|
|
- final tempDocumentPath = '${tempDir.path}/$DOCUMENT_PATH';
|
|
|
- final file = File(tempDocumentPath);
|
|
|
- if (!file.existsSync()) {
|
|
|
- file.create(recursive: true);
|
|
|
- file.writeAsBytesSync(list);
|
|
|
- }
|
|
|
+class ExampleListView extends StatelessWidget {
|
|
|
+ final List<Widget> widgets;
|
|
|
+
|
|
|
+ const ExampleListView({super.key, required this.widgets});
|
|
|
+
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return Padding(
|
|
|
+ padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
|
+ child: ListView.builder(
|
|
|
+ itemCount: widgets.length,
|
|
|
+ itemBuilder: (context, index) {
|
|
|
+ return widgets[index];
|
|
|
+ }));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class CAppBar extends StatelessWidget implements PreferredSizeWidget {
|
|
|
+ const CAppBar({super.key});
|
|
|
|
|
|
- var configuration = CPDFConfiguration();
|
|
|
- // How to disable functionality:
|
|
|
- // setting the default display mode when opening
|
|
|
- // configuration.modeConfig = const ModeConfig(initialViewMode: CPreviewMode.digitalSignatures);
|
|
|
- // top toolbar configuration:
|
|
|
- // android:
|
|
|
- // configuration.toolbarConfig = const ToolbarConfig(androidAvailableActions: [
|
|
|
- // ToolbarAction.thumbnail, ToolbarAction.bota,
|
|
|
- // ToolbarAction.search, ToolbarAction.menu
|
|
|
- // ],
|
|
|
- // availableMenus: [
|
|
|
- // ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
|
|
|
- // ]);
|
|
|
- // iOS:
|
|
|
- // configuration.toolbarConfig = const ToolbarConfig(iosLeftBarAvailableActions: [
|
|
|
- // ToolbarAction.back, ToolbarAction.thumbnail
|
|
|
- // ],
|
|
|
- // iosRightBarAvailableActions: [
|
|
|
- // ToolbarAction.bota, ToolbarAction.search, ToolbarAction.menu
|
|
|
- // ],
|
|
|
- // availableMenus: [
|
|
|
- // ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
|
|
|
- // ]);
|
|
|
- // readerview configuration
|
|
|
- // configuration.readerViewConfig = const ReaderViewConfig(linkHighlight: true, formFieldHighlight: true);
|
|
|
- ComPDFKit.openDocument(tempDocumentPath,
|
|
|
- password: '', configuration: configuration);
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return AppBar(
|
|
|
+ title: Text(AppLocalizations.of(context)!.app_title),
|
|
|
+ actions: [
|
|
|
+ IconButton(
|
|
|
+ padding: const EdgeInsets.all(16),
|
|
|
+ onPressed: () {
|
|
|
+ Navigator.push(context, MaterialPageRoute(builder: (context) {
|
|
|
+ return const SettingsPage();
|
|
|
+ }));
|
|
|
+ },
|
|
|
+ icon: SvgPicture.asset(
|
|
|
+ 'images/ic_home_setting.svg',
|
|
|
+ width: 24,
|
|
|
+ height: 24,
|
|
|
+ color: Theme.of(context).colorScheme.onPrimary,
|
|
|
+ ))
|
|
|
+ ],
|
|
|
+ );
|
|
|
}
|
|
|
+
|
|
|
+ @override
|
|
|
+ Size get preferredSize => const Size(double.infinity, 56);
|
|
|
}
|