【问题标题】:Flutter: cutting a row in "half"Flutter:将一行切成“一半”
【发布时间】:2019-06-05 13:37:59
【问题描述】:

我一直在玩弄颤动的列表,一切都很好。我开始明白整个事情的逻辑了。

现在我想做一个这样的简单布局:

我的问题是我找不到制作第一行的方法。我试图告诉 Row 让它的两个孩子拿一半,不要再少了,但我真的没有找到办法。 (里面的东西最终将是按钮。)我尝试了几件事无济于事。这是我开始的布局:

Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: Column(children: <Widget>[
        Padding(
          padding: EdgeInsets.only(top: 50.0),
        ),
        Row(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(
                  top: buttonPaddingTop,
                  left: buttonPaddingSide,
                  right: buttonPaddingSide),
              child: ButtonTheme(
                child: FlatButton(
                  onPressed: () {},
                  color: Colors.grey,
                  child: const Text('LARGE TEXT',
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontSize: buttonFontSize)),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(
                  top: buttonPaddingTop,
                  left: buttonPaddingSide,
                  right: buttonPaddingSide),
              child: ButtonTheme(
                child: FlatButton(
                  onPressed: () {},
                  color: Colors.grey,
                  child: const Text('LARGE TEXT',
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontSize: buttonFontSize)),
                ),
              ),
            ),
          ],
        ),
      ]),
    ),
  );
}

我应该继续这样尝试,还是应该尝试除 Row 之外的其他对象来安排布局?

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    Expanded 在这种情况下水平占用尽可能多的空间。这意味着如果您在 Row 中使用 2 个 Expanded 小部件,空间将被分成两半。

    试试这个:

    Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: Text('first half'),
              ),
              Expanded(
                child: Text('second half'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: Text('first half'),
              ),
              Expanded(
                child: Text('second half'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: Text('full width'),
              )
            ],
          ),
        ],
      )
    

    确保您阅读了有关 Flutter 小部件的更多信息,例如观看此视频(以及本系列中的其他视频): https://www.youtube.com/watch?v=_rnZaagadyo

    【讨论】:

    • 感谢您的链接,根据我使用的扩展它应该可以工作,我会尝试的。
    • @moo 我添加了一个更完整的答案,所以 2 行一半大小,第三个全宽。
    • 最糟糕的是,我在另一个上下文中使用了扩展,同时做一个列表行,开头有一些东西,结尾有一些东西,中间有一个 Expanded 以占用所有空间并能够使用文本溢出 ^^
    • @moo 没关系,每个人都会这样:)
    猜你喜欢
    • 2021-05-02
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多