Flutter表情符号插件awesome_emojis的使用
Flutter表情符号插件awesome_emojis的使用
此包基于emojis源代码,并进行了一些改进,主要是迁移到了null安全。
🔥 Dart表情符号包 🔥 包含超过3300个表情符号 这个包包含所有最新的Unicode 13 表情符号(2020年)。
使用方法
0️⃣ 导入
import 'package:emojis/emojis.dart'; // 用于使用Emoji集合
import 'package:emojis/emoji.dart'; // 用于使用Emoji工具
1️⃣ 使用
void main() {
print('I ${Emojis.greenHeart} ${Emojis.directHit}'); // 打印 "I 💚 🎯"
// 通过名称获取一个表情符号
Emoji smile = Emoji.byName('Grinning Face');
print('Emoji name : ${smile.name}'); // 打印 "Emoji name is Grinning Face"
print('Emoji character : ${smile.char}'); // 打印 "Emoji character is '😀'"
print('Emoji category : ${smile.emojiGroup}'); // 打印 "EmojiGroup.smileysEmotion 组"
print('Emoji sub-group : ${smile.emojiSubgroup}'); // 打印 "EmojiSubgroup.faceSmiling 子组"
// 通过字符获取表情符号
Emoji womanBlond = Emoji.byChar(Emojis.womanBlondHair);
print(womanBlond); // 打印 "👱♀️"
// 更改肤色
Emoji blondyBlackLady = womanBlond.newSkin(fitzpatrick.dark);
print(blondyBlackLady); // 打印 "👱🏿♀️"
// 获取所有表情符号列表
List<Emoji> emList = Emoji.all();
print(emList.length); // 打印 "所有表情符号的数量"
// 拆分表情符号
List<String> disassembled = Emoji.disassemble(Emojis.mechanic);
print(disassembled); // 打印 "['🔧', '🧑']"
// 组合多个表情符号
String assembled = Emoji.assemble([Emojis.man, Emojis.man, Emojis.girl, Emojis.boy]);
print(assembled); // 打印 "👨👨👧👦️"
// 修改表情符号的肤色
String blackThumbsUp = '👍';
String whiteThumbsUp = Emoji.modify(blackThumbsUp, fitzpatrick.light);
print(whiteThumbsUp); // 打印 "👍🏻"
// 将带棕色皮肤的女性警察转换为普通女性警察
String femaleCop = Emojis.womanPoliceOfficerMediumDarkSkinTone;
String newFemaleCop = Emoji.stabilize(femaleCop);
print('$femaleCop => $newFemaleCop'); // 打印 "👮🏾♀️ => 👮♀️"
// 性别中性化
String aCop = Emoji.stabilize(femaleCop, skin: false, gender: true);
print('$femaleCop => $aCop'); // 打印 "👮🏾♀️ => 👮🏾"
// 通过关键词查找表情符号
final loveEmojis = Emoji.byKeyword('love');
print(loveEmojis);
// 打印 "(🥰, 😍, 😘, 😚, 😙, 🤗, 😻, 😽, 💋, 💌, 💘, 💝, 💖, 💗, 💓, 💞, 💕, ..., 💄, ♾)"
// 通过类别查找表情符号
final foodCategory = Emoji.byGroup(EmojiGroup.foodDrink);
print(foodCategory);
// 打印 "(🍇, 🍈, 🍉, 🍊, 🍋, 🍌, 🍍, 🥭, 🍎, 🍏, 🍐, 🍑, 🍒, 🍓, 🥝, 🍅, 🥥, 🥑, ...)"
// 通过子类别查找表情符号
Iterable<Emoji> moneySubgroupEmojis = Emoji.bySubgroup(EmojiSubgroup.money);
print(moneySubgroupEmojis);
// 打印 "(💰, 💴, 💵, 💶, 💷, 💸, 💳, 🧾, 💹)"
}
更多关于Flutter表情符号插件awesome_emojis的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter表情符号插件awesome_emojis的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用awesome_emojis
插件的示例代码。这个插件允许你在Flutter应用中轻松集成和使用表情符号。
1. 添加依赖
首先,你需要在你的pubspec.yaml
文件中添加awesome_emojis
的依赖。确保你的Flutter环境已经设置好,并且你正在使用的是一个有效的Flutter项目。
dependencies:
flutter:
sdk: flutter
awesome_emojis: ^0.0.4 # 请检查最新版本号
然后运行flutter pub get
来安装依赖。
2. 导入包
在你的Dart文件中(例如main.dart
),导入awesome_emojis
包。
import 'package:flutter/material.dart';
import 'package:awesome_emojis/awesome_emojis.dart';
3. 使用EmojiPicker
接下来,你可以在你的UI中使用EmojiPicker
小部件来显示表情符号选择器。以下是一个简单的示例,展示了如何将EmojiPicker
集成到一个基本的Flutter应用中。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Emoji Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: EmojiScreen(),
);
}
}
class EmojiScreen extends StatefulWidget {
@override
_EmojiScreenState createState() => _EmojiScreenState();
}
class _EmojiScreenState extends State<EmojiScreen> {
String selectedEmoji = '';
void _onEmojiSelected(String emoji) {
setState(() {
selectedEmoji = emoji;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Emoji Picker Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Selected Emoji: $selectedEmoji',
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20),
EmojiPicker(
onEmojiSelected: _onEmojiSelected,
numRecommended: 28, // 推荐表情的数量
icon: Icon(Icons.emoji_emotions_outlined),
recommendations: const [
'❤️', '😍', '😂', '😎', '😢', '😠', '🤔', '😴',
// 添加更多推荐表情
],
),
],
),
),
);
}
}
4. 运行应用
确保你的开发环境已经设置好,然后运行你的Flutter应用。你应该会看到一个简单的界面,上面有一个表情符号选择器,当你选择一个表情符号时,它会在界面上显示。
flutter run
注意事项
- 确保你使用的是最新版本的
awesome_emojis
插件,因为插件的API可能会随着版本更新而变化。 - 你可以根据需要自定义
EmojiPicker
的参数,例如更改推荐表情、图标等。 - 如果你遇到任何问题,请查阅
awesome_emojis
的官方文档或在其GitHub存储库中查找相关信息。
这样,你就成功地在Flutter应用中集成了awesome_emojis
插件,并能够使用表情符号选择器了。