import 'package:flutter/material.dart'; import 'package:teso/Classes/TesoShop.dart'; import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; import 'package:teso/Pages/PageWidgets/Explore/business.dart'; class ExploreBusiness extends StatefulWidget { final List businesses; const ExploreBusiness({Key key, this.businesses}) : super(key: key); @override _ExploreBusinessState createState() => _ExploreBusinessState(); } class _ExploreBusinessState extends State { ScrollController _controller; List show = []; int count = 0; Future fetchImages() async { try { count = show.length; for (int i = 0; i <= 9; i++) { if (widget.businesses.length > count) setState(() { show.add(widget.businesses.elementAt(count)); count++; imageCache.clear(); }); } } catch (e) { print(e); } } @override void didUpdateWidget(covariant ExploreBusiness oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.businesses != widget.businesses || widget.businesses.length == 0) { show = []; fetchImages(); } } void _scrollListener() { if (_controller.offset >= _controller.position.maxScrollExtent && !_controller.position.outOfRange) { fetchImages(); } } @override void initState() { _controller = ScrollController(); _controller.addListener(_scrollListener); count = 0; fetchImages(); super.initState(); } @override Widget build(BuildContext context) { return Container( child: StaggeredGridView.count( controller: _controller, crossAxisCount: 2, children: List.generate(show.length, (int index) { return buildBusiness(context, show.elementAt(index)); }), staggeredTiles: List.generate( show.length, (int index) { return StaggeredTile.fit(1); }, ), ), ); } }