为什么我使用getX在Flutter ListView中不起作用?
当我点击容器并更新SelectedIndex等于电流索引时,但不会更新。
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);
})
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);
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还需要将fenuredetector包裹在OBX中,因此这样:
因为外部OBX仅观察
Controller.datasource.length
You need to wrap the GestureDetector also in an Obx, so like this:
Because the outer Obx only observes
controller.dataSource.length
设置索引元素而不是1
控制器是初始化的?
setup index element not 1
the controller is initialized how?