used an InheritedWidget to hold list of memories and update the InheritedWidget when an item is deleted, which rebuilds widgets that depend on the inheritedWidget
This commit is contained in:
parent
4063f75d4a
commit
060b516058
|
@ -2,31 +2,107 @@ import "dart:io";
|
|||
|
||||
import "package:flutter/cupertino.dart";
|
||||
import "package:flutter/material.dart";
|
||||
import "package:photos/core/configuration.dart";
|
||||
import "package:photos/models/memory.dart";
|
||||
import "package:photos/ui/actions/file/file_actions.dart";
|
||||
import "package:photos/ui/viewer/file/file_widget.dart";
|
||||
|
||||
class FullScreenMemoryDataUpdater extends StatefulWidget {
|
||||
final List<Memory> memories;
|
||||
final int initialIndex;
|
||||
final Widget child;
|
||||
const FullScreenMemoryDataUpdater({
|
||||
required this.memories,
|
||||
required this.initialIndex,
|
||||
required this.child,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<FullScreenMemoryDataUpdater> createState() =>
|
||||
_FullScreenMemoryDataUpdaterState();
|
||||
}
|
||||
|
||||
class _FullScreenMemoryDataUpdaterState
|
||||
extends State<FullScreenMemoryDataUpdater> {
|
||||
late ValueNotifier<int> indexNotifier;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
indexNotifier = ValueNotifier(widget.initialIndex);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
indexNotifier.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void removeCurrentMemory() {
|
||||
setState(() {
|
||||
widget.memories.removeAt(indexNotifier.value);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FullScreenMemoryData(
|
||||
memories: widget.memories,
|
||||
indexNotifier: indexNotifier,
|
||||
removeCurrentMemory: removeCurrentMemory,
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class FullScreenMemoryData extends InheritedWidget {
|
||||
final List<Memory> memories;
|
||||
final ValueNotifier<int> indexNotifier;
|
||||
final VoidCallback removeCurrentMemory;
|
||||
|
||||
const FullScreenMemoryData({
|
||||
required this.memories,
|
||||
required this.indexNotifier,
|
||||
required this.removeCurrentMemory,
|
||||
required Widget child,
|
||||
Key? key,
|
||||
}) : super(child: child, key: key);
|
||||
|
||||
static FullScreenMemoryData? of(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<FullScreenMemoryData>();
|
||||
}
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(FullScreenMemoryData oldWidget) {
|
||||
// Checking oldWidget.memories.length != memories.length here doesn't work
|
||||
//because the old widget and new widget reference the same memories list.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class FullScreenMemoryNew extends StatefulWidget {
|
||||
final String title;
|
||||
final List<Memory> memories;
|
||||
final int index;
|
||||
const FullScreenMemoryNew(this.title, this.memories, this.index, {super.key});
|
||||
final int initialIndex;
|
||||
const FullScreenMemoryNew(
|
||||
this.title,
|
||||
this.initialIndex, {
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<FullScreenMemoryNew> createState() => _FullScreenMemoryNewState();
|
||||
}
|
||||
|
||||
class _FullScreenMemoryNewState extends State<FullScreenMemoryNew> {
|
||||
late final ValueNotifier<int> _currentIndex;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_currentIndex = ValueNotifier(widget.index);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final inheritedData = FullScreenMemoryData.of(context)!;
|
||||
return Scaffold(
|
||||
extendBodyBehindAppBar: true,
|
||||
appBar: AppBar(),
|
||||
|
@ -36,43 +112,57 @@ class _FullScreenMemoryNewState extends State<FullScreenMemoryNew> {
|
|||
alignment: Alignment.bottomCenter,
|
||||
children: [
|
||||
FileWidget(
|
||||
widget.memories[index].file,
|
||||
inheritedData.memories[index].file,
|
||||
autoPlay: false,
|
||||
tagPrefix: "memories",
|
||||
backgroundDecoration: const BoxDecoration(
|
||||
color: Colors.transparent,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Platform.isAndroid
|
||||
? Icons.delete_outline
|
||||
: CupertinoIcons.delete,
|
||||
color: Colors.white, //same for both themes
|
||||
),
|
||||
onPressed: () async {
|
||||
await showSingleFileDeleteSheet(
|
||||
context,
|
||||
widget.memories[_currentIndex.value].file,
|
||||
onFileRemoved: (file) =>
|
||||
{onFileDeleted(widget.memories[_currentIndex.value])},
|
||||
);
|
||||
},
|
||||
),
|
||||
Configuration.instance.getUserID() !=
|
||||
inheritedData.memories[index].file.ownerID
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(64.0),
|
||||
child: Container(
|
||||
color: Colors.red,
|
||||
height: 30,
|
||||
width: 30,
|
||||
),
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
const BottomIcons(),
|
||||
],
|
||||
);
|
||||
},
|
||||
onPageChanged: (index) {
|
||||
_currentIndex.value = index;
|
||||
inheritedData.indexNotifier.value = index;
|
||||
},
|
||||
itemCount: widget.memories.length,
|
||||
itemCount: inheritedData.memories.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> onFileDeleted(Memory removedMemory) async {
|
||||
setState(() {
|
||||
widget.memories.remove(removedMemory);
|
||||
});
|
||||
class BottomIcons extends StatelessWidget {
|
||||
const BottomIcons({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final inheritedData = FullScreenMemoryData.of(context)!;
|
||||
return IconButton(
|
||||
icon: Icon(
|
||||
Platform.isAndroid ? Icons.delete_outline : CupertinoIcons.delete,
|
||||
color: Colors.white, //same for both themes
|
||||
),
|
||||
onPressed: () async {
|
||||
await showSingleFileDeleteSheet(
|
||||
context,
|
||||
inheritedData.memories[inheritedData.indexNotifier.value].file,
|
||||
onFileRemoved: (file) => {
|
||||
inheritedData.removeCurrentMemory.call(),
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,11 @@ class _MemoryCovertWidgetState extends State<MemoryCovertWidget> {
|
|||
onTap: () async {
|
||||
await routeToPage(
|
||||
context,
|
||||
FullScreenMemoryNew(title, widget.memories, index),
|
||||
FullScreenMemoryDataUpdater(
|
||||
initialIndex: index,
|
||||
memories: widget.memories,
|
||||
child: FullScreenMemoryNew(title, index),
|
||||
),
|
||||
forceCustomPageRoute: true,
|
||||
);
|
||||
setState(() {});
|
||||
|
|
Loading…
Reference in a new issue