From 3cb3e12c94315073cd507705ce37db8a7535dc38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Thu, 6 Jul 2023 22:46:16 +0200 Subject: [PATCH] Better rotation for image FileViewer --- lib/widget/fileViewer.dart | 63 +++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/lib/widget/fileViewer.dart b/lib/widget/fileViewer.dart index 6812c94..07986e1 100644 --- a/lib/widget/fileViewer.dart +++ b/lib/widget/fileViewer.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'dart:math'; import 'package:better_open_file/better_open_file.dart'; import 'package:flutter/material.dart'; @@ -8,54 +9,72 @@ import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; import 'confirmDialog.dart'; import 'placeholderView.dart'; -class FileViewer extends StatelessWidget { +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 = AppBar( - title: Text(path.split("/").last), - actions: [ - Visibility( - visible: 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: path, openExternal: true))), - confirmButton: "Öffnen", - ).asDialog(context), icon: const Icon(Icons.open_in_new)), - ) - ], - ); - switch(openExternal ? "" : path.split(".").last) { + 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, + appBar: appbar( + actions: [ + IconButton(onPressed: () { + setState(() { + photoViewController.rotation += pi/2; + }); + }, icon: const Icon(Icons.rotate_right)), + ] + ), backgroundColor: Colors.white, body: PhotoView( - imageProvider: Image.file(File(path)).image, + controller: photoViewController, + imageProvider: Image.file(File(widget.path)).image, backgroundDecoration: BoxDecoration(color: Theme.of(context).colorScheme.surface), - enableRotation: true, ) ); case "pdf": return Scaffold( - appBar: appbar, + appBar: appbar(), body: SfPdfViewer.file( - File(path), + File(widget.path), ), ); default: - OpenFile.open(path).then((result) { + OpenFile.open(widget.path).then((result) { Navigator.of(context).pop(); if(result.type != ResultType.done) { showDialog(context: context, builder: (context) {