Как сделать чтобы каждому элементу были свои значения, при том изначально неизвестны количество элементов Flutter
Когда я нажимаю на кнопку "Сохранить в избранные" меняются все, логично y всеx одно значение, вот как сделать каждому по отдельности с index
class _FavoritesPageState extends State<FavoritesPage> {
bool _isFav = false;
void _toggleFav() {
setState(() {
_isFav = !_isFav;
});
}
Widget _build(BuildContext context, Animal animal) {
String gen = animal.animalGenderId == 1 ? "Еркек" : "Ұрғашы";
return Container(
margin: EdgeInsets.symmetric(horizontal: 8, vertical: 6),
// height: 110,
decoration: BoxDecoration(
color: Theme.of(context).brightness == Brightness.light
? Colors.white
: Color.fromRGBO(28, 28, 30, 1),
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
offset: Offset(0, 1),
blurRadius: 3,
color: Color(0xFF12153D).withOpacity(0.2),
),
],
),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AnimalDetails(
animalId: animal.id,
)));
},
child: Container(
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
//Image
ClipRRect(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10),
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
bottomRight: Radius.circular(10)),
child: Image.asset(
_findPhotoById(animal.photoId),
fit: BoxFit.cover,
height: 90,
width: 90,
),
),
//Desc
Expanded(
child: Container(
margin: EdgeInsets.only(left: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
//title
Container(
margin: EdgeInsets.only(bottom: 5, top: 0),
padding: EdgeInsets.only(top: 0),
child: Text(
"${_findAnimalTypeById(animal.animalType)}, ${animal.breed}, ${animal.weight}кг, $gen, ${animal.age} жас",
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
textAlign: TextAlign.left,
),
),
//Cost
Container(
margin: EdgeInsets.symmetric(
vertical: 5,
),
padding: EdgeInsets.all(4),
constraints:
BoxConstraints(maxHeight: 40, minWidth: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color:
Theme.of(context).brightness == Brightness.light
? mainColor
: Color(0XFF2D2D2D)),
child: Text(
'${animal.cost}$tenge',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.w500),
),
),
],
),
),
),
Container(
child: IconButton(
icon: Icon(_isFav ? Icons.favorite : Icons.favorite_border,
color: _isFav
? Colors.red[700]
: Theme.of(context).brightness == Brightness.light
? mainColor
: Colors.white),
onPressed: _toggleFav,
),
)
],
),
),
),
);
}
String _findPhotoById(int photosId) {
String res = '';
for (int i = 0; i < animalPhotos.length; i++) {
if (photosId == animalPhotos[i].id) {
res = animalPhotos[i].imgUrl;
}
}
return res;
}
String _findAnimalTypeById(int animalId) {
String res = '';
for (int i = 0; i < animalType.length; i++) {
if (animalId == animalType[i].id) {
res = animalType[i].name;
}
}
return res;
}
}