I have a ListView of customers where the user is able to check/uncheck each customer to mark them as a 'favourite'. However, each time a ListItem is tapped, the ListView jumps back to the top of the screen.
How do I prevent this from happening? I only want the Checkbox to refresh, not the entire screen, and to prevent the screen from jumping to the top each time.
@override
Widget build(BuildContext context) {
customers = getAllCustomers();
return Scaffold(
appBar: _getAppBar(),
body: customers != null
? FutureBuilder(
future: getFavouriteCustomers(), // contains IDs of favourite customers
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
List<String> favouriteCustomersList = snapshot.data;
return ListView.builder(
itemCount: customers?.length ?? 0,
itemBuilder: (BuildContext context, int index) {
customer c = customers?.elementAt(index);
if (favouriteCustomersList.contains(c.id)) {
c.isSelected = true;
}
return ListTile(
title: Text(c.name),
trailing: Checkbox(
value: c.isFavourite,
onChanged: (newValue) {}),
onTap: () {
if (c.isSelected) {
setState(() {
c.setFavourite(false);
});
} else {
setState(() {
c.setFavourite(true);
}
}
},
);
});
}
} else {
return CircularProgressIndicator();
}
})
: Center(
child: CircularProgressIndicator(),
);
);
}
ListView.builder(controller: ScrollController(keepScrollOffset: true),)
to your ListView? – Bitterling