Home > Net >  Card Widget won't update even though I wrap in a setState
Card Widget won't update even though I wrap in a setState

Time:03-03

Below is a card widget which I have created. Why is it that when I press the button the variable cardDetailsCurrLevel even though it is inside the setState, it won't get updated? Please help. Thanks!

Widget build(BuildContext context) {
  getCardDetailsDetails(widget.cardDetailsIndex);

  return Padding(
    child: ClipRRect(
      child: Material(
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(
              width: 1.0,
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: <Widget>[
              Text(cardDetailsCurrLevel.toString()),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    cardDetailsCurrLevel = cardDetailsCurrLevel   1;
                  });
                },
                child: const SizedBox(
                  child: Center(
                    child: Text(‘Add’),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    ),
  );
}

The card widget above being used in a ListView. Edit: Added the Widget build.

@override
Widget build(BuildContext context) {

return Column(
  children: <Widget>[
    Expanded(
      child: ListView.separated(
        itemCount: fullCardDetailsList.length,
        key: UniqueKey(),
        itemBuilder: (context, index) {
          getCardDetailsDetails(fullCardDetailsList[index]);

          return Column(
          children: [
            CardDetailsCard(
              cardDetailsIndex: fullCardDetailsList[index],
            ),
          ],
          );
        },
        separatorBuilder: (context, index) {
          return const SizedBox(
            height: 0.0,
          );
        },
      ),
    ),
  ],
);
}

CodePudding user response:

you need to put your card widget code inside build method

  • Related