How To Create Card With Background Image Using Flutter App

admin_img Posted By Bajarangi soft , Posted On 02-11-2020

just wrap your Container into a Row Widget and add a text to it. Row( children: <​Widget>[ Text("add text here"), Container( // child: I'm using a Card widget in flutter to display text. I want to use an asset image as the background/texture for the card, and display text on top. I'm having trouble figuring out how to layer the widgets properly. Any insight is much appreciated.

How To Create Card With Background Image Using Flutter App

Card With Background Image 

Step 1: 
  The first step is to create a new folder and name it "assets" at the root of the Flutter project directory as    shown in the image. Now add the image inside the newly created assets directory.


Step 2:
The image added inside the assets/images folder won't be accessible until we list it in the assets section of our pubspec.yaml file. In step 2, list the image file under the assets section of pubspec.yaml as shown below

flutter: 
  assets: 
   - assets/images/

Complete Code For Card With Background Image  In Flutter
main.dart 
import 'package:flutter/material.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.orange,
        centerTitle: true,
        title: Text('Card Background Image'),
      ),
      body: Stack(
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: ExactAssetImage('assets/images/img2.jpg'),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Center(
            child: Container(
              height: 140,
              child: Card(
                margin: EdgeInsets.all(18),
                elevation: 7.0,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Center(
                    child: Text("Contrary to popular belief, Lorem Ipsum is  not simply random text."),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

 

Related Post