在 Flutter 中显示国旗字符

jul*_*loz 8 dart 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);
  }

Run Code Online (Sandbox Code Playgroud)

“US”区域设置应输出“”

cre*_*not 5

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

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

这是美国国旗,但是,我不得不承认,当你在设备上看到它时,它看起来并不像它,因为字体非常小,而且国旗的分辨率相当高。

您将需要使用自定义字体Text并使用以下命令将其应用到您的小部件:

Text(emoji,
  style: TextStyle(
    fontFamily: '...',
  ),
)
Run Code Online (Sandbox Code Playgroud)

否则,转换和显示标志都可以正常工作。我相信它们只是看起来与你想象的不同。