如何在初始化页面时以及在 Flutter 中的 singleItem = true 时 select 默认项(itemTag)?

How to select a default item (itemTag) when initializing the page and when singleItem = true in Flutter?

我想 select 默认情况下,我使用 ItemTags 与 singleItem: true 为所有和 active:true 只为一个项目。但它没有 select 项。也许是因为 singleItem 是真的??!我尝试使用 singleItem: false 它有效,但对于我的项目,我需要单选,该怎么做? 谢谢。

我的 ItemTags 就像...

 ItemTags(
     key: GlobalKey(debugLabel: index.toString()),
     index: index, // required
     title: TranslationDictionary.instance[item.name],
     customData: item,
     singleItem: true,
     active: _active, // even it's true, it doesn't select item (because singleItem is true?? but why??)
     elevation: 0,
     borderRadius: BorderRadius.circular(8),            
     onPressed: (item) {
       setState(() {
          if (item.active) {
            ...
          } else {
            ...
          });
      },
 );

我自己试过了,它确实适用于这个:

return ItemTags(
        // Each ItemTags must contain a Key. Keys allow Flutter to
        // uniquely identify widgets.
        key: Key(index.toString()),
        index: index, // required
        title: item,
        active: false,
        singleItem: true,
        customData: index,
        textStyle: TextStyle(
          fontSize: _fontSize,
        ),
        combine: ItemTagsCombine.withTextBefore,
        icon: ItemTagsIcon(
          icon: Icons.add,
        ), // OR null, // OR null,
      );

itemBuilder 的 Tags 属性是一个循环,所以 active 属性 应该设置为 false, 并将 singleItem 设置为 true 以避免多选。

EDIT: 看来如果你设置了 singleItem 为真,你就不能启动一个项目来激活,请参阅下面的包装。

active: widget.singleItem ? false : widget.active,

这意味着如果您将 singleItem 设置为 true,它不关心您的活动 属性。所以你不能这样做。

对我来说,有两种解决方案:

解决方案 1

你自己修改包,见:https://medium.com/flutter-community/how-to-modify-an-existing-pub-package-to-use-in-your-flutter-project-4e909452ee66

解决方案 2

模拟点击屏幕来选择必须选择的正确项目