如何在列表中实现选择?

发布于 2025-02-13 10:34:01 字数 1412 浏览 0 评论 0原文

我有分页的职位清单。我需要用户能够选择一个位置并保存它。现在,当我放置一个复选标记并在一个页面上选择一个位置时,我遇到了一个问题,然后转到第二个位置,然后将此复选标记保存在同一位置。当您将其放在一个位置并浏览页面时,它位于同一位置。如何修复?(照片显示了它在翻页时如何工作的示例)。

int? _selectedPosition;

child: ListView.separated(
  shrinkWrap: true,
  itemCount: _positionsList?.length ?? 0,
  itemBuilder: (context, index) {
    return ListTile(
      trailing: _checkPosition(index)
          ? Image.asset(
              Assets.assetsCheckmark,
              width: 13,
              height: 10,
            )
          : const SizedBox.shrink(),
      title: Text(_positionsList![index].name ?? ''),
      onTap: () => _selectPosition(index),
    );
  },
  separatorBuilder: (context, index) {
    return const Divider();
  },
),

void _selectPosition(int index) {
  if (_selectedPosition != index) {
    setState(() {
      _selectedPosition = index;
    });
  } else {
    setState(() {
      _selectedPosition = null;
    });
  }
}
bool _checkPosition(int index) {
  return _selectedPosition == index ? true : false;
}

I have a list of positions with pagination. I need the user to be able to select one position and save it. Now I have such a problem, when I put a check mark and select one position on one page, and go to the second, then this check mark is saved in the same place. When you put it in one position and go through the pages, it is in the same place. How can this be fixed?(The photo shows an example of how it works when turning pages).
enter image description here

enter image description here

int? _selectedPosition;

child: ListView.separated(
  shrinkWrap: true,
  itemCount: _positionsList?.length ?? 0,
  itemBuilder: (context, index) {
    return ListTile(
      trailing: _checkPosition(index)
          ? Image.asset(
              Assets.assetsCheckmark,
              width: 13,
              height: 10,
            )
          : const SizedBox.shrink(),
      title: Text(_positionsList![index].name ?? ''),
      onTap: () => _selectPosition(index),
    );
  },
  separatorBuilder: (context, index) {
    return const Divider();
  },
),

void _selectPosition(int index) {
  if (_selectedPosition != index) {
    setState(() {
      _selectedPosition = index;
    });
  } else {
    setState(() {
      _selectedPosition = null;
    });
  }
}
bool _checkPosition(int index) {
  return _selectedPosition == index ? true : false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

野心澎湃 2025-02-20 10:34:01

似乎您正在使用所选问题的索引,而是需要给每个选项一个唯一的ID,或者在提交步骤时,您需要清除_SelectedPosition

It seems you are using the index of the selected question, instead you need to give each option a unique id, alternatively you need to clear _selectedPosition when you submit the step.

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