首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListTile单选(颤振)

ListTile单选(颤振)
EN

Stack Overflow用户
提问于 2021-01-27 09:40:33
回答 2查看 1.1K关注 0票数 2

当然,这是一个简单的解决方案,但我不知道它-我很新的颤音-仍然。有什么问题吗?

我有警报对话框与4个列表瓷砖,应该作为选择器的gps半径,但我有一个问题的单一选择。现在,当一个被选中的时候,所有的都是在同一时间被选择的。因此,我需要找到一种方法,如何为每一个选择(如果一个被选中,rest应该取消选择),然后传递它的价值,例如。10,25等,而“选择”按钮被按下。

代码语言:javascript
复制
class RadiusSelectorDialog extends StatefulWidget {
  RadiusSelectorDialog({Key key}) : super(key: key);

  @override
  _RadiusSelectorDialogState createState() => _RadiusSelectorDialogState();
}

class _RadiusSelectorDialogState extends State<RadiusSelectorDialog>
    with TitleDescriptionTextMixin {
  bool selected = false;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      scrollable: true,
      contentPadding: EdgeInsets.all(4.0),
      title: Center(child: smallerBoldText('Select radius:', fontSize: 15.0)),
      content: Column(
        children: [
          Divider(
            color: Colors.grey,
            height: 2.0,
          ),
          CustomListTile(
            label: '10 km',
            selected: selected,
            onSelect: () => _onSelect(selected),
          ),
          CustomListTile(
            label: '25 km',
            selected: selected,
            onSelect: () => _onSelect(selected),
          ),
          CustomListTile(
            label: '50 km',
            selected: selected,
            onSelect: () => _onSelect(selected),
          ),
          CustomListTile(
            label: '75 km',
            selected: selected,
            onSelect: () => _onSelect(selected),
          ),
        ],
      ),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(16.0))),
      actions: [
        FlatButton(
            child: Text('Select',
                style: TextStyle(
                    color: LegionColors.primaryRedHigh,
                    fontWeight: FontWeight.bold)),
            onPressed: () {
              Navigator.pop(context);
            })
      ],
    );
  }

  void _onSelect(bool value) {
    setState(() {
      if (selected == false) {
        selected = true;
      } else {
        selected = false;
      }
    });
  }
}

用于选择的自定义listTile。

代码语言:javascript
复制
class CustomListTile extends StatelessWidget {
  final String label;
  final bool selected;
  final Function onSelect;

  const CustomListTile(
      {Key key,
      @required this.selected,
      @required this.onSelect,
      @required this.label})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ListTile(
          title: Center(
              child: Text(label,
                  style: selected == false
                      ? TextStyle(color: Colors.black)
                      : TextStyle(
                          color: LegionColors.primaryRedHigh,
                          fontWeight: FontWeight.bold,
                        ))),
          selected: selected,
          onTap: onSelect,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(16.0))),
        ),
        Divider(
          color: Colors.grey,
          height: 2.0,
        ),
      ],
    );
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-27 10:03:43

您正在使用相同的变量selected来标识是否为所有项选择了该项。下面是发生的事,

tapped

  • setState

  • One items是 -> selected -> true
  1. 所有的项目都更新为selected

为了避免它

您可以将标识符移动到StatefulWidget.

  • Or中并使其为,如果必须在父级中维护选择状态,则可以使用Map implementation。

代码语言:javascript
复制
  HashMap<String, bool> selectedState = new HashMap();

  void updateSelectionStatus(String selectedTileId, bool status) {
    setState(() {
      selectedState[selectedTileId] = status;
    });
  }

然后你可以像这样使用它

代码语言:javascript
复制
CustomListTile(
            label: '75 km',
            selected: selectedState['your-unique-id'],
            onSelect: () => updateSelectionStatus('your-unique-id', !selectedState['your-unique-id']),
          )
票数 2
EN

Stack Overflow用户

发布于 2021-01-27 10:03:32

查看官方颤振网站https://api.flutter.dev/flutter/material/ListTile/selected.html上的工作代码

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65916465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档