如何在列表中实现选择?
我有分页的职位清单。我需要用户能够选择一个位置并保存它。现在,当我放置一个复选标记并在一个页面上选择一个位置时,我遇到了一个问题,然后转到第二个位置,然后将此复选标记保存在同一位置。当您将其放在一个位置并浏览页面时,它位于同一位置。如何修复?(照片显示了它在翻页时如何工作的示例)。
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).
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您正在使用所选问题的索引,而是需要给每个选项一个唯一的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.