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

singleItem not working with multi-tags! #53

Closed Maclaon closed 4 years ago

Maclaon commented 4 years ago
          SelectTags(tags: _form),
          Divider(height: 1.0, indent: 0.0),
          SelectTags(tags: _type),
          Divider(height: 1.0, indent: 0.0),
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_tags/flutter_tags.dart';

class SelectTags extends StatefulWidget {
  final Map<String, String> tags;

  const SelectTags({Key key, this.tags}) : super(key: key);

  @override
  _SelectTagsState createState() => _SelectTagsState(tags);
}

class _SelectTagsState extends State<SelectTags> {
  final Map<String, String> tags;

  _SelectTagsState(this.tags);

  bool _symmetry = false;
  bool _startDirection = false;
  bool _horizontalScroll = true;
  int _column = 0;
  double _fontSize = 16;

  ItemTags _buildTagItem(Map<String,String> tags, index) {
    final item = tags.keys.elementAt(index);

    return ItemTags(
      key: Key(index.toString()),
      index: index,
      elevation: 0,
      title: item,
      active: false,
      singleItem: true,
      textColor: Colors.grey,
      textActiveColor: Colors.white,
      customData: item,
      color: Colors.white,
      activeColor: Colors.green[400],
      textScaleFactor: utf8.encode(item.substring(0, 1)).length > 2 ? 0.8 : 1,
      textStyle: TextStyle(fontSize: _fontSize),
      pressEnabled: true,
      onPressed: (_item) {

      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
          margin: EdgeInsets.all(5),
          child: Tags(
              symmetry: _symmetry,
              columns: _column,
              horizontalScroll: _horizontalScroll,
              verticalDirection: _startDirection
                  ? VerticalDirection.up
                  : VerticalDirection.down,
              textDirection:
                  _startDirection ? TextDirection.rtl : TextDirection.ltr,
              heightHorizontalScroll: 35 * (_fontSize / 14),
              itemCount: tags.entries.length,
              alignment: WrapAlignment.start,
              itemBuilder: (index) => _buildTagItem(tags, index)));
  }
}

when i click the tags in first SelectTags, it working, but not working in second SelectTags!

Maclaon commented 4 years ago

even i use two different widgets, it was still not working! it is weird!

Dn-a commented 4 years ago

hi @Maclaon, have you tried to define a key? example: Tags( key: Key( 'tags1'), ...) Tags( key: Key('tags2'), ...)

Maclaon commented 4 years ago

hi @Maclaon, have you tried to define a key? example: Tags( key: Key( 'tags1'), ...) Tags( key: Key('tags2'), ...)

Oh!, sorry, my problem, the two widgets has same key! i will give it a try~

Maclaon commented 4 years ago

@Dn-a hi, when set different key in Tags it was still not working, and the second Tags error when i click on it is follow:

The following NoSuchMethodError was thrown while handling a gesture:
The getter 'active' was called on null.
Receiver: null
Tried calling: active

When the exception was thrown, this was the stack
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:51:5)
#1      _ItemTagsState._singleItem.<anonymous closure> 
package:flutter_tags/src/item_tags.dart:438
#2      WhereIterator.moveNext  (dart:_internal/iterable.dart:442:13)
#3      WhereIterator.moveNext  (dart:_internal/iterable.dart:441:22)
#4      Iterable.forEach  (dart:core/iterable.dart:283:23)

when i scrolled the tags two the end, it works, but it still exists when i reentry that widget!

Maclaon commented 4 years ago

@Dn-a can u take a glance at this issue! i appreciate~

Dn-a commented 4 years ago

@Maclaon I have to do some tests

Maclaon commented 4 years ago

@Dn-a hi~ buddy, i am looking forward to your answer!

Maclaon commented 4 years ago

i found another lib for my app. Thank u!