首页 > 解决方案 > 在 Flutter 中显示国旗字符

问题描述

这个答案有一些代码可以将语言环境转换为 Java 中的国家/地区表情符号。我尝试在 Dart 中实现它,但没有成功。

我尝试将上面的代码转换为 Dart

  void _emoji() {
    int flagOffset = 0x1F1E6;
    int asciiOffset = 0x41;

    String country = "US";

    int firstChar = country.codeUnitAt(0) - asciiOffset + flagOffset;
    int secondChar = country.codeUnitAt(1) - asciiOffset + flagOffset;

    String emoji =
        String.fromCharCode(firstChar) + String.fromCharCode(secondChar);
    print(emoji);
  }

“美国”语言环境应输出“”

标签: flutterdart

解决方案


您发布的代码工作正常,即print(emoji)成功打印 .

我认为您遇到的真正问题是 FlutterText小部件显示如下:

这是美国国旗,但是,我必须同意,当您在设备上看到它时,它看起来不像它,因为字体非常小,而且国旗的分辨率相当高。

您将需要使用自定义字体并使用以下方法将其应用于您的Text小部件:

Text(emoji,
  style: TextStyle(
    fontFamily: '...',
  ),
)

否则,转换和显示标志都可以正常工作。我相信它们看起来与您预期的不同。


推荐阅读