Created
December 26, 2019 10:13
-
-
Save 5lineofcode/48d1f65c0b13eb9f7420b2ca65e95df1 to your computer and use it in GitHub Desktop.
GetColorizedString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Widget> getColorizedString(String str) { | |
Map<String, dynamic> stringCode = { | |
"red.": Colors.red, | |
"blue.": Colors.blue, | |
"yellow.": Colors.yellow, | |
"green.": Colors.green, | |
"r.": Colors.red, | |
"b.": Colors.blue, | |
"y.": Colors.yellow, | |
"g.": Colors.green, | |
"o.": Colors.orange, | |
}; | |
List<Widget> widgetList = []; | |
var words = str.split(" "); | |
words.forEach((word) { | |
var color = Colors.grey[900]; | |
stringCode.forEach((key, value) { | |
if (word.contains(key)) { | |
color = value; | |
} | |
}); | |
widgetList.add(Text( | |
"$word ", | |
style: TextStyle( | |
color: color, | |
fontSize: 20.0, | |
), | |
)); | |
}); | |
return widgetList; | |
} | |
/* | |
Implementation | |
Use Wrap: | |
*/ | |
Wrap( | |
children: getColorizedString( | |
"Hello blue.Telegram , red.apa red.kabar?\n g.Gimana y.nih?\n o.Tahun o.baruan o.gak?", | |
), | |
), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment