英文:
Flutter Web shows emojis in black&white
问题
在iOS/Android应用中,表情符号显示正常。但在任何网络浏览器(例如Chrome)中,表情符号会显示为黑白。我也尝试了不同的字体族,但结果都一样。
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text('享受! 🥳 如果有任何问题')
),
);
}
}
英文:
In ios/android apps emojis are shown correctly. But using any web-browser (e.g. Chrome) the emoji appears in black and white. I also tried different Font-Families but with the same result.
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text('Enjoy! 🥳 If there\'s any question')
),
);
}
}
答案1
得分: 11
由于彩色表情字体的体积较大(约24MB),Flutter团队在Flutter 3.10.0中将彩色表情设为可选功能(参考链接)。
要启用它,请在您的index.html
文件中初始化引擎时将useColorEmoji
标志设置为true:
engineInitializer.initializeEngine({
// 其他配置...
useColorEmoji: true,
});
英文:
Due to the large size of the color emoji font (~24MB), the Flutter team made color emojis an opt-in feature in Flutter 3.10.0 (ref).
To enable it, set the useColorEmoji
flag to true when initializing the engine in your index.html
file:
engineInitializer.initializeEngine({
// other config...
useColorEmoji: true,
});
答案2
得分: 5
现在,存在与flutter-engine相关的问题。这可能是在此处引入的:链接
最初的问题似乎是flutter提供的字体不支持表情符号字符(.AppleSystemUIFont),因此会使用第一个回退字体NatoEmoji。
临时解决方案是将Apple Color Emoji字体文件提供给Web应用程序作为资源。你可以在这里找到该文件
"/System/Library/Fonts/Apple Color Emoji.ttc"
你可以将其复制到你的应用程序资产中。完成后,
...
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
universal_io: ^2.2.0
...
flutter:
...
fonts:
- family: "Apple Color Emoji"
fonts:
- asset: assets/fonts/apple_color_emoji.ttc
然后在main.dart中
import 'package:flutter/material.dart';
import 'package:universal_io/io.dart';
void main() async {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final fontFamilyFallback = <String>[];
if (Platform.isIOS || Platform.isMacOS) {
fontFamilyFallback.add("Apple Color Emoji");
}
return MaterialApp(
theme: ThemeData(fontFamilyFallback: fontFamilyFallback),
home: Scaffold(
appBar: AppBar(),
body: Center(
child: Builder(builder: (context) {
return const SelectableText(
'🙃',
style: TextStyle(
fontSize: 120,
),
);
}),
),
),
);
}
}
输出:
英文:
Right now, it is issue with the flutter-engine. It was probably introduced in here https://github.com/flutter/engine/commit/7406fd81865292772689a1bbbc2239c665ba07d8
The initial issue looks like the fonts provided in flutter does not have support for emoji characters (.AppleSystemUIFont) hence first fallback font is used that is NatoEmoji.
The temporary solution is to provide Apple Color Emoji font file to the web app as asset. You can find the file here
"/System/Library/Fonts/Apple Color Emoji.ttc"
You can copy this to your app's asset. Once that is done,
...
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
universal_io: ^2.2.0
...
flutter:
...
fonts:
- family: "Apple Color Emoji"
fonts:
- asset: assets/fonts/apple_color_emoji.ttc
Then inside main.dart
import 'package:flutter/material.dart';
import 'package:universal_io/io.dart';
void main() async {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final fontFamilyFallback = <String>[];
if (Platform.isIOS || Platform.isMacOS) {
fontFamilyFallback.add("Apple Color Emoji");
}
return MaterialApp(
theme: ThemeData(fontFamilyFallback: fontFamilyFallback),
home: Scaffold(
appBar: AppBar(),
body: Center(
child: Builder(builder: (context) {
return const SelectableText(
'😆',
style: TextStyle(
fontSize: 120,
),
);
}),
),
),
);
}
}
答案3
得分: 4
使用谷歌的Noto Color Emoji(按照以下步骤):
1. 下载字体 [下载 Noto Color Emoji][1]
[1]: https://fonts.google.com/noto/specimen/Noto+Color+Emoji
2. 将其添加到 pubspec.yaml 中 `flutter:
fonts:
- family: Noto Color Emoji
fonts:
- asset: assets/fonts/NotoColorEmoji-Regular.ttf`
3. 在 TextStyle 类中像这样使用它 `TextStyle(
fontFamilyFallbackm: [
'Apple Color Emoji',
'Noto Color Emoji',
],
)`
与此不同的是尝试:
. 删除 web 文件夹
. 使用命令 `flutter upgrade` 更新 flutter
. 运行命令 `flutter create .`
英文:
Use Noto Color Emoji from google (follow this steps):
- Download the font Download Noto Color Emoji
-
Added it to pubspec.yaml `flutter:
fonts:- family: Noto Color Emoji
fonts:- asset: assets/fonts/NotoColorEmoji-Regular.ttf`
- family: Noto Color Emoji
-
And in TextStyle class use it like this
TextStyle(
fontFamilyFallbackm: [
'Apple Color Emoji',
'Noto Color Emoji',
],
)
Unlike that try to:
. Delete web folder
. Update flutter by using command flutter upgrade
. Run command flutter create .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论