Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

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

I want to select an item by default, I use ItemTags with singleItem: true for all and active:true just for one item. But it doesn't select item. Maybe because singleItem is true??! I tried with singleItem: false it worked, but for my project I need SINGLE SELECTION, how to do that? Thank you.

My ItemTags is like...

 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 {
            ...
          });
      },
 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tried myself and it definitely works with this:

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's Tags attribute is a loop so the active property should set to false, and singleItem to true to avoid multiple selections.

EDIT: It seems that if you set up with singleItem to true, you can't initiate with an item to active, see below in their package.

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

It means that it doesn't care about your active property if you set the singleItem to true. So you can't do this.

For me, there are 2 solutions:

Solution 1

You modify by yourself the package, see : https://medium.com/flutter-community/how-to-modify-an-existing-pub-package-to-use-in-your-flutter-project-4e909452ee66

Solution 2

Simulate a tap on the screen to choose the right item which has to be selected


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...