import 'dart:io'; import 'dart:math'; import 'package:better_open_file/better_open_file.dart'; import 'package:flutter/material.dart'; import 'package:photo_view/photo_view.dart'; import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; import 'confirmDialog.dart'; import 'placeholderView.dart'; class FileViewer extends StatefulWidget { final String path; final bool openExternal; final bool allowExternal; const FileViewer({super.key, required this.path, this.openExternal = false, this.allowExternal = true}); @override State createState() => _FileViewerState(); } class _FileViewerState extends State { PhotoViewController photoViewController = PhotoViewController(); @override Widget build(BuildContext context) { AppBar appbar({List actions = const []}) { return AppBar( title: Text(widget.path.split("/").last), actions: [ Visibility( visible: widget.allowExternal, child: IconButton(onPressed: () => ConfirmDialog( title: "Extern öffnen", content: "Möchtest du die Datei mit dem Systemdialog öffnen?", onConfirm: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => FileViewer(path: widget.path, openExternal: true))), confirmButton: "Öffnen", ).asDialog(context), icon: const Icon(Icons.open_in_new)), ), ...actions ], ); } switch(widget.openExternal ? "" : widget.path.split(".").last) { case "png": case "jpg": case "jpeg": return Scaffold( appBar: appbar( actions: [ IconButton(onPressed: () { setState(() { photoViewController.rotation += pi/2; }); }, icon: const Icon(Icons.rotate_right)), ] ), backgroundColor: Colors.white, body: PhotoView( controller: photoViewController, imageProvider: Image.file(File(widget.path)).image, backgroundDecoration: BoxDecoration(color: Theme.of(context).colorScheme.surface), ) ); case "pdf": return Scaffold( appBar: appbar(), body: SfPdfViewer.file( File(widget.path), ), ); default: OpenFile.open(widget.path).then((result) { Navigator.of(context).pop(); if(result.type != ResultType.done) { showDialog(context: context, builder: (context) { return AlertDialog( content: Text(result.message), ); }); } }); return const PlaceholderView(text: "Datei wird extern geöffnet...", icon: Icons.open_in_new); } } }