我的Ffltter应用程序中有一个包含长文本字符串的文本小部件.它被放置在具有固定宽度的容器中.默认情况下,文本字符串换行为多行.

但是,当我try 将文本小部件插入到Row小部件中时,文本字符串突然切换到一行,并在右侧被裁剪.

当文本小部件位于一行内时,保持其原始多行行为的简单方法是什么?

multi-line switches to single line

以下是我一直使用的代码:

var container = new Container (
  child: new Row (
    children: [
      new Icon (Icons.navigate_before),
      new Text ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."),
      new Icon (Icons.navigate_next),
    ],
  ),
  decoration: new BoxDecoration (
    backgroundColor: Colors.grey[300],
  ),
  width: 400.0,
);

推荐答案

需要知道的最重要的一点是,列或行中的子窗口小部件占用的空间与它们需要的空间相同.

子元素本身不受行/列的主轴大小(宽度/高度)的限制,如果它们较大,则会溢出.(Hence, why you see the red overflow indicator on the right)

您的行为不是由文本小部件造成的,而是由布局本身造成的.您需要将文本小部件的宽度限制为Row布局中两个图标之间的剩余宽度.

通过将子元素包装在Expanded中,可以明确描述每个子小部件在行中的大小.

Expanded将根据行/列中的其他Expandeds和剩余空间调整自身的大小.

让我们看一个示例,以了解扩展在行或列中是如何工作的:

var row = new Container(
  width: 400.0,
  height: 100.0,
  child: new Row(
    children: [
      // Red Bar
      // This will take up 50dp always, since it is not wrapped by a Expanded
      new Container(
        width: 50.0,
        decoration: new BoxDecoration(
          backgroundColor: Colors.red[500],
        )
      ),
      // Green Bar
      // This will take up 1/3 of the remaining space (1/3 of 350dp)
      new Expanded(
        flex: 1,
        child: new Container(
          decoration: new BoxDecoration(
            backgroundColor: Colors.green[500],
          )
        ),
      ),
      // Blue Bar
      // This will take up 2/3 of the remaining space (2/3 of 350dp)
      new Expanded(
        flex: 2,
        child: new Container(
          decoration: new BoxDecoration(
            backgroundColor: Colors.blue[500],
          )
        ),
      )
    ]
  ),
);

其结果如下:

Code Output

让我们来分析一下这一点:

  1. 红色条always将是50dp长的,因为它没有被包裹在展开的.即使它比父容器大,它的宽度也将为50dp.

  2. 现在我们有350dp(400-350)在绿色条和蓝色条之间分配.

  3. 我们现在要做的是将每个展开项的所有剩余弹性值相加,结果是:1[绿色]+2[蓝色]=3

  4. 现在,根据展开的弹性值和所有展开的累计弹性值,每个展开的空间将是剩余空间的比例:绿色=1/3,蓝色=2/3.

  5. 因此,绿色条的宽度为1/3*350=116.67dp

  6. 蓝色条的宽度为2/3*350=233.33dp

回到你最初的问题:你需要做的就是把你的文本小部件包装在一个扩展的文件中.默认情况下,Expandeds的弹性值为1.由于行中只展开了一个(1/1=100%),因此此文本将占用行中剩余的空间.

解决方案:

var container = new Container (
  child: new Row (
    children: [
      new Icon (Icons.navigate_before),
      new Expanded(
        child: new Text ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."),
      ),      
      new Icon (Icons.navigate_next),
    ],
  ),
  decoration: new BoxDecoration (
    backgroundColor: Colors.grey[300],
  ),
  width: 400.0,
);

Dart相关问答推荐

将数据库提供程序导入另一个文件提供程序 dart

带有字符串键的 Dart Map,与忽略大小写进行比较

在Flutter中在 initstate() 之前调用了dependOnInheritedElement()

MappedListIterable 不是子类型

从 Future 实例中获取值

如何将指针事件发送到堆栈中的低级子级

ProcessException:进程C:\..\myapp\android\gradlew.bat异常退出:

Flutter - 如何自动启用 AnimatedOpacity?

如何使用我的 dart 包私有而不在 pub dart lang 上显示?

如何在DartPad中导入库?

在调试和生产模式之间切换

如何创建一个对话框,能够接受文本输入并在Flutter中显示结果?

为什么这个文件会自动创建自己 generate_plugin_registrant.dart

如何在Flatter dart中使用相同的元素初始化列表?

使用Flatter向Cloud Firestore添加对象

可选参数的默认值

用 Dart 模式替换静态继承

如何在 Dart 中获取字符串的字节数?

如何在 Dart 中获取文件名?

如何在 Dart 中生成唯一 id