Flutter标签视图插件tag_view的使用
Flutter标签视图插件tag_view的使用
开始使用
添加依赖
你可以通过以下命令将 tag_view
添加为依赖项(使用最新稳定版本):
$ dart pub add tag_view
或者,你可以在 pubspec.yaml
文件的依赖部分手动添加 tag_view
:
dependencies:
tag_view: ^replace-with-latest-version
简单使用
import 'package:tag_view/tag_view.dart';
List<String> tags = ['标签 1', '标签 2'];
TagView(
tags,
isEnableDelete: true,
tagBackgroundColor: Colors.blue,
tagTextColor: Colors.white,
margin: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
onDelete: (deletePos) {
setState(() {
tags.removeAt(deletePos);
});
},
onClick: (clickPos) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(tags[clickPos].toString())));
},
)
使用示例
以下是完整的示例代码:
import 'package:flutter/material.dart';
import 'package:tag_view/tag_view.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: '标签视图',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({super.key});
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late TextEditingController _controller;
late List<String> _tags;
String _validateError = '';
[@override](/user/override)
void initState() {
super.initState();
_controller = TextEditingController();
_tags = [];
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).primaryColor,
title: Text('标签视图', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
),
body: Container(
margin: EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
TextField(
controller: _controller,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
hintText: '输入标签',
errorText: _validateError.length > 0 ? _validateError : null,
),
onSubmitted: (value) {
setState(() {
var s = value.replaceAll(" ", "");
if (value.length == 0 || s.length == 0) {
_validateError = '请输入标签';
_controller.text = '';
} else if (_tags.contains(value)) {
_validateError = '标签已存在!';
} else {
_validateError = '';
_tags.add(value);
_controller.clear();
}
});
},
),
SizedBox(height: 20),
TagView(
_tags,
isEnableDelete: true,
tagBackgroundColor: Colors.blue,
tagTextColor: Colors.white,
margin: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
onDelete: (deletePos) {
setState(() {
_tags.removeAt(deletePos);
});
},
onClick: (clickPos) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(_tags[clickPos].toString())));
},
),
SizedBox(height: 20),
InkWell(
onTap: () {
setState(() {
_tags.clear();
});
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Text('清除标签',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold)),
),
)
],
),
),
);
}
}
更多关于Flutter标签视图插件tag_view的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter标签视图插件tag_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter中使用tag_view
插件的示例代码。tag_view
插件通常用于显示和管理标签(Tag),允许用户选择、添加或删除标签。
首先,确保在你的pubspec.yaml
文件中添加tag_view
依赖项:
dependencies:
flutter:
sdk: flutter
tag_view: ^x.y.z # 请将x.y.z替换为最新版本号
然后运行flutter pub get
来获取依赖项。
接下来,在你的Flutter项目中创建一个示例页面来使用TagView
。以下是一个完整的示例代码:
import 'package:flutter/material.dart';
import 'package:tag_view/tag_view.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter TagView Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: TagViewExample(),
);
}
}
class TagViewExample extends StatefulWidget {
@override
_TagViewExampleState createState() => _TagViewExampleState();
}
class _TagViewExampleState extends State<TagViewExample> {
List<String> selectedTags = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TagView Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Selected Tags:',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
SizedBox(height: 8),
Wrap(
spacing: 4,
runSpacing: 4,
children: List.generate(
selectedTags.length,
(index) => Chip(
label: Text(selectedTags[index]),
backgroundColor: Colors.blue.shade300,
onDeleted: () {
setState(() {
selectedTags.removeAt(index);
});
},
),
),
),
SizedBox(height: 24),
Text(
'Add Tags:',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
SizedBox(height: 8),
TagView(
initialTags: [],
onTagAdded: (tag) {
setState(() {
selectedTags.add(tag);
});
},
onTagRemoved: (tag) {
setState(() {
selectedTags.remove(tag);
});
},
suggestions: List.generate(
20,
(index) => 'Tag $index',
),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter a tag',
suffixIcon: Icon(Icons.add),
),
),
],
),
),
);
}
}
代码解释
- 依赖项:在
pubspec.yaml
中添加tag_view
依赖项。 - 主应用:
MyApp
类定义了应用程序的入口点,设置了主题和主页面。 - 示例页面:
TagViewExample
是一个有状态的Widget,用于管理标签的显示和选择。 - 状态管理:
_TagViewExampleState
类中有一个selectedTags
列表来存储选中的标签。 - UI布局:
- 使用
Column
布局来组织内容。 - 显示已选中的标签,使用
Chip
组件并允许删除。 - 使用
TagView
组件来添加和删除标签,并提供标签建议。
- 使用
运行这段代码后,你将看到一个简单的界面,允许你添加和删除标签,并显示已选中的标签。
注意:tag_view
插件的具体API可能会随着版本更新而变化,请查阅最新的官方文档以确保兼容性和正确使用。