Home > Software design >  How can I make part of the text bold?
How can I make part of the text bold?

Time:10-11

I am trying to make only the first word bold and I want to continue using the switch function how can I do it?

    String get foodText {
    switch (food) {
      case Food.yes:
        return 'we have food';
      case Food.no:
        return 'we don't have food';
    }
  }

this is where I print it

 Row(
                children: [
                  const Icon(Icons.food_bank),
                  const SizedBox(width: 6),
                  Text(foodText),
                ],
              ),

CodePudding user response:

You can use RichText and return widget in your get function:

Widget get foodText {
    switch (food) {
      case Food.yes:
        return RichText(
          textAlign: TextAlign.end,
          text: TextSpan(
              text: 'w',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
              children: <TextSpan>[
                TextSpan(
                  text: 'e have food',
                  style: TextStyle(
                    fontWeight: FontWeight.normal,
                    color: Colors.black,
                  ),
                ),
              ]),
        );
      case Food.no:
      default:
        return RichText(
          textAlign: TextAlign.end,
          text: TextSpan(
              text: 'w',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
              children: <TextSpan>[
                TextSpan(
                  text: 'e don\'t have food',
                  style: TextStyle(
                    fontWeight: FontWeight.normal,
                    color: Colors.black,
                  ),
                ),
              ]),
        );
    }
  }

and use it like this:

Row(
  children: [
     const Icon(Icons.food_bank),
     const SizedBox(width: 6),
     foodText,
   ],
),
  • Related