【问题标题】:Flutter align widgets in an expanded container, to the top left and bottom right?Flutter将扩展容器中的小部件对齐到左上角和右下角?
【发布时间】:2019-01-16 02:12:13
【问题描述】:

我的应用程序中有一个布局,其中包含一些文本和一个时间戳。我希望时间戳(图片中的时钟图标)位于展开小部件的左下角,而文本(图片中的“what”字符串)应从右上角开始。

代表:

|text|              |
|    -----------    |
|    empty space    |
|    -----------    |
|              |time|

目前,我在下面管理的内容使它们与极左和极右对齐,但它们似乎受到 Column 小部件的限制,因此与小部件的中心对齐。而且我似乎无法扩展以在另一个扩展中工作。

小部件代码:

return new Container(
      child: new Row(
        children: <Widget>[
          new Column(
            children: <Widget>[
              new MaterialButton(
                minWidth: 60.0,
                child: new Icon(Icons.keyboard_arrow_up),
                onPressed: () => null,
              ),
              new Container(
                padding: new EdgeInsets.all(5.0),
                child: new Text("1231"),
              ),
              new MaterialButton(
                minWidth: 60.0,
                child: new Icon(Icons.keyboard_arrow_down),
                onPressed: () => null,
              ),
            ],
          ),
          new Expanded(
            // child: new Container(
            // padding: EdgeInsets.all(10.0),
            child: new Column(
              // mainAxisAlignment: MainAxisAlignment.end,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Align(
                  alignment: Alignment.topLeft,
                  child: new Text("what"),
                ),

                // "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."),
                new Align(
                  // padding: new EdgeInsets.all(10.0),
                  alignment: Alignment.bottomRight,
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      new Container(
                        padding: new EdgeInsets.all(2.0),
                        child: new Icon(
                          Icons.access_time,
                          size: 12.0,
                        ),
                      ),
                      new Container(
                        padding: new EdgeInsets.all(2.0),
                        child: new Text("1 hr"),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            // ),
          ),
          new Column(
            children: <Widget>[
              new MaterialButton(
                minWidth: 60.0,
                child: new Icon(Icons.save, size: 20.0),
                onPressed: () => null,
              ),
              new Container(
                padding: new EdgeInsets.all(5.0),
                child: new Text("8"),
              ),
              new MaterialButton(
                minWidth: 60.0,
                child: new Icon(Icons.comment, size: 20.0),
                onPressed: () => null,
              ),
            ],
          ),
        ],
      ),
    );
  }

【问题讨论】:

  • 你能再放一张图片吗?我看不到它
  • @diegoveloper 这是一个链接:imgur.com/a/QwCBhpY

标签: dart flutter


【解决方案1】:

尝试使用 alignment: Alignment.bottomRight 作为 Container 类的命名参数

【讨论】:

    【解决方案2】:

    好的,所以我修改了您的Widget,并在容器中添加了一些颜色以非常容易地跟踪更改:

            double maxHeight = 130.0;
    
        Widget _buildLeft() {
          return Container(
            height: maxHeight,
            color: Colors.red,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new MaterialButton(
                  minWidth: 60.0,
                  child: new Icon(Icons.keyboard_arrow_up),
                  onPressed: () => null,
                ),
                new Container(
                  padding: new EdgeInsets.all(5.0),
                  child: new Text("1231"),
                ),
                new MaterialButton(
                  minWidth: 60.0,
                  child: new Icon(Icons.keyboard_arrow_down),
                  onPressed: () => null,
                ),
              ],
            ),
          );
        }
    
        Widget _buildCenter() {
          return Expanded(
            child: Container(
              height: maxHeight,
              color: Colors.blue,
              child: Stack(
                children: <Widget>[
                  Positioned(
                    child: new Text("what"),
                    top: 0.0,
                    left: 0.0,
                  ),
                  // "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."),
                  Positioned(
                    bottom: 0.0,
                    right: 0.0,
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        new Container(
                          padding: new EdgeInsets.all(2.0),
                          child: new Icon(
                            Icons.access_time,
                            size: 12.0,
                          ),
                        ),
                        new Container(
                          padding: new EdgeInsets.all(2.0),
                          child: new Text("1 hr"),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        }
    
        Widget _buildRight() {
          return Container(
            height: maxHeight,
            color: Colors.green,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new MaterialButton(
                  minWidth: 60.0,
                  child: new Icon(Icons.save, size: 20.0),
                  onPressed: () => null,
                ),
                new Container(
                  padding: new EdgeInsets.all(5.0),
                  child: new Text("8"),
                ),
                new MaterialButton(
                  minWidth: 60.0,
                  child: new Icon(Icons.comment, size: 20.0),
                  onPressed: () => null,
                ),
              ],
            ),
          );
        }
    
        @override
        Widget build(BuildContext context) {
          return new Container(
            child: new Row(
              children: <Widget>[
                _buildLeft(),
                _buildCenter(),
                _buildRight(),
              ],
            ),
          );
        }
    

    【讨论】:

    • 感谢您的回复。但是结果似乎和我原来的代码一样。我希望扩展的小部件(蓝色)填充整个中间部分的长度和宽度,并在左上角显示文本,在左下角显示时间。
    • 谢谢,这有点符合我的要求,但是有没有办法根据文本大小使高度动态化?目前,文本也没有像以前那样环绕。
    猜你喜欢
    • 2017-05-13
    • 2020-07-28
    • 2019-08-11
    • 1970-01-01
    • 2017-10-25
    • 2014-05-17
    • 2013-12-22
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多