Debug School

rakesh kumar
rakesh kumar

Posted on

HOW TO CHANGE THE COLOR OF TEXT DYNAMICALLY IN FLUTTER?-FLUTTER

Output Screenshot:

SOLUTION

Color getColor(number) {
   if (number > 0 && number < 100) return Colors.red;
   if (number >= 100 && number < 200) return Colors.blue;
   ...
}
Enter fullscreen mode Exit fullscreen mode

color: getColor(item),

Color textColor = Colors.black; // Default color

TextStyle(
  fontWeight: FontWeight.bold,
  color: textColor,
),
Enter fullscreen mode Exit fullscreen mode
FlatButton(
  onPressed: () {
    setState(() => textColor =
        Color((Random().nextDouble() * 0xFFFFFF).toInt() << 0)
            .withOpacity(1.0)); // this is generate random color, u can use your own..
  },
  child: Text("change color"),
),
Enter fullscreen mode Exit fullscreen mode

Full Summary:

Refrence
Click here

Top comments (0)