简体   繁体   English

如何使小部件的宽度与Flutter中的屏幕一样大?

[英]How to make a widget take as much width as the screen in Flutter?

In my flutter project, I haved used a Row where I aligned two text horizontally, the first Text size is fixed but the I want the second one to have as much width as the screen size. 在我的flutter项目中,我使用了一个Row,将两个文本水平对齐,第一个Text的大小是固定的,但是我希望第二个Text的宽度与屏幕的大小一样。

Here's is my code- 这是我的代码-

            Container(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: (
                  Row(
                    children: <Widget>[
                      Container(
                        width: 150,
                        color: Colors.blue,
                        child: Text("City: "),
                      ),
                      Container(

                        color: Colors.red,
                        child: Text("London"),
                      )
                    ],
                  )
                ),
              ),
            ),



            Container(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: (
                    Row(
                      children: <Widget>[
                        Container(
                          width: 150,
                          color: Colors.blue,
                          child: Text("Country: "),
                        ),
                        Container(

                          color: Colors.red,
                          child: Text("England "),
                        )
                      ],
                    )
                ),
              ),
            ),



            Container(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: (
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          width: 150,
                          color: Colors.blue,
                          child: Text("Address: "),
                        ),
                        Container(
                          color: Colors.red,
                          child: Text("aaaaa zzzzz wwwww qqqqqq rrrrr"),
                        )
                      ],
                    )
                ),
              ),
            ),

With the above code I got the following output- 使用上面的代码,我得到以下输出-

在此处输入图片说明

The problem is the black marked section in the image. 问题是图像中的黑色标记部分。

So, I need help to know how the text will take as much width as the screen size & rest of the text will be shown in the next line. 因此,我需要帮助来了解文本将采用与屏幕尺寸一样大的宽度,而文本的其余部分将在下一行显示。

The text widget has a property called overflow you can use that to either clip the extra string or put ellipsis. 文本小部件具有一个称为overflow的属性,您可以使用该属性剪切多余的字符串或放置省略号。 You could try different types of TextOverflow like a clip, ellipsis, fade, etc. As for the putting it to the next line you could set maxLines property to any number you like. 您可以尝试不同类型的TextOverflow,例如剪辑,省略号,淡入淡出等。至于将其放置到下一行,您可以将maxLines属性设置为您喜欢的任何数字。

example: 例:

Container(
    width: MediaQuery.of(context).size.width / 2
    child: Text(
      "Hello world!!!!",
      maxLines: 2,
      overflow: TextOverflow.ellipsis
    ),
);

This problem can be solved by a widget called Flexible . 可以通过名为Flexible的小部件解决此问题。 I had to write few more lines for solving these but it works perfectly for the problem mentioned in the question. 我不得不再写几行来解​​决这些问题,但是对于问题中提到的问题,它行之有效。

So, here's the code- 所以,这是代码-

            Container(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: (
                  Row(
                    children: <Widget>[
                      Container(
                        width: 150,
                        color: Colors.blue,
                        child: Text("City: "),
                      ),
                      Container(

                        color: Colors.red,
                        child: Text("London"),
                      )
                    ],
                  )
                ),
              ),
            ),



            Container(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: (
                    Row(
                      children: <Widget>[
                        Container(
                          width: 150,
                          color: Colors.blue,
                          child: Text("Country: "),
                        ),
                        Container(

                          color: Colors.red,
                          child: Text("England "),
                        )
                      ],
                    )
                ),
              ),
            ),



            Container(

              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: (
                    Row(

                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          width: 150,
                          color: Colors.blue,
                          child: Text("Address: "),
                        ),
                        new Flexible(
                          child: new Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(

                                color: Colors.red,
                                child: Text("aaaaa zzzzz qqqqqq eeeeeeeeeeeeee "),
                              )
                            ],
                          ),
                        ),


                      ],
                    )
                ),
              ),
            ),

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

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