Home > OS >  How to define conditional text color in Text widget Flutter
How to define conditional text color in Text widget Flutter

Time:01-31

Here is my code

Text(
  nDataList.temp >= temp ? 'Normal' : 'Not Normal',
  style: const TextStyle(
    fontWeight: FontWeight.bold,
    fontSize: 12,
    color: (nDataList.temp >= temp ? Colors.white : Colors.red),
  ),
),

I want to get specific color when nDataList.temp >= temp

But my

color: (nDataList.temp >= temp ? Colors.white : Colors.red)

This portion is not working.

CodePudding user response:

I think your issue is related to null safety if nDataList.temp allows null you can't do the check as you did, the solution could be

Text(
  (nDataList.temp ?? 0) >= temp ? 'Normal' : 'Not Normal',
  style: const TextStyle(
    fontWeight: FontWeight.bold,
    fontSize: 12,
    color: (nDataList.temp ?? 0) >= temp ? Colors.white : Colors.red,
  ),
),

CodePudding user response:

Try this in your commandline:

First, flutter clean

Then flutter run

  •  Tags:  
  • Related