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 NewArrivals extends StatefulWidget { final List? products; const NewArrivals({Key? key, this.products}) : super(key: key); @override _NewArrivalsState createState() => _NewArrivalsState(); } class _NewArrivalsState extends State { late 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("New Arrivals"), ), 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)); }), ), ); } }