【问题标题】:How to create card view with text in front of the image in flutter?如何在颤动的图像前创建带有文本的卡片视图?
【发布时间】:2019-08-30 10:07:54
【问题描述】:

我花了几个小时在颤振中创建一个可重用的小部件。我的预期结果是这样的:

但我坚持根据它的父级填充不透明度宽度,如下所示:

这是我尝试过的代码:

Stack(
  children: <Widget>[
    Container(
      padding: const EdgeInsets.all(12),
      child: Image.network(
        "https://raw.githubusercontent.com/flutter/website/master/examples/layout/lakes/step5/images/lake.jpg",
        fit: BoxFit.cover
      )
    ),
    Positioned(
      left: 15,
      bottom: 15,
      child: Container(
        decoration: BoxDecoration(
          color: Color.fromRGBO(0, 0, 0, 0.5)
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Text("TITLE", style: new TextStyle(fontSize: 28, color: Colors.white)),
                new Text("Sub Title", style: new TextStyle(fontSize: 20, color: Colors.white))
              ],
            ),
            FlatButton(
              color: Colors.red[400],
              highlightColor: Colors.red[900],
              onPressed: (){},
              child: new Text("Book Now", style: new TextStyle(fontSize: 28, color: Colors.white)),
            )
          ],
        ),
      )
    )
  ],
)

有什么建议吗?

【问题讨论】:

    标签: flutter flutter-card


    【解决方案1】:

    简答: 您需要在 Positioned 小部件中添加 right 属性。像这样

    Positioned(
      left: 15,
      bottom: 15,
      right: 0,
      ...)
    

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text("Title")),
        body: Container(
          width: 300,
          height: 300,
          child: Stack(
            alignment: Alignment.bottomLeft,
            children: <Widget>[
              Image.asset(
                "assets/images/chocolate_pic.png",
                width: 300,
                height: 300,
                fit: BoxFit.cover,
              ),
              Container(
                color: Colors.black.withOpacity(0.5),
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Text("Awesome", style: TextStyle(fontSize: 28)),
                    Text("Chocolate", style: TextStyle(fontSize: 22)),
                  ],
                ),
              )
            ],
          ),
        ),
      );
    }
    

    【讨论】:

      【解决方案2】:

      而不是这个

      Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      new Text("TITLE", style: new TextStyle(fontSize: 28, color: Colors.white)),
                      new Text("Sub Title", style: new TextStyle(fontSize: 20, color: Colors.white))
                    ],
                  ),
                  FlatButton(
                    color: Colors.red[400],
                    highlightColor: Colors.red[900],
                    onPressed: (){},
                    child: new Text("Book Now", style: new TextStyle(fontSize: 28, color: Colors.white)),
                  )
                ],
              ),
      

      您可以在具有titlesubtitletrailing 的定位和容器内使用ListTile 小部件(可以在此处放置立即预订按钮)。并将容器的背景颜色设置为具有不透明度的黑/白。

      【讨论】:

      • 你能根据我的问题提供一些例子吗?我尝试使用ListTile,但出现错误在 performLayout() 期间抛出了以下断言
      • 您能否用 listTile 和整个错误更新您的问题。
      • 我不能因为代码太多,但错误看起来像i.stack.imgur.com/c2VDp.png
      猜你喜欢
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 2020-05-24
      • 2021-11-04
      • 2013-08-06
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多