简体   繁体   English

Flutter中如何正确实现堆栈?

[英]How to Implement Stack Correctly in Flutter?

Basically I'm new to Flutter and trying to make a design like this -基本上我是 Flutter 的新手,并试图做出这样的设计 -

在此处输入图像描述

So I Tried Stack for this but couldn't make it perfect.因此,我为此尝试了 Stack,但无法使其完美。 Here is my code and image这是我的代码和图像

Stack(
   children: [
      Positioned(
         child: Container(
            height: 150,
            width: 150,
            decoration: BoxDecoration(
               borderRadius: BorderRadius.circular(10.0),
               color: Colors.yellow,
               ),
            ),
         ),
      Positioned(
         child: Container(
         height: 250,
         width: 250,
         decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10.0),
            color: Colors.red,
            ),
         ),
         top: 10,
         left: 5,
         right: 5,
       ),
  ],),

在此处输入图像描述

You will need to give proper positione to each and every Positioned widget.您需要为每个 Positioned 小部件提供适当的位置。

This code will provide you output which you expact.此代码将为您提供您所期望的 output。 You can checkout interactive code here .您可以在此处签出交互式代码。

      Stack(
        children: [
          Positioned(
            left: 125,
            top: 50,
            child: Container(
              height: 250,
              width: 250,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.0),
                color: Colors.black,
              ),
            ),
          ),
          Positioned(
            left: 100,
            top: 60,
            child: Container(
              height: 300,
              width: 300,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.0),
                color: Colors.red,
              ),
            ),
          ),
          Positioned(
            left: 75,
            top: 75,
            child: Container(
              height: 350,
              width: 350,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.0),
                color: Colors.yellow,
              ),
            ),
          ),
        ],
      )

在此处输入图像描述

Rather then achieve it with Stack widget you can try this with Column widget like below而不是使用Stack小部件来实现它,您可以尝试使用Column小部件,如下所示

Column(
    children: [
      Container(
        height: 20,
        width: MediaQuery.of(context).size.width - 150,
        decoration: BoxDecoration(/*as-per-your-requirement*/),
      ),
      Container(
        height: 20,
        width: MediaQuery.of(context).size.width - 100,
        decoration: BoxDecoration(/*as-per-your-requirement*/),
      ),
      Container(
        ///this will be your main layout that user can completely see
        width: MediaQuery.of(context).size.width - 50,
        decoration: BoxDecoration(/*as-per-your-requirement*/),
        child: /*as-per-your-requirement*/,
      ),
    ],
  )
Stack(
    children: [
      Positioned(
          top: -52.h,
          left: -22.w,
          child: Text(
            "$position",
            style: TextStyle(
                fontWeight: FontWeight.w700,
                fontSize: 200.sp,
                color: Color(0x2007B4CF)),
          )),
      Positioned(
        child: SvgPicture.asset(Res.ic_plan_lock),
        top: 20.h,
        right: 20.w,
      ),
      Positioned(
        top: 46.h,
        left: 13.w,
        right: 13.w,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SvgPicture.asset(Res.ic_plan_secured),
            SizedBox(
              height: 10.h,
            ),
            Text(
              offer.offerName,
              style: TextStyle(
                  fontWeight: FontWeight.w600,
                  fontSize: 14.sp,
                  color: ColorUtil.of(context).primaryDark),
            ),
            SizedBox(
              height: 12.h,
            ),
            Text(
              offer.offerText,
              textAlign: TextAlign.center,
              style: TextStyle(
                  fontWeight: FontWeight.w600,
                  fontSize: 14.sp,
                  color: Colors.black),
            ),
          ],
        ),
      ),
      Positioned(
          bottom: 20.h,
          left: 20.h,
          right: 20.h,
          child: GestureDetector(
            behavior: getDefaultGestureDetectorBehaviour(),
            onTap: () {
              onCtaClicked();
            },
            child: Container(
              height: 31.h,
              width: 159.w,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: Color(0xFF2D2D3D),
                  borderRadius: BorderRadius.circular(10.h)),
              child: Text(
                offer.route,
                style: TextStyle(
                    color: ColorUtil.of(context).colorPrimaryLight,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w500),
              ),
            ),
          ))
    ],
  ),

Please update your code with the below code.请使用以下代码更新您的代码。 Here, I have fix the height only as device wise width could be changed.在这里,我只修复了高度,因为可以更改设备宽度。 I have used Align widget to position the widget to bottom.我已经使用将小部件对齐到 position 小部件到底部。

Stack(
        children: [
          Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              margin: const EdgeInsets.symmetric(horizontal: 90),
              height: 160,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)),
                color: Colors.greenAccent,
              ),
            ),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              margin: const EdgeInsets.symmetric(horizontal: 50),
              height: 140,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15)),
                color: Colors.orange,
              ),
            ),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 0),
              height: 120,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)),
                color: Colors.limeAccent,
                boxShadow: [
                  BoxShadow(
                    color: Colors.yellow,
                    offset: Offset(0.0, 1.0), //(x,y)
                    blurRadius: 5.0,
                  ),
                ],
              ),
            ),
          ),
        ],)

It will look like this,它看起来像这样,

在此处输入图像描述

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

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