【问题标题】:How to replace text with image(is it possible in flutter)如何用图像替换文本(在颤振中是否可能)
【发布时间】:2021-09-03 01:21:46
【问题描述】:
var replaced = text.replaceAll(regExp, '*');

我们可以用实际图像代替星号标记吗..

【问题讨论】:

  • 你想如何替换我没有得到的图像?据我所知,您可以或者您愿意更改可能的路径或图像。请描述您的问题
  • 我已将一堆表情符号保存为本地图像,我想将消息上的特定文本替换为此图像表情符号。
  • 你不需要表情符号的图像,你可以使用 emoji unicodes
  • 是的,但我被明确告知要以这种方式这样做有什么运气..

标签: flutter dart flutter-web


【解决方案1】:

好的。因此,您在本地存储了一堆表情符号。你必须用表情符号替换文本。

我没有完美的答案。但是您如何才能实现我可以展示的路径。 使用地图。因为您假设要更改图像的文本,所以必须有一对。所以你可以为它定义一个地图。键是文本,值是表情符号的路径。将您的文件夹视为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

它们还会为您提供更好的性能。

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多