Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add background image in flutter

I am trying to create the attached screen in Flutter. How do I add a background image and add the text at the specific location (ignore the white text box).

Thanks for your help

enter image description here

like image 838
user2570135 Avatar asked Apr 17 '19 12:04

user2570135


People also ask

How do I decorate an image in Flutter?

Certain Decoration classes in Flutter allow you to pass an image to use as a decoration. The image needs to be defined as a DecorationImage . It's quite common to use a DecorationImage for setting a background image. Not only defining the image to use, you can also adjust how the image should be painted.


2 Answers

To add background image you have to use DecorationImage class and inside BoxDecoration.

 class Home extends StatelessWidget{
      @override
      Widget build(BuildContext context){
        return Scaffold(
          body: Container(
            decoration: BoxDecoration(
              image: DecorationImage(image: AssetImage("assets/image1.jpg"), fit: BoxFit.cover),
            ),
            child: Center(child: Text('Welcome To',style: TextStyle(
              color: Colors.white,
              fontSize: 40.0
            ),)),
            )
        );
      }
    }

enter image description here

like image 57
Tahseen Quraishi Avatar answered Oct 06 '22 10:10

Tahseen Quraishi


Also make sure to create an assets directory and add your image asset(s) to it, then update your pubspec.yaml file under "flutter:" as below with:

flutter:
  assets:
    - assets/splash.png

Where splash.png is your image asset. Or just use:

flutter:
  assets:
    - assets/

if you want the whole directory. If not, you'll just render a blank container.

like image 23
Oprimus Avatar answered Oct 06 '22 10:10

Oprimus