好的。因此,您在本地存储了一堆表情符号。你必须用表情符号替换文本。
我没有完美的答案。但是您如何才能实现我可以展示的路径。
使用地图。因为您假设要更改图像的文本,所以必须有一对。所以你可以为它定义一个地图。键是文本,值是表情符号的路径。将您的文件夹视为images,格式为png。
Map<String, String> myData = {"like" : "images/like.png", "time":
"images/time.png"};
等等。像这样,您可以创建它的完整地图。如果限制为 30 -40 条记录,您将手动执行。
现在您可以对其执行操作。假设你有 String value = "I like your shirt!";
所以我们需要扫描字符串,然后在 tha 映射中搜索每个单词,如果找到则替换。
代码是这样的
void main() {
//your value that you are going to scan
String str = " I like your shirt";
//Defining the map
Map<String, String> data = {"like" : "images/like.png", "shirt" :
"images/shirt.png", "happy" : "images/happy.png"};
//function that will do the changes and display
void replaceTextWithEmojis(String value, Map<String, String> data){
//splitting the value into the array or list of items so we can iterate easily
List<String> inputvalues = str.split(" ");
//looping through the text
for (int i =0; i<inputvalues.length; i++){
//checking where that text is present in the emojis map or not.
if(data.containsKey(inputvalues[i])){
//this will replace the text with the path for that key
inputvalues[i] = data[inputvalues[i]];
}
} //end of the for loop
print(inputvalues);
}
replaceTextWithEmojis(str, data);
}
这将替换为所需的值。但是您想在文本之间显示图像。那是什么困难的事情。我不认为你可以在颤抖中做到这一点。或者就像您需要识别可替换文本的位置,然后断开字符串并将其存储在其他变量中。在断点处附加图像。所以在我们用地图替换的地方你可以添加widget(child ImageProvider(inputvalue[i]));
我建议您将 unicode 用于表情符号。
您可以将包用于 unicode emjois:https://pub.dev/packages/emojis
它们还会为您提供更好的性能。