Search code examples
fluttertextborderflutter-text

How to give border/background to a Text in Flutter?


I want to give my text a border or background. Just a simple text in red with a black border around it.

See the following image as inspiration:

enter image description here


Solution

  • I found the answer in Flutter Documentation

    There's no border property but yes you can use two overlapping Texts to do the job. Here's the code

    Stack(
      children: <Widget>[
        // Stroked text as border.
        Text(
          'WHO',
          style: TextStyle(
            fontSize: 40,
            foreground: Paint()
              ..style = PaintingStyle.stroke
              ..strokeWidth = 6
              ..color = Colors.grey,
          ),
        ),
        // Solid text as fill.
        Text(
          'WHO',
          style: TextStyle(
            fontSize: 40,
            color: Colors.red,
          ),
        ),
      ],
    )