简体   繁体   English

如何在 flutter 中居中对齐卡片内的小部件

[英]How to center align a widget inside card in flutter

im stuck somewhere in my design, Im trying to put 2 containers that have further few Childs and they are all wrapped inside a card widget.我在我的设计中卡住了某个地方,我试图放置 2 个具有更多孩子的容器,它们都被包裹在一个卡片小部件中。 Now the problem im facing is that I want to align my widgets at an exact center position but when I do it on one screen size, it disperses on the other screen size and vice versa.现在我面临的问题是我想将我的小部件对齐在一个精确的中心 position 但是当我在一个屏幕尺寸上执行此操作时,它会分散在另一个屏幕尺寸上,反之亦然。 Im not able to make a generic design for this part to be able to work on all types of screen sizes.我无法为这部分进行通用设计,以便能够在所有类型的屏幕尺寸上工作。 Below are the attached screenshots for reference.以下是随附的屏幕截图以供参考。

屏幕 1

屏幕 2

if you see the thumbs up and gift icon, they're not aligned exactly in the middle of the greyed area of the container.如果您看到竖起大拇指和礼物图标,它们并没有完全对齐在容器灰色区域的中间。 I want them exactly to be centered.我希望它们准确地居中。 Right now if I center them on one screen, they overflow on other and vice versa.现在,如果我将它们集中在一个屏幕上,它们就会溢出到另一个屏幕上,反之亦然。 Below is the code for these parts.下面是这些部分的代码。

 //code for card 1, i-e: 98/100 card
SizedBox(
                height: 193,
                width: 180,
                child: Card(
                  color: Color(0xfffaf5f5),
                  child: Column(
                    children: <Widget>[
                      Container(
                        width: MediaQuery.of(context).size.width,
                        height: MediaQuery.of(context).size.height * 0.16,
                        color: Colors.white,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text(
                              '98',
                              style: TextStyle(
                                fontFamily: 'Karla',
                                fontSize: 60,
                                fontWeight: FontWeight.bold,
                                color: Color(0xff54c19f),
                              ),
                            ),
                            Text(
                              '/100',
                              style: TextStyle(
                                fontFamily: 'Karla',
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                                color: Color(0xff75777d),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        alignment: Alignment.center,
                        color: Colors.transparent,
                        width: MediaQuery.of(context).size.width,
                        margin: EdgeInsets.only(top: 7),
                        child: Image.asset(
                          'images/pouce-ok.png',
                          width: 30,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              //code for card 2, i-e: 129pts card
              SizedBox(
                height: 193,
                width: 180,
                child: Card(
                  color: Color(0xfffaf5f5),
                  child: Column(
                    children: <Widget>[
                      Container(
                        width: MediaQuery.of(context).size.width,
                        height: MediaQuery.of(context).size.height * 0.16,
                        color: Colors.white,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text(
                              '129',
                              style: TextStyle(
                                fontFamily: 'Karla',
                                fontSize: 60,
                                fontWeight: FontWeight.bold,
                                color: Color(0xff478dd0),
                              ),
                            ),
                            Text(
                              'pts',
                              style: TextStyle(
                                fontFamily: 'Karla',
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                                color: Color(0xff75777d),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 7),
                        child: Image.asset(
                          'images/cadeaux.png',
                          width: 35,
                        ),
                      ),
                    ],
                  ),
                ),
              ),

I want them all to be centered align regardless of the screen size, any help would be appreciated, thnx in advance.无论屏幕大小如何,我都希望它们都居中对齐,任何帮助都将不胜感激,thnx提前。

guys I appreciate your help but I figured it out myself after experimenting different techniques and spending heck lot if time.伙计们,我感谢您的帮助,但我在尝试了不同的技术并花了很多时间后自己想通了。 so basically I was looking at the wrong place in the code.所以基本上我在看代码中的错误位置。 I shouldn't have provided a fixed height of the card instead by using a media query in place of height property of SizedBox I did it responsive relative to all the devices.我不应该通过使用媒体查询代替 SizedBox 的 height 属性来提供卡的固定高度,而是相对于所有设备进行响应。 below is the code of how I did it:下面是我如何做到这一点的代码:

//old code
SizedBox(
            height: 193,
            width: 180,
            child: Card(
              color: Color(0xfffaf5f5),
              child: Column(...

//new code
SizedBox(
            //this is the how the height should be given
            height: MediaQuery.of(context).size.height * 0.25,
            width: 180,
            child: Card(
              color: Color(0xfffaf5f5),
              child: Column(....

So that's how I have achieved it.所以我就是这样实现的。 Thankyou for your help again, Thanks:)再次感谢您的帮助,谢谢:)

mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment:CrossAxisAlignment.center,

That should do it.那应该这样做。

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

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