'How to create a infinite scroll on Flutter?
i'm trying to create a page with infinite scroll, i made a code but doesn't work. The pages dont refresh de the setState and aslo don't load more data.
I have another class where i use this, but i tried the same code and don't worked, is this actual page i can scroll as well, but without data update.
I tried a event listener on controller but don't have any effect on the page, if someone can help i will be really grateful!!
Here is the code:
class WidgetOrdersMobile extends StatefulWidget {
WidgetOrdersMobile({Key? key}) : super(key: key);
@override
_WidgetOrdersMobileState createState() => _WidgetOrdersMobileState();
}
class _WidgetOrdersMobileState extends State<WidgetOrdersMobile> {
List<Compras> listCompras = [];
int currentPage = 1;
bool loading = false;
bool finished = false;
final _controller = ScrollController();
@override
void initState() {
_getData(clean: true);
_controller.addListener(() async {
if (_controller.position.pixels == _controller.position.maxScrollExtent &&
!loading &&
!finished) {
currentPage++;
_getData();
}
super.initState();
});
}
void _getData({bool? clean}) async {
setState(() {
loading = true;
});
if (clean == true) FiltersController.cleanFilters();
var list = await ComprasController.fillComprasList(page: currentPage);
list.isEmpty
? setState(() {
finished = true;
})
: list.forEach((e) => {
if (e.itens != null)
{
setState(() {
listCompras.add(e);
})
}
});
setState(() {
loading = false;
});
}
@override
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
child: Column(
children: [
filtersRow(),
loading
? ListView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemCount: 10,
itemBuilder: (context, index) {
return Container(
height: 150,
width: 100,
padding: EdgeInsets.all(10),
child: LoaderShimmers());
},
)
: ListView.builder(
controller: _controller,
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemCount: listCompras.length,
itemBuilder: (context, index) {
return OrderExpansiveCard(listCompras[index]);
},
),
finished
? Center(
child: Padding(
padding: const EdgeInsets.all(15),
child: Text('Não há mais nada para mostrar'),
))
: SizedBox(height: 0),
loading ? CircularProgressIndicator() : SizedBox(height: 20)
],
),
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
