How to set image in a container as a background in flutter
Asked Answered
B

2

5

I want to design a custom component card, an image attached in the card, title section will be overleaping on the image section, and description will be added below image section. how to overleap a text on an image in flutter?

class CardWithImage extends StatelessWidget {
  final String title, buttonText;
  final String subTitle;
  final String body;
  final asset;

  CardWithImage({
    this.title,
    this.subTitle,
    this.body,
    this.asset,
    this.buttonText,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      margin: EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: ThemeColors.primaryColor,
        borderRadius: BorderRadius.circular(5),
        boxShadow: [
          BoxShadow(
            color: ThemeColors.grey5,
            blurRadius: 10,
            spreadRadius: 5,
            offset: Offset(3, 3),
          )
        ],
      ),
      child: Column(
        children: <Widget>[
          Image.asset(
            asset,
            height: 200,
            width: MediaQuery.of(context).size.width,
            fit: BoxFit.cover,
          ),

        ],
      ),
    );
  }
}

I want to create a card like this image

Betroth answered 11/2, 2020 at 11:54 Comment(0)
M
5

you can Achieve your desire card using following code.

Container(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          height: 200,
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage('assets/images/background.png'),
                fit: BoxFit.cover),
          ),
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              color: Colors.grey.withOpacity(0.3),
              height: 50,
              width: MediaQuery.of(context).size.width,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  "Title",
                ),
              ),
            ),
          ),
        ),
        Container(
          padding: EdgeInsets.all(10),
          child: Text(
            "Your Big Text ",
            textAlign: TextAlign.left,
          ),
        )
      ],
    ),
  ),
Menashem answered 12/2, 2020 at 5:42 Comment(0)
A
5

Use

Container(
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("images/background.png"), fit: BoxFit.cover)),
        child:<Widget that needs to be in foreground>
        ......
Acrosstheboard answered 11/2, 2020 at 11:59 Comment(0)
M
5

you can Achieve your desire card using following code.

Container(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          height: 200,
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage('assets/images/background.png'),
                fit: BoxFit.cover),
          ),
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              color: Colors.grey.withOpacity(0.3),
              height: 50,
              width: MediaQuery.of(context).size.width,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  "Title",
                ),
              ),
            ),
          ),
        ),
        Container(
          padding: EdgeInsets.all(10),
          child: Text(
            "Your Big Text ",
            textAlign: TextAlign.left,
          ),
        )
      ],
    ),
  ),
Menashem answered 12/2, 2020 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.