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> {
bool _shouldRender;
int _photoGridSize;
StreamSubscription<ClearSelectionsEvent> _clearSelectionsEvent;
@override
@ -334,6 +340,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
@override
Widget build(BuildContext context) {
_photoGridSize = LocalSettings.instance.getPhotoGridSize();
if (widget.shouldRecycle) {
return _getRecyclableView();
} else {
@ -354,7 +361,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
},
child: _shouldRender
? _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 {
return _getGridView();
@ -388,7 +395,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 2,
mainAxisSpacing: 2,
crossAxisCount: LocalSettings.instance.getPhotoGridSize(),
crossAxisCount: _photoGridSize,
),
padding: const EdgeInsets.all(0),
);

View file

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