Improve sheet viewer by using pdfrx

This makes it incompatible with web
This commit is contained in:
2024-12-21 00:28:10 +01:00
parent d5d5bc6e5d
commit 118b9c8358
10 changed files with 188 additions and 63 deletions

View File

@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:sheetless/sheetview.dart';
import 'package:sheetless/sheet_viewer_page.dart';
import 'package:sheetless/storage_helper.dart';
import 'api.dart';

101
lib/sheet_viewer_page.dart Normal file
View File

@@ -0,0 +1,101 @@
import 'package:flutter/material.dart';
import 'package:pdfrx/pdfrx.dart';
import 'package:sheetless/api.dart';
import 'package:sheetless/sheet.dart';
class SheetViewerPage extends StatefulWidget {
final Sheet sheet;
final ApiClient apiClient;
const SheetViewerPage(
{super.key, required this.sheet, required this.apiClient});
@override
State<SheetViewerPage> createState() => _SheetViewerPageState();
}
class _SheetViewerPageState extends State<SheetViewerPage> {
int page = 1;
int numPages = 1;
@override
void initState() {
super.initState();
}
Future<PdfDocument> loadPdf() async {
var file = await widget.apiClient.getPdfFileCached(widget.sheet.uuid);
if (file == null) {
throw Exception("Failed fetching pdf file");
}
var document = await PdfDocument.openFile(file.path);
return document;
}
@override
Widget build(BuildContext context) {
print("Building with page: $page");
return Scaffold(
appBar: AppBar(
title: Text(widget.sheet.name),
),
body: FutureBuilder(
future: loadPdf(),
builder: (BuildContext context, AsyncSnapshot<PdfDocument> snapshot) {
if (snapshot.hasData) {
var document = snapshot.data;
if (document != null) {
numPages = document.pages.length;
}
return GestureDetector(
onTapUp: (TapUpDetails details) {
// Get the size of the screen
final screenWidth = MediaQuery.of(context).size.width;
// Check where the user tapped
if (details.localPosition.dx < screenWidth / 2) {
// Left half of the screen
setState(() {
page = page > 1 ? page - 1 : 1;
});
} else {
// Right half of the screen
setState(() {
page = page < numPages ? page + 1 : numPages;
});
}
},
child: Stack(
children: [
PdfPageView(
key: ValueKey(page),
document: document,
pageNumber: page,
alignment: Alignment.center,
),
Container(
child: Text('$page / $numPages'),
alignment: Alignment.bottomCenter,
padding: EdgeInsets.only(bottom: 5),
),
],
),
);
} else if (snapshot.hasError) {
return Center(
child: Text(
style: Theme.of(context)
.textTheme
.displaySmall!
.copyWith(color: Colors.red),
textAlign: TextAlign.center,
snapshot.error.toString()));
} else {
return const Center(child: CircularProgressIndicator());
}
}),
);
}
}

View File

@@ -1,63 +1 @@
import 'package:flutter/material.dart';
import 'package:pdfx/pdfx.dart';
import 'package:sheetless/api.dart';
import 'package:sheetless/sheet.dart';
class SheetViewerPage extends StatefulWidget {
final Sheet sheet;
final ApiClient apiClient;
const SheetViewerPage(
{super.key, required this.sheet, required this.apiClient});
@override
State<SheetViewerPage> createState() => _SheetViewerPageState();
}
class _SheetViewerPageState extends State<SheetViewerPage> {
@override
void initState() {
super.initState();
}
Future<PdfController> loadPdf() async {
var file = await widget.apiClient.getPdfFileCached(widget.sheet.uuid);
if (file == null) {
throw Exception("Failed fetching pdf file");
}
return PdfController(document: PdfDocument.openFile(file.path));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.sheet.name),
),
body: FutureBuilder(
future: loadPdf(),
builder:
(BuildContext context, AsyncSnapshot<PdfController> snapshot) {
if (snapshot.hasData) {
return PdfView(
controller: snapshot.data!,
pageSnapping: false,
scrollDirection: Axis.vertical,
);
} else if (snapshot.hasError) {
return Center(
child: Text(
style: Theme.of(context)
.textTheme
.displaySmall!
.copyWith(color: Colors.red),
textAlign: TextAlign.center,
snapshot.error.toString()));
} else {
return const Center(child: CircularProgressIndicator());
}
}),
);
}
}

View File

@@ -7,9 +7,13 @@
#include "generated_plugin_registrant.h"
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>
void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
}

View File

@@ -4,9 +4,11 @@
list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_linux
url_launcher_linux
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST
pdfrx
)
set(PLUGIN_BUNDLED_LIBRARIES)

View File

@@ -10,6 +10,7 @@ import flutter_secure_storage_macos
import path_provider_foundation
import pdfx
import sqflite_darwin
import url_launcher_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
@@ -17,4 +18,5 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
PdfxPlugin.register(with: registry.registrar(forPlugin: "PdfxPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}

View File

@@ -336,6 +336,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.0"
pdfrx:
dependency: "direct main"
description:
name: pdfrx
sha256: "29c7b03d27d647c80da8cc08bd1256c74df90e5640fdd676646e4bd04f90553a"
url: "https://pub.dev"
source: hosted
version: "1.0.94"
pdfx:
dependency: "direct main"
description:
@@ -549,6 +557,70 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
url_launcher:
dependency: transitive
description:
name: url_launcher
sha256: "9d06212b1362abc2f0f0d78e6f09f726608c74e3b9462e8368bb03314aa8d603"
url: "https://pub.dev"
source: hosted
version: "6.3.1"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
sha256: "6fc2f56536ee873eeb867ad176ae15f304ccccc357848b351f6f0d8d4a40d193"
url: "https://pub.dev"
source: hosted
version: "6.3.14"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
sha256: "16a513b6c12bb419304e72ea0ae2ab4fed569920d1c7cb850263fe3acc824626"
url: "https://pub.dev"
source: hosted
version: "6.3.2"
url_launcher_linux:
dependency: transitive
description:
name: url_launcher_linux
sha256: "4e9ba368772369e3e08f231d2301b4ef72b9ff87c31192ef471b380ef29a4935"
url: "https://pub.dev"
source: hosted
version: "3.2.1"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
sha256: "17ba2000b847f334f16626a574c702b196723af2a289e7a93ffcb79acff855c2"
url: "https://pub.dev"
source: hosted
version: "3.2.2"
url_launcher_platform_interface:
dependency: transitive
description:
name: url_launcher_platform_interface
sha256: "552f8a1e663569be95a8190206a38187b531910283c3e982193e4f2733f01029"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
url_launcher_web:
dependency: transitive
description:
name: url_launcher_web
sha256: "772638d3b34c779ede05ba3d38af34657a05ac55b06279ea6edd409e323dca8e"
url: "https://pub.dev"
source: hosted
version: "2.3.3"
url_launcher_windows:
dependency: transitive
description:
name: url_launcher_windows
sha256: "44cf3aabcedde30f2dba119a9dea3b0f2672fbe6fa96e85536251d678216b3c4"
url: "https://pub.dev"
source: hosted
version: "3.1.3"
uuid:
dependency: transitive
description:

View File

@@ -45,6 +45,7 @@ dependencies:
flutter_cache_manager: ^3.4.1
flutter_secure_storage: ^9.2.2
jwt_decoder: ^2.0.1
pdfrx: ^1.0.94
dev_dependencies:
flutter_test:

View File

@@ -9,6 +9,7 @@
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <pdfx/pdfx_plugin.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
@@ -17,4 +18,6 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("PdfxPlugin"));
PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
}

View File

@@ -6,9 +6,11 @@ list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_windows
pdfx
permission_handler_windows
url_launcher_windows
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST
pdfrx
)
set(PLUGIN_BUNDLED_LIBRARIES)