Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a few words in different colors in Flutter

Tags:

I am writing an application which shows a few words in different colors in flutter.

I tried to load HTML files using the plugin flutter_html_view but that one doesn't support styles(inline). I also tried to use markdown.

How can I achieve this?

like image 625
Caffeinatedwolf Avatar asked May 27 '18 12:05

Caffeinatedwolf


People also ask

How do you make your text a different color on flutter?

Steps. Step 1: Locate the file where you have placed the Text widget. Step 2: Inside the Text widget, add the Style parameter and assign the TextStyle widget. Step 3: Inside the TextStyle widget, add the color parameter and set the color of your choice.

How do I change text color dynamically in flutter?

1. The computeLuminance() method from the Color class. You can use this method to programmatically compute the background luminance then determine whether the text color should be white or black, like this: Text( 'Hello', style: TextStyle( fontSize: 80, color: _backgroundColor.

How do you make a layout background with two different colors in flutter?

One possible approach is to use a Stack. Set the background color to your grey (i think, color blind), add the white piece as an image positioned at the bottom. Lastly add your button, again positioned at the bottom.


2 Answers

Use RichText class

var text = new RichText(   text: new TextSpan(     // Note: Styles for TextSpans must be explicitly defined.     // Child text spans will inherit styles from parent     style: new TextStyle(       fontSize: 14.0,       color: Colors.black,     ),     children: <TextSpan>[       new TextSpan(text: 'Hello'),       new TextSpan(text: 'World', style: new TextStyle(fontWeight: FontWeight.bold)),     ],   ),  ); 
like image 146
Shyju M Avatar answered Oct 05 '22 22:10

Shyju M


You can achieve this by using a RichText class class with TextStyle class

RichText widget helps to answer and achieve the above cases

RichText(   textAlign: TextAlign.center,   text: TextSpan(children: <TextSpan>[      TextSpan(        text: "I agree to the ",        style: TextStyle(color: Colors.black87)),      TextSpan(        text: "Terms and Conditions",        style: TextStyle(           color: Colors.deepPurple,            fontWeight: FontWeight.bold)),    ]), ) 

enter image description here

like image 25
Paresh Mangukiya Avatar answered Oct 05 '22 23:10

Paresh Mangukiya