claude refactorings, flutter best practices, platform dependent changes, general cleanup
This commit is contained in:
+73
-16
@@ -1,16 +1,17 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:open_filex/open_filex.dart';
|
||||
import 'package:photo_view/photo_view.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
|
||||
|
||||
import '../routing/app_routes.dart';
|
||||
import '../state/app/modules/settings/bloc/settings_cubit.dart';
|
||||
import '../utils/file_saver.dart';
|
||||
import 'info_dialog.dart';
|
||||
import 'placeholder_view.dart';
|
||||
import 'share_position_origin.dart';
|
||||
@@ -30,6 +31,55 @@ enum FileViewingActions {
|
||||
save
|
||||
}
|
||||
|
||||
/// Workaround for a Syncfusion PDF viewer race: SfPdfViewer's internal
|
||||
/// LayoutBuilder calls `localToGlobal` during build, which asserts when an
|
||||
/// ancestor RenderTransform (from the page-push animation) is still mid-layout.
|
||||
/// We wait for the route's enter animation to complete before mounting it.
|
||||
class _DeferredPdfViewer extends StatefulWidget {
|
||||
const _DeferredPdfViewer({required this.path});
|
||||
final String path;
|
||||
|
||||
@override
|
||||
State<_DeferredPdfViewer> createState() => _DeferredPdfViewerState();
|
||||
}
|
||||
|
||||
class _DeferredPdfViewerState extends State<_DeferredPdfViewer> {
|
||||
bool _ready = false;
|
||||
Animation<double>? _routeAnimation;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
if (_ready || _routeAnimation != null) return;
|
||||
final animation = ModalRoute.of(context)?.animation;
|
||||
if (animation == null || animation.isCompleted) {
|
||||
_ready = true;
|
||||
return;
|
||||
}
|
||||
_routeAnimation = animation..addStatusListener(_onAnimationStatus);
|
||||
}
|
||||
|
||||
void _onAnimationStatus(AnimationStatus status) {
|
||||
if (status == AnimationStatus.completed && mounted) {
|
||||
setState(() => _ready = true);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_routeAnimation?.removeStatusListener(_onAnimationStatus);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!_ready) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
return SfPdfViewer.file(File(widget.path));
|
||||
}
|
||||
}
|
||||
|
||||
class _FileViewerState extends State<FileViewer> {
|
||||
PhotoViewController photoViewController = PhotoViewController();
|
||||
|
||||
@@ -44,7 +94,7 @@ class _FileViewerState extends State<FileViewer> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
AppBar appbar({List actions = const []}) => AppBar(
|
||||
AppBar appbar({List<Widget> actions = const []}) => AppBar(
|
||||
title: Text(widget.path.split('/').last),
|
||||
actions: [
|
||||
...actions,
|
||||
@@ -55,17 +105,26 @@ class _FileViewerState extends State<FileViewer> {
|
||||
AppRoutes.openFileViewer(context, widget.path, openExternal: true);
|
||||
break;
|
||||
case FileViewingActions.share:
|
||||
SharePlus.instance.share(
|
||||
ShareParams(
|
||||
files: [XFile(widget.path)],
|
||||
sharePositionOrigin: SharePositionOrigin.get(context)
|
||||
)
|
||||
);
|
||||
unawaited(SharePlus.instance.share(
|
||||
ShareParams(
|
||||
files: [XFile(widget.path)],
|
||||
sharePositionOrigin: SharePositionOrigin.get(context),
|
||||
),
|
||||
));
|
||||
break;
|
||||
case FileViewingActions.save:
|
||||
await FileSaver.writeBytes(await File(widget.path).readAsBytes(), widget.path.split('/').last);
|
||||
if(!context.mounted) return;
|
||||
InfoDialog.show(context, 'Die Datei wurde im Downloads Ordner gespeichert.');
|
||||
try {
|
||||
final bytes = await File(widget.path).readAsBytes();
|
||||
final saved = await FilePicker.saveFile(
|
||||
fileName: widget.path.split('/').last,
|
||||
bytes: bytes,
|
||||
);
|
||||
if (!context.mounted) return;
|
||||
if (saved != null) InfoDialog.show(context, 'Datei gespeichert.');
|
||||
} on Object catch (e) {
|
||||
if (!context.mounted) return;
|
||||
InfoDialog.show(context, 'Speichern fehlgeschlagen: $e');
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
@@ -86,7 +145,7 @@ class _FileViewerState extends State<FileViewer> {
|
||||
dense: true,
|
||||
),
|
||||
),
|
||||
if(Platform.isAndroid) const PopupMenuItem(
|
||||
const PopupMenuItem(
|
||||
value: FileViewingActions.save,
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.save_alt_outlined),
|
||||
@@ -129,9 +188,7 @@ class _FileViewerState extends State<FileViewer> {
|
||||
case 'pdf':
|
||||
return Scaffold(
|
||||
appBar: appbar(),
|
||||
body: SfPdfViewer.file(
|
||||
File(widget.path),
|
||||
),
|
||||
body: _DeferredPdfViewer(path: widget.path),
|
||||
);
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user