When I call the toggleSingleCardSelection, the isSelected of the chosen index will update but in my view, it will not fetch its updated data. Please help if I'm doing it the wrong way. I am also using the Get package. Using a stateful widget may solve my problem but I am learning the get package so I hope someone can guide me for my problem.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class Category {
Category({
required this.title,
required this.isSelected,
});
String? title;
bool? isSelected;
}
class MyController extends GetxController {
final discomfortData = [
Category(title: 'Ear', isSelected: false),
Category(title: 'Heart', isSelected: false),
Category(title: 'Kidney', isSelected: false),
Category(title: 'Liver', isSelected: false),
Category(title: 'Lungs', isSelected: false),
Category(title: 'Skin', isSelected: false),
Category(title: 'Stomach', isSelected: false),
Category(title: 'Throat', isSelected: false),
].obs;
void toggleSingleCardSelection(int index) {
for (var indexBtn = 0; indexBtn < discomfortData.length; indexBtn++) {
if (indexBtn == index) {
discomfortData[index].isSelected = true;
print('${dummyData[indexBtn].title} value: ${dummyData[index].isSelected}');
} else {
discomfortData[index].isSelected = false;
print('${dummyData[indexBtn].title} value: ${dummyData[index].isSelected}');
}
}
}
}
class MyView extends StatelessWidget {
final MyController controller = Get.put(MyController());
@override
Widget build(BuildContext context) {
return Obx(
() => Scaffold(
body: ListView.builder(
itemCount: controller.discomfortData.length,
itemBuilder: (ctx, index) {
return Card(
color: controller.discomfortData[index].isSelected!
? Colors.blue
: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 5,
child: InkWell(
onTap: () => controller.toggleSingleCardSelection(index),
child: SizedBox(
height: 120,
width: 120,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(controller.discomfortData[index].title!),
],
),
),
),
);
},
),
),
);
}
}
This is the log when I click the 3rd index of the card: