info: keep alive info page (by metadata sliver) but only fetch metadata when necessary
This commit is contained in:
parent
4d914d9211
commit
77be0b6189
3 changed files with 53 additions and 7 deletions
|
@ -299,6 +299,7 @@ class FullscreenVerticalPageView extends StatefulWidget {
|
|||
class _FullscreenVerticalPageViewState extends State<FullscreenVerticalPageView> {
|
||||
bool _isInitialScale = true;
|
||||
ValueNotifier<Color> _backgroundColorNotifier = ValueNotifier(Colors.black);
|
||||
ValueNotifier<bool> _infoPageVisibleNotifier = ValueNotifier(false);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
@ -343,8 +344,12 @@ class _FullscreenVerticalPageViewState extends State<FullscreenVerticalPageView>
|
|||
scrollDirection: Axis.vertical,
|
||||
controller: widget.verticalPager,
|
||||
physics: _isInitialScale ? const PageScrollPhysics() : const NeverScrollableScrollPhysics(),
|
||||
onPageChanged: widget.onVerticalPageChanged,
|
||||
onPageChanged: (page) {
|
||||
widget.onVerticalPageChanged(page);
|
||||
_infoPageVisibleNotifier.value = page == FullscreenBodyState.infoPage;
|
||||
},
|
||||
children: [
|
||||
// fake page for opacity transition between collection and fullscreen views
|
||||
const SizedBox(),
|
||||
ImagePage(
|
||||
collection: widget.collection,
|
||||
|
@ -359,7 +364,11 @@ class _FullscreenVerticalPageViewState extends State<FullscreenVerticalPageView>
|
|||
if (notification is BackUpNotification) widget.onImagePageRequested();
|
||||
return false;
|
||||
},
|
||||
child: InfoPage(collection: widget.collection, entry: widget.entry),
|
||||
child: InfoPage(
|
||||
collection: widget.collection,
|
||||
entry: widget.entry,
|
||||
visibleNotifier: _infoPageVisibleNotifier,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -14,11 +14,13 @@ import 'package:tuple/tuple.dart';
|
|||
class InfoPage extends StatefulWidget {
|
||||
final ImageCollection collection;
|
||||
final ImageEntry entry;
|
||||
final ValueNotifier<bool> visibleNotifier;
|
||||
|
||||
const InfoPage({
|
||||
Key key,
|
||||
@required this.collection,
|
||||
@required this.entry,
|
||||
this.visibleNotifier,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
|
@ -97,7 +99,11 @@ class InfoPageState extends State<InfoPage> {
|
|||
),
|
||||
SliverPadding(
|
||||
padding: horizontalPadding,
|
||||
sliver: MetadataSectionSliver(entry: entry, columnCount: split ? 2 : 1),
|
||||
sliver: MetadataSectionSliver(
|
||||
entry: entry,
|
||||
columnCount: split ? 2 : 1,
|
||||
visibleNotifier: widget.visibleNotifier,
|
||||
),
|
||||
),
|
||||
SliverPadding(
|
||||
padding: EdgeInsets.only(bottom: 8 + mqViewInsetsBottom),
|
||||
|
|
|
@ -10,34 +10,56 @@ import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
|||
class MetadataSectionSliver extends StatefulWidget {
|
||||
final ImageEntry entry;
|
||||
final int columnCount;
|
||||
final ValueNotifier<bool> visibleNotifier;
|
||||
|
||||
const MetadataSectionSliver({
|
||||
@required this.entry,
|
||||
@required this.columnCount,
|
||||
this.visibleNotifier,
|
||||
});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _MetadataSectionSliverState();
|
||||
}
|
||||
|
||||
class _MetadataSectionSliverState extends State<MetadataSectionSliver> {
|
||||
class _MetadataSectionSliverState extends State<MetadataSectionSliver> with AutomaticKeepAliveClientMixin {
|
||||
Map _metadata;
|
||||
String _loadedMetadataUri;
|
||||
|
||||
bool get isVisible => widget.visibleNotifier.value;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_registerWidget(widget);
|
||||
_getMetadata();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(MetadataSectionSliver oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_unregisterWidget(oldWidget);
|
||||
_registerWidget(widget);
|
||||
_getMetadata();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_unregisterWidget(widget);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _registerWidget(MetadataSectionSliver widget) {
|
||||
widget.visibleNotifier.addListener(_getMetadata);
|
||||
}
|
||||
|
||||
void _unregisterWidget(MetadataSectionSliver widget) {
|
||||
widget.visibleNotifier.removeListener(_getMetadata);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
debugPrint('$runtimeType build');
|
||||
super.build(context);
|
||||
final directoryNames = (_metadata?.keys?.toList() ?? [])..sort();
|
||||
return SliverStaggeredGrid.countBuilder(
|
||||
crossAxisCount: widget.columnCount,
|
||||
|
@ -57,10 +79,19 @@ class _MetadataSectionSliverState extends State<MetadataSectionSliver> {
|
|||
}
|
||||
|
||||
Future<void> _getMetadata() async {
|
||||
debugPrint('$runtimeType _getMetadata');
|
||||
_metadata = await MetadataService.getAllMetadata(widget.entry);
|
||||
if (_loadedMetadataUri == widget.entry.uri) return;
|
||||
if (isVisible) {
|
||||
_metadata = await MetadataService.getAllMetadata(widget.entry);
|
||||
_loadedMetadataUri = widget.entry.uri;
|
||||
} else {
|
||||
_metadata = null;
|
||||
_loadedMetadataUri = null;
|
||||
}
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
|
||||
class _Directory extends StatelessWidget {
|
||||
|
|
Loading…
Reference in a new issue