为什么我使用getX在Flutter ListView中不起作用?

发布于 2025-02-10 07:59:03 字数 925 浏览 1 评论 0原文

当我点击容器并更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2025-02-17 07:59:03

您还需要将fenuredetector包裹在OBX中,因此这样:

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);
})

因为外部OBX仅观察Controller.datasource.length

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

不语却知心 2025-02-17 07:59:03
onTap: () {
                  controller.selectedIndex.value = index;
                },);

设置索引元素而不是1
控制器是初始化的?

onTap: () {
                  controller.selectedIndex.value = index;
                },);

setup index element not 1
the controller is initialized how?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文