I need help with this task. This is a test version. In my real project, I get data from the server, namely the id and username. I need to change the color of a certain ListTile by clicking on IconButton.
class StartPage extends StatelessWidget {
const StartPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: ids.length,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(20),
child: MyListTile(
index: index,
id: ids[index],
),
),
),
);
}
}
class MyListTile extends StatefulWidget {
const MyListTile({
super.key,
required this.index,
required this.id,
});
final int index;
final int id;
@override
State<MyListTile> createState() => _MyListTileState();
}
class _MyListTileState extends State<MyListTile> {
bool clicked = false;
@override
Widget build(BuildContext context) {
return Container(
color: clicked ? Colors.blue : Colors.transparent,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text('id: ${widget.id}, index: ${widget.index}'),
IconButton(
onPressed: () {
setState(() {
!clicked;
});
},
icon: const Icon(Icons.add_circle, size: 30)),
],
),
);
}
}
List<int> ids = [500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510];
It is necessary to remember the ID depending on the pressed list item. Most likely, I need to compare the selected ID and the ID of the tile.