flutter - 如何将容器中的图像设置为 flutter 中的背景

标签 flutter dart background-image assets cardview

我想设计一个自定义组件卡,卡中附有图像,标题部分将重叠在图像部分上,描述将添加在图像部分下方。如何在 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

最佳答案

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

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

关于flutter - 如何将容器中的图像设置为 flutter 中的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60168242/

相关文章:

css - 全宽屏幕图像

flutter - 自从Catalina更新以来,Flutter无法打印/调试打印/记录日志并忽略断点

dart - Flutter:使用全局流?

android - 在 Flutter ListView 中加载更多选项

performance - Dart - 异步与同步性能注意事项

css - 从左到右动画文本填充

jquery - 当我切换/添加具有其他背景的另一个类时,为什么我的背景 svg 没有改变?

flutter - 单击按钮后执行函数 FLUTTER

dart - 在调试和生产模式之间切换

javascript - 从 dart native/flutter 调用 javascript