flutter_tags
使用 flutter tags,您可以创建可选或可输入的标签,它们可以自动适应屏幕宽度
快速轻松地创建精美的标签。
安装
将此添加到您的 package 的 pubspec.yaml 文件中
dependencies:
flutter_tags: "^0.4.9+1"
演示
|
|
简单用法
import 'package:flutter_tags/flutter_tags.dart';
.
.
.
List _items;
double _fontSize = 14;
@override
void initState(){
super.initState();
// if you store data on a local database (sqflite), then you could do something like this
Model().getItems().then((items){
_items = items;
});
}
@override
Widget build(BuildContext context) {
return Tags(
key:_tagStateKey,
textField: TagsTextField(
textStyle: TextStyle(fontSize: _fontSize),
constraintSuggestion: true, suggestions: [],
//width: double.infinity, padding: EdgeInsets.symmetric(horizontal: 10),
onSubmitted: (String str) {
// Add item to the data source.
setState(() {
// required
_items.add(str);
});
},
),
itemCount: _items.length, // required
itemBuilder: (int index){
final item = _items[index];
return ItemTags(
// Each ItemTags must contain a Key. Keys allow Flutter to
// uniquely identify widgets.
key: Key(index.toString()),
index: index, // required
title: item.title,
active: item.active,
customData: item.customData,
textStyle: TextStyle( fontSize: _fontSize, ),
combine: ItemTagsCombine.withTextBefore,
image: ItemTagsImage(
image: AssetImage("img.jpg") // OR NetworkImage("https://...image.png")
), // OR null,
icon: ItemTagsIcon(
icon: Icons.add,
), // OR null,
removeButton: ItemTagsRemoveButton(
onRemoved: (){
// Remove the item from the data source.
setState(() {
// required
_items.removeAt(index);
});
//required
return true;
},
), // OR null,
onPressed: (item) => print(item),
onLongPressed: (item) => print(item),
);
},
);
}
final GlobalKey<TagsState> _tagStateKey = GlobalKey<TagsState>();
// Allows you to get a list of all the ItemTags
_getAllItem(){
List<Item> lst = _tagStateKey.currentState?.getAllItem;
if(lst!=null)
lst.where((a) => a.active==true).forEach( ( a) => print(a.title));
}
包裹的小部件示例
您可以自由地将 ItemTags () 包装在另一个小部件中
Tags(
itemCount: items.length,
itemBuilder: (int index){
return Tooltip(
message: item.title,
child:ItemTags(
title:item.title,
)
);
},
);
Tags() 参数
| 属性名 | 描述 | 默认值 |
|---|---|---|
列数 |
在必要时设置列数 | 空 |
项目数 |
显示的标签数量 | 必需 |
对称性 |
能够水平查看和滚动标签 | 假 |
horizontalScroll |
偏移抽屉宽度 | 0.4 |
heightHorizontalScroll |
HorizontalScroll 的高度,用于正确显示标签 | 60 |
spacing |
标签之间的水平间距 | 6 |
runSpacing |
标签之间的垂直间距 | 14 |
alignment |
水平包装对齐 | WrapAlignment.center |
runAlignment |
垂直包装对齐 | WrapAlignment.center |
direction |
ItemTags 的方向 | Axis.horizontal |
verticalDirection |
从下到上或从上到下迭代 Item | VerticalDirection.down |
textDirection |
ItemTags 的文本方向 | TextDirection.ltr |
itemBuilder |
标签生成器 | |
textField |
添加文本字段 | TagsTextFiled() |
ItemTags() 参数
index- 必需title- 必需textScaleFactor- 自定义文本缩放因子active- 布尔值 (默认为 true)pressEnabled- 启用按下标签 (默认为 true)customData- 可以将任何自定义值添加到 customData 字段,稍后检索。一个很好的例子:存储 Firestore 文档的 ID。textStyle- textStyle()alignment- MainAxisAlignment (默认为 MainAxisAlignment.center)combine- * 以不同的方式组合文本、图标、图像的能力 (默认为 ItemTagsCombine.imageOrIconOrText)*icon- ItemTagsIcon()image- ItemTagsImage()removeButton- ItemTagsRemoveButton()borderRadius- BorderRadiusborder- 自定义边框侧padding- 默认 EdgeInsets.symmetric(horizontal: 7, vertical: 5)elevation- 默认 5singleItem- 默认 falsetextOverflow- 默认 TextOverflow.fadetextColor- 默认 Colors.blacktextActiveColor- 默认 Colors.whitecolor- 默认 Colors.whiteactiveColor- 默认 Colors.blueGreyhighlightColor-splashColor-colorShowDuplicate- 默认 Colors.redonPressed- 回调onLongPressed- 回调onRemoved- 回调