【问题标题】:How to set image in a container as a background in flutter如何将容器中的图像设置为颤动的背景
【发布时间】:2020-05-26 19:47:22
【问题描述】:

我想设计一个自定义组件卡片,卡片中附有图像,标题部分将重叠在图像部分上,描述将添加在图像部分下方。如何在颤动中重叠图像上的文本?

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

【问题讨论】:

    标签: flutter dart background-image assets cardview


    【解决方案1】:

    使用

    Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage("images/background.png"), fit: BoxFit.cover)),
            child:<Widget that needs to be in foreground>
            ......
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码实现您的愿望卡。

      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,
                ),
              )
            ],
          ),
        ),
      

      【讨论】:

        猜你喜欢
        • 2020-08-17
        • 2014-06-23
        • 1970-01-01
        • 2020-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-06
        相关资源
        最近更新 更多