flutter - 如何在 flutter 中将一个容器夹在另一个容器上?

标签 flutter user-interface containers flutter-layout flutter-dependencies

我想构建一个可重复使用的卡片小部件,它将包含带有一些自定义设计布局的图像和文本。我尽我所能,但未能达到预期的结果。任何帮助将不胜感激。

这就是我想要做的
This is what I want to do

我被困在这里
I'm stuck here

这是我的代码

class ReusabelCard extends StatelessWidget {
      ReusabelCard(
          {this.cardChild, @required this.assetImagePath, @required this.cardText});
      final Widget cardChild;
      final String assetImagePath;
      final String cardText;
      @override
      Widget build(BuildContext context) {
        return Container(
            height: MediaQuery.of(context).size.height * 0.35,
            width: MediaQuery.of(context).size.width * 0.5,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(MediaQuery.of(context).size.width * 0.5 * 0.28),
            ),
            child: Stack(
              children: [
                LayoutBuilder(
                  builder: (context, contraint) {
                    return Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Icon(
                          Icons.trip_origin,
                          size: contraint.biggest.width,
                          color: Colors.grey[300],
                        ),
                        Container(
                          height: MediaQuery.of(context).size.height*0.05,
                          width: MediaQuery.of(context).size.width,
                          color: Colors.green,
                        ),
                      ],
                    );
                  },
                ),
              ],
            )
            
            );
      }
    }

最佳答案

使用 ClipRRect 来实现:

ClipRRect(
  borderRadius: BorderRadius.circular(50.0), //clipping the whole widget
  child: Container(
    height: MediaQuery.of(context).size.height * 0.4, //I adjusted here for responsiveness problems on my device
    width: MediaQuery.of(context).size.width * 0.5,
    color: Colors.white,
    child: LayoutBuilder(
      builder: (context, constraint) {
        return Stack(
          children: [
            Center(
              child: Icon(
                Icons.trip_origin,
                size: constraint.biggest.width,
                color: Colors.grey[300],
              ),
            ),
            Positioned(
              right: 0,
              left: 0,
              top: 20.0,
              child: Icon(
                Icons.sports_volleyball_rounded, //just to represent the ball
                size: constraint.biggest.width * 0.5,
              ),
            ),
            Positioned(
              bottom: 0.0,
              child: Container(
                alignment: Alignment.center,
                height: MediaQuery.of(context).size.height * 0.1,
                width: constraint.biggest.width,
                color: Colors.yellow[700],
                child: Text(
                  'Sports',
                  style: Theme.of(context)
                      .textTheme
                      .headline3
                      .copyWith(color: Colors.white),
                ),
              ),
            ),
          ],
        );
      },
    ),
  ),
);

关于flutter - 如何在 flutter 中将一个容器夹在另一个容器上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65598454/

相关文章:

Flutter - 未处理的异常 : type '_InternalLinkedHashMap<String, 动态>

flutter - BLoC 在 Flutter 中请求位置时不会产生状态

flutter - 将Flutter App上传到App Store时出错:非公开API使用情况

swift - 当 isEnabled 为 false(禁用)时避免使 NSButton 透明/透明

flutter - Flutter:为什么内容替换列表中的值而不是添加到列表中?

c++ - Qt creator,在指定位置插入自定义菜单到菜单栏

ajax - 优雅降级 - 何时考虑

带有 slurm 的 kubernetes,这是正确的设置吗?

Azure 容器实例 - 同一容器中的 DNS 和子网

logging - 如何在 Bluemix 上启用对容器的监控和日志记录?