'setState() not updating color in listbuilder

I'm trying to change the background colour and text colour when user clicks on the container. I use setState to update the widget. It does update dateIndexSelected but does not change the colour. It's in the same context, but still not working.

onTap: () {
  setState(() {
    dateIndexSelected = index;
  });
},

GestureDetector(
  onTap: () {
    setState(() {
      dateIndexSelected = index;
    });
  },
  child: Stack(
    children: [
      Container(
        decoration: BoxDecoration(
          color: (fullIndex.contains(index))
              ? const Color.fromRGBO(203, 203, 203, 0.5)
              : (index == dateIndexSelected
                  ? const Color.fromRGBO(2, 91, 93, 1)
                  : Colors.white),
          border: Border.all(
              width: index == dateIndexSelected ? 1 : 0,
              color: const Color.fromRGBO(
                  117, 117, 117, 0.5)),
          borderRadius: BorderRadius.circular(10),
        ),
        width: MediaQuery.of(context).size.width * 0.2,
        height:
            (MediaQuery.of(context).size.height * 0.1) +
                30,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              date.day.toString(),
              style: TextStyle(
                  fontSize: 22,
                  fontWeight: FontWeight.w600,
                  height: 1.0,
                  color: lockIndex.contains(index)
                      ? const Color.fromRGBO(
                          117, 117, 117, 1)
                      : ((fullIndex.contains(index))
                          ? const Color.fromRGBO(
                              159, 159, 159, 1)
                          : (index == dateIndexSelected
                              ? Colors.white
                              : Colors.black))),
            ),
            Text(
              (index == 0)
                  ? "Today"
                  : _dayFormat(date.weekday),
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: lockIndex.contains(index)
                    ? const Color.fromRGBO(
                        117, 117, 117, 1)
                    : ((fullIndex.contains(index))
                        ? const Color.fromRGBO(
                            159, 159, 159, 1)
                        : (index == dateIndexSelected
                            ? Colors.white
                            : Colors.black)),
              ),
            )
          ],
        ),
      ).pOnly(left: 10, top: 10),
      lockIndex.contains(index)
          ? const Icon(
              Icons.lock,
              size: 25,
            ).pOnly(
              left: (MediaQuery.of(context).size.width *
                      0.2) -
                  5,
            )
          : Container(),
    ],
  ),
);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source