【问题标题】:FlatButton with Text ellipsis overflowed带有文本省略号的 FlatButton 溢出
【发布时间】:2020-09-15 04:44:26
【问题描述】:

当 FlatButton 的文本溢出时,我尝试使用省略号。

它适用于普通的 FlatButton,但不适用于 FlatButton.icon。我将不胜感激。

void main() => runApp(Test());

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        // bottomNavigationBar: BottomAppBar(child: widgetOk()),
        bottomNavigationBar: BottomAppBar(child: widgetNotOk()),
      ),
    );
  }
}

Widget widgetOk() {
  return Row(
    children: <Widget>[
      Flexible(
        child: FlatButton(
          child: Text('0123456789' * 10, overflow: TextOverflow.ellipsis),
          onPressed: () {},
        ),
      ),
      Flexible(
        child: FlatButton(
          child: Text('0123456789' * 10, overflow: TextOverflow.ellipsis),
          onPressed: () {},
        ),
      ),
    ],
  );
}

Widget widgetNotOk() {
  return Row(
    children: <Widget>[
      Flexible(
        child: FlatButton.icon(
          icon: Icon(Icons.bug_report),
          label: Text('0123456789' * 10, overflow: TextOverflow.ellipsis),
          onPressed: () {},
        ),
      ),
      Flexible(
        child: FlatButton.icon(
          icon: Icon(Icons.bug_report),
          label: Text('0123456789' * 10, overflow: TextOverflow.ellipsis),
          onPressed: () {},
        ),
      ),
    ],
  );
}

【问题讨论】:

    标签: flutter layout flutter-layout ellipsis


    【解决方案1】:

    您可以将labelFlexible 包裹起来,以查看ellipsis 生效。

    下面的工作代码:

    Flexible(
            child: FlatButton.icon(
              icon: Icon(Icons.bug_report),
              label: Flexible(
                  child: Text('0123456789' * 10, overflow: TextOverflow.ellipsis)),
              onPressed: () {},
            ),
          ),
          Flexible(
            child: FlatButton.icon(
              icon: Icon(Icons.bug_report),
              label: Flexible(
                child: Text('0123456789' * 10, overflow: TextOverflow.ellipsis),
              ),
              onPressed: () {},
            ),
          ),
    

    【讨论】:

    • 谢谢,它正在工作。但是我不明白为什么每个文本都需要 2 个灵活变量。你能解释一下吗?
    • 外部Flexible 是告诉Row 只为其所有子级占用可用空间。因为它的一个孩子是FlatButton.icon,它本身是两个小部件Texticon 的组合。根据官方文档,图标和文本在开始处填充 12 个像素,在结束处填充 16 个像素,它们之间有 8 个像素的间隙。因此,为了使较大的文本能够正确呈现并同时容纳icon,使用Flexible。希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2019-11-25
    • 2013-12-31
    • 1970-01-01
    • 2019-06-19
    • 2013-04-01
    相关资源
    最近更新 更多