Flutter中添加特殊字符的实现方法——以替换(c)为版权符号为例
在Flutter代码中替换(c)为版权符号的几种方法
嘿,我来帮你搞定这个问题~这里有几种简单实用的方式,能轻松把文本里的(c)换成版权符号(©):
方法1:直接插入版权符号(最便捷)
你可以直接在字符串里输入Unicode版权符号©,或者用Unicode转义序列\u00A9(对应版权符号的十六进制编码)。修改后的代码如下:
Widget copyrightText = Container( padding: const EdgeInsets.only(left: 32.0, right: 32.0), // 直接输入版权符号 child: Text('2018 © Author\'s Name'), );
或者用转义序列的写法:
Widget copyrightText = Container( padding: const EdgeInsets.only(left: 32.0, right: 32.0), // 使用Unicode转义序列 child: Text('2018 \u00A9 Author\'s Name'), );
方法2:使用String.fromCharCode()生成符号
如果需要动态生成特殊字符,或者更清晰地展示字符编码,可以用String.fromCharCode()方法,传入版权符号的十六进制码0x00A9:
Widget copyrightText = Container( padding: const EdgeInsets.only(left: 32.0, right: 32.0), child: Text('2018 ${String.fromCharCode(0x00A9)} Author\'s Name'), );
方法3:批量替换字符串中的(c)
如果你的文本是从外部(比如后端接口、配置文件)获取的,里面包含多个(c)需要替换,可以用字符串的replaceAll()方法:
// 假设原始文本来自外部 String originalText = '2018 (c) Author\'s Name'; Widget copyrightText = Container( padding: const EdgeInsets.only(left: 32.0, right: 32.0), child: Text(originalText.replaceAll('(c)', '©')), );
这些方法都能完美实现你的需求,选最适合你场景的就好啦~
内容的提问来源于stack exchange,提问作者Maryna K.
相关产品推荐
相关产品推荐

