Flutter/Dart:在字符串中更改特定Unicode字符的颜色

huangapple go评论83阅读模式
英文:

Flutter/Dart: Changing colors of specific unicode characters in a string

问题

I'm new to Flutter, trying a code to change the color of specific Unicode characters in a string. Color coding \u0951, \u0952, and \u1cda to say blue, red, and green. The output string is not matching the expected format. I'm seeing some characters repeating and in some characters, the color is being applied to neighboring characters. Any help in figuring out the issue is greatly appreciated.

I want the code to work in both Android and IOS platforms.

Earlier I had a similar code in Kotlin when I developed this app in Android using spannableStr worked as expected.
The Flutter Dart code is:

import 'package:flutter/material.dart';

import 'package:flutter/painting.dart';
import 'package:google_fonts/google_fonts.dart';

class TextModifierScreen extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return TextModifierScreenState();
  }
}

class TextModifierScreenState extends State{

  List<TextSpan> _displaySpans = [];
  String inputString = "मृ॒त्यवे॒ यो॑ स्वाहा᳚ भू॒ मु॒भयो॒रा ॥";

  void replaceVowelsWithColor(String input) {
    List<TextSpan> spans = [];
    for (int i = 0; i < input.length; i++) {
      String char = input[i];
      int unicode = char.codeUnitAt(0);
      String unicodeString = '\\u${unicode.toRadixString(16).padLeft(4, '0')}';
      print('Unicode of $char is $unicodeString');
      if(char == '\u0952'){
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.red, ),
        ));
      }else if(char == '\u0951'){
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.blue),
        ));
      }else if(char == '\u1cda'){
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.green),
        ));
      }else{
        spans.add(TextSpan(
            text: char,
          style: TextStyle(color: Colors.black),
        ));
      }
    }
    setState(() {
      _displaySpans = spans;
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Accents Color Replacer'),
      ),
      body: Center(
        child: Padding(
          padding: EdgeInsets.only(left: 20, right: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RichText(
                text: TextSpan(
                  style: GoogleFonts.tiroDevanagariSanskrit(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                  children: <TextSpan>[
                    TextSpan(text: 'Original Text:\n '),
                    TextSpan(text: inputString),
                  ],
                ),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  replaceVowelsWithColor(inputString);
                },
                child: Text('Replace colors'),
              ),
              SizedBox(height: 20),
              RichText(
                text: TextSpan(
                  style: GoogleFonts.tiroDevanagariSanskrit(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                  children: <TextSpan>[
                    TextSpan(text: 'Modified Text: \n'),
                    ..._displaySpans,
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

The first image is what I'm seeing. The second image is the expected output.

Thanks in advance!!!

[Update] If I replace the code to replace colors of vowels in an English text, it seems to work properly.

void replaceVowelsWithColor(String input) {

    List<TextSpan> spans = [];
    for (int i = 0; i < input.length; i++) {
      String char = input[i];
      int unicode = char.codeUnitAt(0);
      String unicodeString = '\\u${unicode.toRadixString(16).padLeft(4, '0')}';
      print('Unicode of $char is $unicodeString');
      if(char == '\u0061'){
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.red, ),
        ));
      }else if(char == '\u0065'){
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.blue),
        ));
      }else if(char == '\u0069'){
        spans.add(TextSpan(
          text: char,
          //color: Colors.green,
          style: TextStyle(color: Colors.green),
        ));
      }else if(char == '\u006F'){
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.yellow),
        ));
      }else if(char == '\u0075'){
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.pink),
          ));
      }else{
        spans.add(TextSpan(
          text: char,
          style: TextStyle(color: Colors.black),
        ));
      }
    }
    setState(() {
      _displayEngSpans = spans;
    });
  }
英文:

I'm new to Flutter, trying a code to change the color of specific Unicode characters in a string. Color coding \u0951, \u0952, and \u1cda to say blue, red and green. The output string is not matching the expected format. I'm seeing some characters repeating and in some characters, the color is being applied to neighboring characters. Any help in figuring out the issue is greatly appreciated.
I want the code to work in both Android and IOS platforms.

Earlier I had a similar code in Kotlin when I developed this app in Android using spannableStr worked as expected.
The Flutter Dart code is:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter/painting.dart&#39;;
import &#39;package:google_fonts/google_fonts.dart&#39;;
class TextModifierScreen extends StatefulWidget{
@override
State&lt;StatefulWidget&gt; createState() {
return TextModifierScreenState();
}
}
class TextModifierScreenState extends State{
List&lt;TextSpan&gt; _displaySpans = [];
String inputString = &quot;मृ॒त्यवे॒ यो॑ स्वाहा᳚ भू॒ मु॒भयो॒रा ॥&quot;;
void replaceVowelsWithColor(String input) {
List&lt;TextSpan&gt; spans = [];
for (int i = 0; i &lt; input.length; i++) {
String char = input[i];
int unicode = char.codeUnitAt(0);
String unicodeString = &#39;\\u${unicode.toRadixString(16).padLeft(4, &#39;0&#39;)}&#39;;
print(&#39;Unicode of $char is $unicodeString&#39;);
if(char == &#39;\u0952&#39;){
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.red, ),
));
}else if(char == &#39;\u0951&#39;){
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.blue),
));
}else if(char == &#39;\u1cda&#39;){
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.green),
));
}else{
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.black),
));
}
}
setState(() {
_displaySpans = spans;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(&#39;Accents Color Replacer&#39;),
),
body: Center(
child: Padding(
padding: EdgeInsets.only(left: 20, right: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: &lt;Widget&gt;[
RichText(
text: TextSpan(
style: GoogleFonts.tiroDevanagariSanskrit(
fontSize: 20,
color: Colors.black,
),
children: &lt;TextSpan&gt;[
TextSpan(text: &#39;Original Text:\n &#39;),
TextSpan(text: inputString),
],
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
replaceVowelsWithColor(inputString);
},
child: Text(&#39;Replace colors&#39;),
),
SizedBox(height: 20),
RichText(
text: TextSpan(
style: GoogleFonts.tiroDevanagariSanskrit(
fontSize: 20,
color: Colors.black,
),
children: &lt;TextSpan&gt;[
TextSpan(text: &#39;Modifed Text: \n&#39;),
..._displaySpans,
//_displaySpans[0],_displaySpans[2],_displaySpans[9],_displaySpans[2]
],
),
),
],
),
),
),
);
}
}

The first image is what I'm seeing. The second image is the expected output.
Flutter/Dart:在字符串中更改特定Unicode字符的颜色
Flutter/Dart:在字符串中更改特定Unicode字符的颜色

Thanks in advance!!!

[Update] If I replace the code to replace colors of vowels in an English text, it seems to work properly.
Flutter/Dart:在字符串中更改特定Unicode字符的颜色

void replaceVowelsWithColor(String input) {
List&lt;TextSpan&gt; spans = [];
for (int i = 0; i &lt; input.length; i++) {
String char = input[i];
int unicode = char.codeUnitAt(0);
String unicodeString = &#39;\\u${unicode.toRadixString(16).padLeft(4, &#39;0&#39;)}&#39;;
print(&#39;Unicode of $char is $unicodeString&#39;);
if(char == &#39;\u0061&#39;){
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.red, ),
));
}else if(char == &#39;\u0065&#39;){
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.blue),
));
}else if(char == &#39;\u0069&#39;){
spans.add(TextSpan(
text: char,
//color: Colors.green,
style: TextStyle(color: Colors.green),
));
}else if(char == &#39;\u006F&#39;){
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.yellow),
));
}else if(char == &#39;\u0075&#39;){
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.pink),
));
}else{
spans.add(TextSpan(
text: char,
style: TextStyle(color: Colors.black),
));
}
}
setState(() {
_displayEngSpans = spans;
});
}

答案1

得分: 2

已解决该问题。问题出在使用的特定字体上。以下是使用不同的Google Devanagari字体输出的维迪克·德瓦纳加里文字的结果。根据当前版本,Noto Serif Devanagari和Pragati Narrow似乎能够呈现所需的维迪克重音符号。Tiro字体在更改颜色时对某些特殊字符存在问题。

Flutter/Dart:在字符串中更改特定Unicode字符的颜色

英文:

Resolved the issue. The issue was with the specific font being used. Here are the outputs of Vedic Devanagari script using different Devanagari Google fonts.
As per current versions, the Noto Serif Devanagari and Pragati Narrow looks to render the required Vedic accents. The Tiro font has an issue with some special characters if we change colors.

Flutter/Dart:在字符串中更改特定Unicode字符的颜色

huangapple
  • 本文由 发表于 2023年8月5日 14:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定