improve page loading

This commit is contained in:
2025-07-25 17:49:40 +02:00
parent 9bc6444d3f
commit a8c41c4b35

View File

@@ -9,8 +9,11 @@ class SheetViewerPage extends StatefulWidget {
final Sheet sheet; final Sheet sheet;
final ApiClient apiClient; final ApiClient apiClient;
const SheetViewerPage( const SheetViewerPage({
{super.key, required this.sheet, required this.apiClient}); super.key,
required this.sheet,
required this.apiClient,
});
@override @override
State<SheetViewerPage> createState() => _SheetViewerPageState(); State<SheetViewerPage> createState() => _SheetViewerPageState();
@@ -21,11 +24,13 @@ class _SheetViewerPageState extends State<SheetViewerPage> {
int page = 1; int page = 1;
int numPages = 1; int numPages = 1;
bool isFullscreen = false; bool isFullscreen = false;
late Future<bool> documentLoaded;
PdfDocument? document; PdfDocument? document;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
documentLoaded = loadPdf();
} }
@override @override
@@ -34,15 +39,15 @@ class _SheetViewerPageState extends State<SheetViewerPage> {
super.dispose(); super.dispose();
} }
Future<PdfDocument> loadPdf() async { Future<bool> loadPdf() async {
var file = await widget.apiClient.getPdfFileCached(widget.sheet.uuid); var file = await widget.apiClient.getPdfFileCached(widget.sheet.uuid);
if (file == null) { if (file == null) {
throw Exception("Failed fetching pdf file"); throw Exception("Failed fetching pdf file");
} }
var document = await PdfDocument.openFile(file.path); document = await PdfDocument.openFile(file.path);
return document; return true;
} }
void toggleFullscreen() { void toggleFullscreen() {
@@ -50,30 +55,30 @@ class _SheetViewerPageState extends State<SheetViewerPage> {
if (isFullscreen) { if (isFullscreen) {
log.info("enter fullscreen"); log.info("enter fullscreen");
// enter fullscreen // enter fullscreen
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky, SystemChrome.setEnabledSystemUIMode(
overlays: []); SystemUiMode.immersiveSticky,
overlays: [],
);
} else { } else {
// exit fullscreen // exit fullscreen
log.info("exit fullscreen"); log.info("exit fullscreen");
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, SystemChrome.setEnabledSystemUIMode(
overlays: SystemUiOverlay.values); SystemUiMode.edgeToEdge,
overlays: SystemUiOverlay.values,
);
} }
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(title: Text(widget.sheet.name)),
title: Text(widget.sheet.name),
),
body: FutureBuilder( body: FutureBuilder(
future: loadPdf(), future: documentLoaded,
builder: (BuildContext context, AsyncSnapshot<PdfDocument> snapshot) { builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData) { if (snapshot.hasData && document != null) {
document = snapshot.data;
if (document != null) {
numPages = document!.pages.length; numPages = document!.pages.length;
}
return GestureDetector( return GestureDetector(
onTapUp: (TapUpDetails details) { onTapUp: (TapUpDetails details) {
// Get the size of the screen // Get the size of the screen
@@ -100,7 +105,10 @@ class _SheetViewerPageState extends State<SheetViewerPage> {
}); });
} }
}, },
child: Stack( child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Stack(
children: [ children: [
PdfPageView( PdfPageView(
key: ValueKey(page), key: ValueKey(page),
@@ -115,20 +123,30 @@ class _SheetViewerPageState extends State<SheetViewerPage> {
), ),
], ],
), ),
// PdfPageView(
// key: ValueKey(page),
// document: document,
// pageNumber: page + 1,
// alignment: Alignment.center,
// ),
],
),
); );
} else if (snapshot.hasError) { } else if (snapshot.hasError) {
return Center( return Center(
child: Text( child: Text(
style: Theme.of(context) style: Theme.of(
.textTheme context,
.displaySmall! ).textTheme.displaySmall!.copyWith(color: Colors.red),
.copyWith(color: Colors.red),
textAlign: TextAlign.center, textAlign: TextAlign.center,
snapshot.error.toString())); snapshot.error.toString(),
),
);
} else { } else {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} }
}), },
),
); );
} }
} }