if-statementflutterdarttextstyle

How to include if statement in dart's TextStyle


I have a TextStyle and a variable textBold that can be true or false. How to implement if in this TextStyle?

TextStyle(
    color: Colors.white,
    if ($textBold == true){
      fontWeight: FontWeight.bold,
    }

Solution

  • Use conditional statement like below.

    Text(
           "Hello",
            style: TextStyle(
            fontWeight: textBold ? FontWeight.bold : FontWeight.normal
           ),
          ),