How To Style A Flutter Container Background With Both Image And Color

So you want to style your flutter container to have both a background image and background color and allow text to be clearly visible over this container. Equivalent to CSS overlays. Make sure you have an image in the assets/image folder and also don't forget to add the said image to your pubspec.yaml file under assets:

1
2
3
//pubspec.yaml
assets:            
      - assets/images/answer-header.png
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Flutter code
 Container(
            width: MediaQuery.of(context).size.width,
            padding: EdgeInsets.fromLTRB(8, 40, 8, 40),
            height: 500,
            decoration: BoxDecoration(
              color: Colors.blue,
              image: DecorationImage(
                 image: AssetImage("assets/images/answer-header.png"),
                 fit: BoxFit.cover,
            ),
        ),
        child: Text(
              'Some text here for overlay effect',
              style: Theme.of(context).primaryTextTheme.bodyText1,
      ),
),

Related Posts

0 Comments

12345

    00