Merge pull request #686 from ente-io/grid

Fix stutter while scrolling
This commit is contained in:
Vishnu Mohandas 2022-12-07 09:42:22 +05:30 committed by GitHub
commit f9d6b0ca53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View file

@ -232,7 +232,12 @@ class _LazyLoadingGalleryState extends State<LazyLoadingGallery> {
) )
], ],
), ),
_shouldRender ? _getGallery() : PlaceHolderWidget(_files.length), _shouldRender
? _getGallery()
: PlaceHolderWidget(
_files.length,
LocalSettings.instance.getPhotoGridSize(),
),
], ],
); );
} }
@ -299,6 +304,7 @@ class LazyLoadingGridView extends StatefulWidget {
class _LazyLoadingGridViewState extends State<LazyLoadingGridView> { class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
bool _shouldRender; bool _shouldRender;
int _photoGridSize;
StreamSubscription<ClearSelectionsEvent> _clearSelectionsEvent; StreamSubscription<ClearSelectionsEvent> _clearSelectionsEvent;
@override @override
@ -334,6 +340,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
_photoGridSize = LocalSettings.instance.getPhotoGridSize();
if (widget.shouldRecycle) { if (widget.shouldRecycle) {
return _getRecyclableView(); return _getRecyclableView();
} else { } else {
@ -354,7 +361,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
}, },
child: _shouldRender child: _shouldRender
? _getGridView() ? _getGridView()
: PlaceHolderWidget(widget.filesInDay.length), : PlaceHolderWidget(widget.filesInDay.length, _photoGridSize),
); );
} }
@ -369,7 +376,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
}); });
} }
}, },
child: PlaceHolderWidget(widget.filesInDay.length), child: PlaceHolderWidget(widget.filesInDay.length, _photoGridSize),
); );
} else { } else {
return _getGridView(); return _getGridView();
@ -388,7 +395,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 2, crossAxisSpacing: 2,
mainAxisSpacing: 2, mainAxisSpacing: 2,
crossAxisCount: LocalSettings.instance.getPhotoGridSize(), crossAxisCount: _photoGridSize,
), ),
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
); );

View file

@ -4,18 +4,20 @@ import 'package:flutter/material.dart';
class PlaceHolderWidget extends StatelessWidget { class PlaceHolderWidget extends StatelessWidget {
const PlaceHolderWidget( const PlaceHolderWidget(
this.count, { this.count,
this.columns, {
Key key, Key key,
}) : super(key: key); }) : super(key: key);
final int count; final int count, columns;
static final _gridViewCache = <int, GridView>{}; static final _gridViewCache = <String, GridView>{};
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (!_gridViewCache.containsKey(count)) { final key = _getCacheKey(count, columns);
_gridViewCache[count] = GridView.builder( if (!_gridViewCache.containsKey(key)) {
_gridViewCache[key] = GridView.builder(
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
shrinkWrap: true, shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
@ -26,11 +28,15 @@ class PlaceHolderWidget extends StatelessWidget {
); );
}, },
itemCount: count, itemCount: count,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, crossAxisCount: columns,
), ),
); );
} }
return _gridViewCache[count]; return _gridViewCache[key];
}
String _getCacheKey(int totalCount, int columns) {
return totalCount.toString() + ":" + columns.toString();
} }
} }