Fix: Delete files from memory widget (#1111)

This commit is contained in:
Neeraj Gupta 2023-05-18 23:15:39 +05:30 committed by GitHub
commit e5af9ab928
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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() { Future<void> onFileDeleted(Memory removedMemory) async {
if (widget.memories.length == 1) { if (!mounted) {
Navigator.pop(context); return;
} else { }
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(() { setState(() {
if (_index != 0) { widget.memories.remove(removedMemory);
_pageController?.jumpToPage(_index - 1); });
} } else {
widget.memories.removeAt(_index); await _pageController!.nextPage(
if (_index != 0) { duration: const Duration(milliseconds: 200),
_index--; curve: Curves.easeInOut,
} );
setState(() {
_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, currentFile,
onFileRemoved: (file) => {onFileDeleted()}, 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]);
}, },
), ),
], ],