【问题标题】:How to clip one container over another in flutter?如何将一个容器夹在另一个容器上?
【发布时间】:2021-04-12 08:35:23
【问题描述】:

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

这就是我想做的事情

我被困在这里了

这是我的代码

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

【问题讨论】:

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


    【解决方案1】:

    使用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),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
    

    【讨论】:

    • 很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2019-12-15
    相关资源
    最近更新 更多