Do Grid View Better

This commit is contained in:
Evan Reichard 2021-03-04 01:06:09 -05:00
parent eba444459b
commit 5e83740f57

View File

@ -21,12 +21,8 @@ class _HomeScreenState extends State<HomeScreen> {
HomeBloc bloc; HomeBloc bloc;
ScrollController _gridViewController = new ScrollController();
ScrollController _parentController = new ScrollController();
int _currentIndex = 0; int _currentIndex = 0;
int _totalLength = 1; int _totalLength = 1;
double _lastOffset = 0.0;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -39,32 +35,12 @@ class _HomeScreenState extends State<HomeScreen> {
} }
void _init(){ void _init(){
_gridViewController.addListener(_scrollListener);
if(bloc != null) if(bloc != null)
return; return;
bloc = HomeBloc(AppProvider.getApplication(context)); bloc = HomeBloc(AppProvider.getApplication(context));
} }
// Needed Due to Nested Scroll Views
void _scrollListener() {
// Reflect Downwards & Upwards Scrolling (Respectively) on Parent
if (_gridViewController.offset > _lastOffset) {
if (_parentController.offset < _parentController.position.maxScrollExtent) {
_parentController.jumpTo(_parentController.offset + (_lastOffset - _gridViewController.offset).abs());
}
} else if (_gridViewController.offset < _lastOffset) {
if (_parentController.offset > 0) {
_parentController.jumpTo(_parentController.offset - (_lastOffset - _gridViewController.offset).abs());
}
}
// Don't update offset on top or bottom bounce
if (_gridViewController.offset < _gridViewController.position.maxScrollExtent && _gridViewController.offset > 0)
_lastOffset = _gridViewController.offset;
}
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
@ -75,9 +51,7 @@ class _HomeScreenState extends State<HomeScreen> {
var widgetMap = [ var widgetMap = [
<Widget>[ <Widget>[
_buildAppBar("Gallery"), _buildAppBar("Gallery"),
SliverFillRemaining( _buildGridView()
child: _buildGridView()
)
], ],
<Widget>[ <Widget>[
_buildAppBar("Albums"), _buildAppBar("Albums"),
@ -90,7 +64,6 @@ class _HomeScreenState extends State<HomeScreen> {
]; ];
return CustomScrollView( return CustomScrollView(
shrinkWrap: true, shrinkWrap: true,
controller: _parentController,
slivers: widgetMap[_currentIndex], slivers: widgetMap[_currentIndex],
); );
} }
@ -158,23 +131,17 @@ class _HomeScreenState extends State<HomeScreen> {
Widget _buildGridView() { Widget _buildGridView() {
MediaQueryData queryData = MediaQuery.of(context); MediaQueryData queryData = MediaQuery.of(context);
final int itemMultiplier = 3; // Can change this to change desired image size
final int crossAxisCount = (queryData.size.width * itemMultiplier / 500).ceil(); final int desiredContentWidth = 500;
final int derivedContentWidth = 500;
return new StaggeredGridView.builder( return SliverStaggeredGrid.extentBuilder(
shrinkWrap: true,
itemCount: _totalLength, itemCount: _totalLength,
controller: _gridViewController,
gridDelegate: SliverStaggeredGridDelegateWithFixedCrossAxisCount(
staggeredTileCount: _totalLength,
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4.0, mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0, crossAxisSpacing: 4.0,
maxCrossAxisExtent: 500 / queryData.devicePixelRatio,
staggeredTileBuilder: (int index) => new StaggeredTile.fit(1), staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
),
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
return _buildCard(index, derivedContentWidth); return _buildCard(index, desiredContentWidth);
}, },
); );
} }
@ -196,19 +163,6 @@ class _HomeScreenState extends State<HomeScreen> {
}); });
return snapshot.data; return snapshot.data;
// WidgetsBinding.instance.addPostFrameCallback((_) {
// if (_totalLength == bloc.cachedMediaItemList.length - 1)
// return;
// if (bloc.cachedMediaItemList.length == 0)
// return;
// setState(() {
// _totalLength = bloc.cachedMediaItemList.length - 1;
// });
// });
} }
); );
} }