Flutter标签输入插件flutter_chips_input_sunny的使用
Flutter标签输入插件flutter_chips_input_sunny的使用
使用说明
安装
请按照以下步骤安装flutter_chips_input_sunny
插件:
dependencies:
flutter_chips_input_sunny: ^版本号
在您的pubspec.yaml
文件中添加上述依赖项,并运行flutter pub get
以获取新的依赖项。
导入
在您的Dart文件中导入flutter_chips_input_sunny
库:
import 'package:flutter_chips_input_sunny/flutter_chips_input.dart';
示例代码
ChipsInput
以下是一个简单的示例代码,展示了如何使用ChipsInput
组件:
import 'package:flutter/material.dart';
import 'package:flutter_chips_input_sunny/flutter_chips_input.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Chips Input',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ChipsInputController<AppProfile> controller = ChipsInputController<AppProfile>(
findSuggestions: _findSuggestions,
);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Chips Input Example'),
),
body: Padding(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChipsInput<AppProfile>(
controller: controller,
placeholder: "Search contacts",
maxChips: 5,
decoration: InputDecoration(
labelText: "Select People",
),
chipBuilder: (context, _, profile) {
return InputChip(
key: ObjectKey(profile),
label: Text(profile.name),
avatar: CircleAvatar(
backgroundImage: NetworkImage(profile.imageUrl),
),
onDeleted: () => controller.deleteChip(profile),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
);
},
suggestionBuilder: (context, _, profile) {
return ListTile(
key: ObjectKey(profile),
leading: CircleAvatar(
backgroundImage: NetworkImage(profile.imageUrl),
),
title: Text(profile.name),
subtitle: Text(profile.email),
onTap: () => controller.addChip(profile),
);
},
),
],
),
),
);
}
ChipSuggestions<AppProfile> _findSuggestions(String query) {
if (query.isNotEmpty) {
var lowercaseQuery = query.toLowerCase();
var foundResults = mockResults.where(
(profile) {
return profile.name.toLowerCase().contains(lowercaseQuery) ||
profile.email.toLowerCase().contains(lowercaseQuery);
},
).toList(growable: false)
..sort((a, b) => a.name.toLowerCase().indexOf(lowercaseQuery).compareTo(b.name.toLowerCase().indexOf(lowercaseQuery)));
var exactMatch = mockResults.firstWhere(
(profile) => profile.name.toLowerCase() == lowercaseQuery || profile.email.toLowerCase() == lowercaseQuery,
orElse: () => null,
);
return ChipSuggestions<AppProfile>(
suggestions: foundResults,
match: foundResults.length == 1 && exactMatch != null
? Suggestion.highlighted(item: exactMatch, highlightText: exactMatch.name)
: Suggestion.empty());
} else {
return const ChipSuggestions.empty();
}
}
}
class AppProfile {
final String name;
final String email;
final String imageUrl;
const AppProfile(this.name, this.email, this.imageUrl);
[@override](/user/override)
bool operator ==(Object other) =>
identical(this, other) || other is AppProfile && runtimeType == other.runtimeType && name == other.name;
[@override](/user/override)
int get hashCode => name.hashCode;
[@override](/user/override)
String toString() {
return name;
}
}
const mockResults = [
AppProfile('John Doe', 'jdoe@flutter.io', 'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
AppProfile('Paul', 'paul@google.com', 'https://mbtskoudsalg.com/images/person-stock-image-png.png'),
AppProfile('Fred', 'fred@google.com', 'https://media.istockphoto.com/photos/feeling-great-about-my-corporate-choices-picture-id507296326'),
AppProfile('Brian', 'brian@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('John', 'john@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Thomas', 'thomas@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Nelly', 'nelly@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Marie', 'marie@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Charlie', 'charlie@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Diana', 'diana@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Ernie', 'ernie@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Gina', 'fred@flutter.io', 'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
];
更多关于Flutter标签输入插件flutter_chips_input_sunny的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter标签输入插件flutter_chips_input_sunny的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_chips_input_sunny
是一个用于在 Flutter 应用中实现标签输入功能的插件。它允许用户输入多个标签(chips),并且可以自定义标签的样式和行为。以下是如何使用 flutter_chips_input_sunny
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 flutter_chips_input_sunny
插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_chips_input_sunny: ^1.0.0 # 请确保使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入插件:
import 'package:flutter_chips_input_sunny/flutter_chips_input.dart';
3. 使用 ChipsInput
组件
ChipsInput
是插件提供的主要组件。你可以通过以下方式使用它:
class MyChipsInputPage extends StatefulWidget {
[@override](/user/override)
_MyChipsInputPageState createState() => _MyChipsInputPageState();
}
class _MyChipsInputPageState extends State<MyChipsInputPage> {
final List<String> _chips = [];
final TextEditingController _controller = TextEditingController();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Chips Input Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
ChipsInput(
controller: _controller,
initialValue: _chips,
onChanged: (chips) {
setState(() {
_chips.clear();
_chips.addAll(chips);
});
},
decoration: InputDecoration(
labelText: 'Enter tags',
border: OutlineInputBorder(),
),
maxChips: 5, // 最大标签数量
findSuggestions: (String query) {
// 这里可以根据输入的内容返回建议的标签列表
if (query.isNotEmpty) {
return ['Flutter', 'Dart', 'Widget', 'State', 'Provider']
.where((tag) => tag.toLowerCase().contains(query.toLowerCase()))
.toList();
}
return [];
},
chipBuilder: (context, chip) {
return Chip(
label: Text(chip),
onDeleted: () {
setState(() {
_chips.remove(chip);
});
},
);
},
suggestionBuilder: (context, suggestion) {
return ListTile(
title: Text(suggestion),
onTap: () {
setState(() {
_chips.add(suggestion);
_controller.clear();
});
},
);
},
),
SizedBox(height: 20),
Text('Selected Chips: ${_chips.join(', ')}'),
],
),
),
);
}
}