簡體   English   中英

如何在 flutter 中將一個容器夾在另一個容器上?

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

我想構建一個可重復使用的卡片小部件,它將具有帶有一些自定義設計布局的圖像和文本。 我盡我所能,但未能達到預期的結果。 任何幫助將非常感激。 這就是我想做的

我被困在這里

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

使用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