简体   繁体   English

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

[英]How to clip one container over another in flutter?

I want to build a reusable card widget it will have an image and text with some custom design layout.我想构建一个可重复使用的卡片小部件,它将具有带有一些自定义设计布局的图像和文本。 I tried everything I could but wasn't able to achieve the desired result.我尽我所能,但未能达到预期的结果。 Any help would be much appreciated.任何帮助将非常感激。 This is what I want to do这就是我想做的

I'm stuck here我被困在这里

This is my code 


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

Use ClipRRect to do it:使用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),
                ),
              ),
            ),
          ],
        );
      },
    ),
  ),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM