I was trying to add color to my Overweight and Underweight with the color red. Could you help me by letting me know how can I do that?
Here is the code:
import 'dart:math';
class CalculatorBrain {
CalculatorBrain({this.height, this.weight});
final int height;
final int weight;
double _bmi;
String calculateBMI() {
_bmi = weight / pow(height / 100, 2);
return _bmi.toStringAsFixed(1);
}
String getResults(){
if (_bmi >= 25) {
return 'Overweight';
} else if (_bmi > 18.5) {
return 'Normal';
} else {
return 'Underweight';
}
}
String getInterpretation () {
if (_bmi >= 25) {
return 'You have a higher than normal weight. Try to exercise more.';
} else if (_bmi > 18.5) {
return 'You have a normal body weight. Good job!';
} else {
return 'You have a lower than normal body weight. You can eat a bit more.';
}
}
}
CodePudding user response:
When you declare a Text
widget, you can declare also the style
of it through the TextStyle
class.
Here an example:
Text(
"Hello world!",
style: TextStyle(
color: Colors.red,
)
)
You can declare other style for text such as fontWeight
, textAlign
and so on. You can check all on the docs
In your case you can create another function that return the TextStyle
or only the text color.
Color getResultColor(){
if (_bmi >= 25) {
return Colors.red;
} else if (_bmi > 18.5) {
return Colors.black;
} else {
return Colors.red;
}
}
Finally, your Text
would be like:
Text(
getResults(),
style: TextStyle(
color: getResultsColor(),
)
)
CodePudding user response:
Strings can't have colors by itself in flutter. If you want to add a visible color you can make use of the Text widget. The text widget requires a string and then you can give it some styling too:
return Text(
getResults(),
style: TextStyle(
color: Colors.red,
),
);