Przeglądaj źródła

Fix: Delete files from memory widget (#1111)

Neeraj Gupta 2 lat temu
rodzic
commit
e5af9ab928
1 zmienionych plików z 34 dodań i 16 usunięć
  1. 34 16
      lib/ui/home/memories/full_screen_memory.dart

+ 34 - 16
lib/ui/home/memories/full_screen_memory.dart

@@ -3,6 +3,7 @@ import "dart:io";
 import "package:flutter/cupertino.dart";
 import "package:flutter/cupertino.dart";
 import "package:flutter/material.dart";
 import "package:flutter/material.dart";
 import "package:intl/intl.dart";
 import "package:intl/intl.dart";
+import 'package:photos/models/file.dart';
 import "package:photos/models/memory.dart";
 import "package:photos/models/memory.dart";
 import "package:photos/services/memories_service.dart";
 import "package:photos/services/memories_service.dart";
 import "package:photos/theme/text_style.dart";
 import "package:photos/theme/text_style.dart";
@@ -124,6 +125,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
       ),
       ),
       extendBodyBehindAppBar: true,
       extendBodyBehindAppBar: true,
       body: Container(
       body: Container(
+        key: ValueKey(widget.memories.length),
         color: Colors.black,
         color: Colors.black,
         child: Stack(
         child: Stack(
           alignment: Alignment.bottomCenter,
           alignment: Alignment.bottomCenter,
@@ -138,18 +140,33 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
     );
     );
   }
   }
 
 
-  void onFileDeleted() {
-    if (widget.memories.length == 1) {
-      Navigator.pop(context);
+  Future<void> onFileDeleted(Memory removedMemory) async {
+    if (!mounted) {
+      return;
+    }
+    final totalFiles = widget.memories.length;
+    if (totalFiles == 1) {
+      // Deleted the only file
+      Navigator.of(context).pop(); // Close pageview
+      return;
+    }
+    if (_index == totalFiles - 1) {
+      // Deleted the last file
+      await _pageController!.previousPage(
+        duration: const Duration(milliseconds: 200),
+        curve: Curves.easeInOut,
+      );
+      setState(() {
+        widget.memories.remove(removedMemory);
+      });
     } else {
     } else {
+      await _pageController!.nextPage(
+        duration: const Duration(milliseconds: 200),
+        curve: Curves.easeInOut,
+      );
       setState(() {
       setState(() {
-        if (_index != 0) {
-          _pageController?.jumpToPage(_index - 1);
-        }
-        widget.memories.removeAt(_index);
-        if (_index != 0) {
-          _index--;
-        }
+        _index--;
+        widget.memories.remove(removedMemory);
       });
       });
     }
     }
   }
   }
@@ -180,7 +197,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
   }
   }
 
 
   Widget _buildBottomIcons() {
   Widget _buildBottomIcons() {
-    final file = widget.memories[_index].file;
+    final File currentFile = widget.memories[_index].file;
     return SafeArea(
     return SafeArea(
       child: Container(
       child: Container(
         alignment: Alignment.bottomCenter,
         alignment: Alignment.bottomCenter,
@@ -194,7 +211,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
                 color: Colors.white, //same for both themes
                 color: Colors.white, //same for both themes
               ),
               ),
               onPressed: () {
               onPressed: () {
-                showDetailsSheet(context, file);
+                showDetailsSheet(context, currentFile);
               },
               },
             ),
             ),
             IconButton(
             IconButton(
@@ -207,14 +224,15 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
               onPressed: () async {
               onPressed: () async {
                 await showSingleFileDeleteSheet(
                 await showSingleFileDeleteSheet(
                   context,
                   context,
-                  file,
-                  onFileRemoved: (file) => {onFileDeleted()},
+                  currentFile,
+                  onFileRemoved: (file) =>
+                      {onFileDeleted(widget.memories[_index])},
                 );
                 );
               },
               },
             ),
             ),
             SizedBox(
             SizedBox(
               height: 32,
               height: 32,
-              child: FavoriteWidget(file),
+              child: FavoriteWidget(currentFile),
             ),
             ),
             IconButton(
             IconButton(
               icon: Icon(
               icon: Icon(
@@ -222,7 +240,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
                 color: Colors.white, //same for both themes
                 color: Colors.white, //same for both themes
               ),
               ),
               onPressed: () {
               onPressed: () {
-                share(context, [file]);
+                share(context, [currentFile]);
               },
               },
             ),
             ),
           ],
           ],