flutter - 如何使我的内容具有响应性并且不会溢出容器

标签 flutter dart

这是我的容器在模拟器 (Pixel 3) 上的样子:

enter image description here

现在这就是它在我的 Galaxy S9 上的样子:

enter image description here

我专栏的文字溢出来了。我以为它会自动“适应”,但似乎并非如此。

溢出的4个元素的代码是:

                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Row(
                                    //mainAxisAlignment: MainAxisAlignment.start,
                                    children: <Widget>[
                                      Icon(
                                        Icons.turned_in_not,
                                        color: Colors.grey[700],
                                        size: 18.0,
                                      ),
                                      SizedBox(width: 7.0,),
                                      Text(
                                        'Economy',
                                        style: TextStyle(
                                          fontFamily: 'SamsungSans',
                                          fontSize: 15.0,
                                          color: Colors.grey[700],
                                          fontWeight: FontWeight.w400
                                        ),
                                      ),
                                    ],
                                  ),
                                  SizedBox(
                                    height: 15.0,
                                  ),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: <Widget>[
                                      Icon(
                                        Icons.location_on,
                                        color: Colors.grey[700],
                                        size: 18.0,
                                      ),
                                      SizedBox(width: 7.0,),
                                      Text(
                                        'Terminal 1',
                                        style: TextStyle(
                                          fontFamily: 'SamsungSans',
                                          fontSize: 15.0,
                                          color: Colors.grey[700],
                                          fontWeight: FontWeight.w400
                                        ),
                                      ),
                                    ],
                                  ),

                                ],
                              ),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Row(
                                    children: <Widget>[
                                      Icon(
                                        Icons.airline_seat_legroom_normal,
                                        color: Colors.grey[700],
                                        size: 18.0,
                                      ),
                                      SizedBox(width: 7.0,),
                                      Text(
                                        'Standard legroom',
                                        style: TextStyle(
                                          //fontFamily: 'SamsungSans',
                                          fontSize: 14.0,
                                          color: Colors.grey[700],
                                          fontWeight: FontWeight.w400
                                        ),
                                      ),
                                    ],
                                  ),
                                  SizedBox(
                                    height: 15.0,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Icon(
                                        Icons.card_travel,
                                        color: Colors.grey[700],
                                        size: 18.0,
                                      ),
                                      SizedBox(width: 7.0,),
                                      Text(
                                        '1 included',
                                        style: TextStyle(
                                          fontFamily: 'SamsungSans',
                                          fontSize: 15.0,
                                          color: Colors.grey[700],
                                          fontWeight: FontWeight.w400
                                        ),
                                      ),
                                    ],
                                  )
                                ],
                              )
                            ],
                          ),                          

知道我应该更改什么以使文本不会溢出其容器吗?

谢谢。

最佳答案

我建议用 Expanded 小部件包装溢出的小部件。希望对您有所帮助。

更新 我只是在一些具有 mdpi 分辨率的旧设备上使用了上面的代码。为了修复溢出,我在第二个 Column 中添加了 Expanded,它解决了溢出问题,但“Standard legroom”文本仍然溢出。因此,如果字符串非常长,我建议为您的文本添加 overflow: TextOverflow.ellipsis

如果您想保留整个字符串,只需将 Expanded 添加到溢出的 Text 小部件。但这种情况下会出现多行文字,无法解决。

无论如何,这是我的最终代码:

    Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              //mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Icon(
                  Icons.turned_in_not,
                  color: Colors.grey[700],
                  size: 18.0,
                ),
                SizedBox(
                  width: 7.0,
                ),
                Text(
                  'Economy',
                  style: TextStyle(
                      fontFamily: 'SamsungSans',
                      fontSize: 15.0,
                      color: Colors.grey[700],
                      fontWeight: FontWeight.w400),
                ),
              ],
            ),
            SizedBox(
              height: 15.0,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Icon(
                  Icons.location_on,
                  color: Colors.grey[700],
                  size: 18.0,
                ),
                SizedBox(
                  width: 7.0,
                ),
                Text(
                  'Terminal 1',
                  style: TextStyle(
                      fontFamily: 'SamsungSans',
                      fontSize: 15.0,
                      color: Colors.grey[700],
                      fontWeight: FontWeight.w400),
                ),
              ],
            ),
          ],
        ),
        Expanded(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Icon(
                    Icons.airline_seat_legroom_normal,
                    color: Colors.grey[700],
                    size: 18.0,
                  ),
                  SizedBox(
                    width: 7.0,
                  ),
                  Expanded(
                    child: Text(
                      'Standard legroom',
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                          //fontFamily: 'SamsungSans',
                          fontSize: 14.0,
                          color: Colors.grey[700],
                          fontWeight: FontWeight.w400),
                    ),
                  ),
                ],
              ),
              SizedBox(
                height: 15.0,
              ),
              Row(
                children: <Widget>[
                  Icon(
                    Icons.card_travel,
                    color: Colors.grey[700],
                    size: 18.0,
                  ),
                  SizedBox(
                    width: 7.0,
                  ),
                  Text(
                    '1 included',
                    style: TextStyle(
                        fontFamily: 'SamsungSans',
                        fontSize: 15.0,
                        color: Colors.grey[700],
                        fontWeight: FontWeight.w400),
                  ),
                ],
              )
            ],
          ),
        )
      ],
    );

Two options[![][1] ] 2

关于flutter - 如何使我的内容具有响应性并且不会溢出容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57373089/

相关文章:

dart - 如何在 Flutter 上将 DatePicker 上的可选日期设置为从现在起 3 天?

android - 单击按钮之前执行的 Flutter 代码

Dart 版本是 2.2.0,但我收到要求版本 >=2.2.0 的错误

list - 如何过滤和删除重复的 JSON 列表?

flutter - 如果 Flutter 中的有状态小部件可以实现同样的功能,为什么我们需要无状态小部件?

flutter - 如何解决 "RangeError (index): Invalid value: Only valid value is 0: 1"错误

dart - 如何在服务器上部署 flutter web?

flutter - 我希望 _scanQR() 函数在我的 flutter 应用程序在任何 UI 设置之前打开时执行

dart - 如何检查字符串是否包含阿拉伯字符dart flutter

firebase - flutter 和 Firebase : How can I put the documentID into the document field the moment I just create the document?