import 'package:teso/Classes/API%20Clasess/Product.dart'; import 'package:teso/Pages/PageWidgets/Explore/products.dart'; import 'package:flutter/material.dart'; import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; class TrendingAll extends StatefulWidget { final List products; const TrendingAll({Key key, this.products}) : super(key: key); @override _TrendingAllState createState() => _TrendingAllState(); } class _TrendingAllState 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.products.length > count) setState(() { show.add(widget.products.elementAt(count)); count++; imageCache.clear(); }); } } catch (e) { print(e); } } 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 Scaffold( appBar: AppBar( automaticallyImplyLeading: true, title: Text("Trending Products"), ), body: StaggeredGrid.count( crossAxisCount: 2, children: List.generate(show.length, (int index) { return index % 2 == 0 ? buildProducts(context, show.elementAt(index)) : buildProducts(context, show.elementAt(index)); }), ), ); } }