0

How to change the color of the tapped list item in list view? Is it possible to change the color of the corresponding list item when clicked?

Color color = Colors.red;
ListView.builder(
  physics: BouncingScrollPhysics(),
  itemCount: 10,
  itemBuilder: (BuildContext context, int index) {
    return GestureDetector(
      onTap: () {
        setState(() {
          color = Colors.blue;
        });
      },
      child: Container(
        margin: EdgeInsets.all(15),
        padding: EdgeInsets.all(10),
        height: 50,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20.0),
          border: Border.all(color: Colors.grey[300]),
          color: color
        ),
      ),
    );
  },
),

What am i doing wrong here? clicking one container changes all container's color

  • 1
    Does this answer your question? [Change background color of ListTile upon selection in Flutter](https://stackoverflow.com/questions/49331612/change-background-color-of-listtile-upon-selection-in-flutter) – kelvin Dec 05 '20 at 12:25
  • I am still confused! How to change the color of that particular container among all the containers. I have added my code. Take a look! – Srivatsan Narasimhan Dec 05 '20 at 12:47

0 Answers0