简体   繁体   English

Flutter:左对齐扩展的小部件

[英]Flutter: Left align expanded widgets

I am trying to get some text left aligned and fitting to a single row, with the last part being kept in it's original form.我试图让一些文本左对齐并适合单行,最后一部分保持原始形式。 Something like the following:类似于以下内容:

// Original
A really long name which would wrap / Yesterday
// Output
A really long name... / Yesterday

The code below works for the above example, but:下面的代码适用于上面的例子,但是:

// Original
Short Name / Yesterday
// Required Output (all left aligned)
Short Name / Yesterday
// Actual Output
Short Name            / Yesterday

Is there a way to achieve that?有没有办法实现这一目标? The code below is correctly shortening the left-hand component but alignment is off.下面的代码正确地缩短了左侧组件,但对齐已关闭。

      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        ListTile(
          leading: CircleAvatar(
            backgroundColor: Colors.blue,
            child: Text('AB'),
          ),
          title: Text('Some text',
              overflow: TextOverflow.ellipsis, maxLines: 1),
          subtitle: Row(children: <Widget>[
            Container(
                child: Text('Person Name,
                    overflow: TextOverflow.ellipsis, maxLines: 1)),
            Expanded(
                flex: 1,
                child:
                    Text(' / Yesterday'))
          ]),
        ),
      ]);

Just add the mainAxisAlignment property and set it to mainAxisAlignment.spaceBetween.只需添加 mainAxisAlignment 属性并将其设置为 mainAxisAlignment.spaceBetween。 This will space out your container and Text.这将使您的容器和文本隔开。 Just remove the flex property from the expanded widget.只需从展开的小部件中删除 flex 属性。 It already defaults to 1.它已经默认为 1。

I met the same problem and finally I found a solution.我遇到了同样的问题,最后我找到了解决方案。 Put your "A really long name which would wrap" text into Flexible() instead of Expanded().将您的“一个非常长的名称可以包装”文本放入 Flexible() 而不是 Expanded() 中。 This will make your text get free spaces only when needed.这将使您的文本仅在需要时获得可用空间。 So when your text content is not long enough, it won't take all spaces.因此,当您的文本内容不够长时,它不会占用所有空格。

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

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