【问题标题】:How to align widget under another widget in Flutter?如何在 Flutter 中的另一个小部件下对齐小部件?
【发布时间】:2021-12-09 15:46:33
【问题描述】:

我有一个小部件表示进度 like this

我需要能够将名称放在气泡下,但是当我这样做时:

Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      border: Border.all(
                        color: Colors.green,
                        width: 1.5,
                      ),
                    ),
                    height: MediaQuery.of(context).size.height / 10,
                    width: MediaQuery.of(context).size.width / 10,
                    child: Center(
                      child: Icon(
                        Icons.check,
                        color: Colors.green,
                        size: 30,
                      ),
                    ),
                  ),
                  Text("Test label"),
                ],
              ),

this happens.

我还尝试将条与圆圈排成一行:

 Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Row(
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(
                              vertical:
                                  MediaQuery.of(context).size.height / 20,
                            ),
                            height:
                                1 + MediaQuery.of(context).size.height / 10,
                            width: MediaQuery.of(context).size.width / 16,
                          ),
                          Container(
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              border: Border.all(
                                color: mainColor,
                                width: 1.5,
                              ),
                            ),
                            height: MediaQuery.of(context).size.height / 10,
                            width: MediaQuery.of(context).size.width / 10,
                            child: Center(
                              child: Icon(
                                Icons.check,
                                color: mainColor,
                                size: 30,
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(
                              vertical:
                                  MediaQuery.of(context).size.height /20,
                            ),
                            height:
                                1 + MediaQuery.of(context).size.height / 10,
                            width: MediaQuery.of(context).size.width / 16,
                            child: Container(
                              color: grey,
                            ),
                          ),
                        ],
                      ),
                    Text("Test label"),
                    ],
                  ),

This is better,但仍然会产生问题if the text is bigger

如何在不引起线条问题的情况下对齐圆圈下方的文本?

整个widget的代码(抱歉这么长,不知道怎么显示):

Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(
                      vertical:
                      MediaQuery.of(context).size.height / 20,
                    ),
                    height:
                    1 + MediaQuery.of(context).size.height / 10,
                    width: MediaQuery.of(context).size.width / 16,
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          border: Border.all(
                            color: Colors.green,
                            width: 1.5,
                          ),
                        ),
                        height: MediaQuery.of(context).size.height / 10,
                        width: MediaQuery.of(context).size.width / 10,
                        child: Center(
                          child: Icon(
                            Icons.check,
                            color: Colors.green,
                            size: 30,
                          ),
                        ),
                      ),
                      Text("Test label"),
                    ],
                  ),Container(
                    padding: EdgeInsets.symmetric(
                      vertical:
                      MediaQuery.of(context).size.height / 20,
                    ),
                    height:
                    1 + MediaQuery.of(context).size.height / 10,
                    width: MediaQuery.of(context).size.width / 16,
                    child: Container(
                      color: Colors.grey,
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(
                      vertical: MediaQuery.of(context).size.height / 20,
                    ),
                    height: 1 + MediaQuery.of(context).size.height / 10,
                    width: 3 / 2 * MediaQuery.of(context).size.width / 8,
                    child: Container(
                      color: Colors.grey,
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      border: Border.all(
                        color: Colors.grey,
                        width: 1.5,
                      ),
                    ),
                    height: MediaQuery.of(context).size.height / 10,
                    width: MediaQuery.of(context).size.width / 10,
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(
                      vertical: MediaQuery.of(context).size.height / 20,
                    ),
                    height: 1 + MediaQuery.of(context).size.height / 10,
                    width: MediaQuery.of(context).size.width / 8,
                    child: Container(
                      color: Colors.grey,
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(
                      vertical: MediaQuery.of(context).size.height / 20,
                    ),
                    height: 1 + MediaQuery.of(context).size.height / 10,
                    width: MediaQuery.of(context).size.width / 8,
                    child: Container(
                      color: Colors.grey,,
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      border: Border.all(
                        color: Colors.grey,,
                        width: 1.5,
                      ),
                    ),
                    height: MediaQuery.of(context).size.height / 10,
                    width: MediaQuery.of(context).size.width / 10,
                  )
                ],
              ),

【问题讨论】:

    标签: flutter flutter-layout alignment


    【解决方案1】:

    我建议的一个解决方案是尝试使用 ConstrainedBoxContainer 包装文本并限制最大宽度。

    例如-

    ConstrainedBox(
      constraints: BoxConstraints(maxWidth: 50),
        child: Container(
          child: Text('Your Text'),
      ),
    ),
    

    认为如果文本太长而无法放在一行中,则会将其移动到下一行。

    【讨论】:

    • 如果我包括灵活的,圆圈和文字都会消失。没有它,它工作得很好,谢谢
    • 太好了,我会编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多