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;
ScrollController _gridViewController = new ScrollController();
ScrollController _parentController = new ScrollController();
int _currentIndex = 0;
int _totalLength = 1;
double _lastOffset = 0.0;
@override
Widget build(BuildContext context) {
@ -39,32 +35,12 @@ class _HomeScreenState extends State<HomeScreen> {
}
void _init(){
_gridViewController.addListener(_scrollListener);
if(bloc != null)
return;
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
void dispose() {
super.dispose();
@ -75,9 +51,7 @@ class _HomeScreenState extends State<HomeScreen> {
var widgetMap = [
<Widget>[
_buildAppBar("Gallery"),
SliverFillRemaining(
child: _buildGridView()
)
_buildGridView()
],
<Widget>[
_buildAppBar("Albums"),
@ -90,7 +64,6 @@ class _HomeScreenState extends State<HomeScreen> {
];
return CustomScrollView(
shrinkWrap: true,
controller: _parentController,
slivers: widgetMap[_currentIndex],
);
}
@ -158,23 +131,17 @@ class _HomeScreenState extends State<HomeScreen> {
Widget _buildGridView() {
MediaQueryData queryData = MediaQuery.of(context);
final int itemMultiplier = 3;
final int crossAxisCount = (queryData.size.width * itemMultiplier / 500).ceil();
final int derivedContentWidth = 500;
// Can change this to change desired image size
final int desiredContentWidth = 500;
return new StaggeredGridView.builder(
shrinkWrap: true,
return SliverStaggeredGrid.extentBuilder(
itemCount: _totalLength,
controller: _gridViewController,
gridDelegate: SliverStaggeredGridDelegateWithFixedCrossAxisCount(
staggeredTileCount: _totalLength,
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
maxCrossAxisExtent: 500 / queryData.devicePixelRatio,
staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
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;
// WidgetsBinding.instance.addPostFrameCallback((_) {
// if (_totalLength == bloc.cachedMediaItemList.length - 1)
// return;
// if (bloc.cachedMediaItemList.length == 0)
// return;
// setState(() {
// _totalLength = bloc.cachedMediaItemList.length - 1;
// });
// });
}
);
}