when I tap containter and update selectedIndex equal current index, but it not will update.
class HomeController extends GetxController {
final RxList<PurineTypeModel> dataSource = RxList();
final selectedIndex = 0.obs;
}
Obx(() {
return ListView.builder(
cacheExtent: 30,
scrollDirection: Axis.horizontal,
itemBuilder: (c, index) {
print("refresh?? $index");
return GestureDetector(child: Container(
width: 100,
color: controller.selectedIndex.value == index ? Colors.red : Colors.green,
child: Text(
controller.selectedIndex.string),
), onTap: () {
controller.selectedIndex.value = 1;
},);
},
itemCount: controller.dataSource.length);
})
CodePudding user response:
onTap: () {
controller.selectedIndex.value = index;
},);
setup index element not 1 the controller is initialized how?
CodePudding user response:
You need to wrap the GestureDetector also in an Obx, so like this:
Obx(() {
return ListView.builder(
cacheExtent: 30,
scrollDirection: Axis.horizontal,
itemBuilder: (c, index) {
print("refresh?? $index");
return Obx(() =>
GestureDetector(child: Container(
width: 100,
color: controller.selectedIndex.value == index ? Colors
.red : Colors.green,
child: Text(
controller.selectedIndex.string),
), onTap: () {
controller.selectedIndex.value = 1;
},));
},
itemCount: controller.dataSource.length);
})
Because the outer Obx only observes controller.dataSource.length