Dn-a / flutter_tags

With flutter tags you can create selectable or input tags that automatically adapt to the screen width
https://pub.dartlang.org/packages/flutter_tags
MIT License
508 stars 124 forks source link

The truth about "active" tag #46

Closed Canada2000 closed 4 years ago

Canada2000 commented 4 years ago

Hi @Dn-a Great work with the new ^0.4.7 flutter_tags version !!! It seems that the active property is not working when we need to activate a specific tag programatically. For example : active: (index == 1) ? true : false, does not activate the 1st Tag only.

Here is a short example that works with no errors... I was expecting that one of the tags is already activated when the app starts... but none is activated until we select one... how can we specify the selected/active tag in the code?

----Complete Code using: flutter_tags: "^0.4.7":-------

import 'package:flutter/material.dart';
import 'package:flutter_tags/flutter_tags.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyTags(),
        ),),);}
}

class MyTags extends StatefulWidget {
  @override
   MyTagsState createState() => MyTagsState();
}

class MyTagsState extends State<MyTags> {
  List<String> _myTagsList=[];

  @override
  void initState() {
    super.initState();
    _myTagsList.addAll(["Tag1", "Tag2", "Tag3", "Tag4"]);
  }

  @override
  Widget build(BuildContext context) {
    return _myTags();
  }

  Tags _myTags() {
    return Tags(
      itemCount: _myTagsList.length,
      itemBuilder: (int index) {
        return ItemTags(
          key: Key(index.toString()),
          index: index,
          title: _myTagsList[index].toString(),
          singleItem: true,
          active: (index == 1) ? true : false,
        );},);
  }
}
Dn-a commented 4 years ago

@canada4439 when starting the app with singleItem: true, all tags will have active: false. it is a use case that I had not considered but that I will implement in later versions. Temporarily to solve the problem, when starting the app, you could set singleItem: false, set active: true where you want and set singleItem: true when the boot is complete.

Canada2000 commented 4 years ago

@Dn-a thanks a lot Antonino... I didnt know that singleItem:true turns off active:. Would be useful to adjust as very often we need to "activate" a tag in code... for example when longpress currently it does not activate... so need to take care in code.

I will close the issue... then I will try your suggestion... I will let you know if any issue... Again, great work !!! ;0)

songxing10000 commented 4 years ago

same question