代码之家  ›  专栏  ›  技术社区  ›  ad_on_is

颤振:将列作为子级的扁平按钮放在Appbar Breaks布局中

  •  1
  • ad_on_is  · 技术社区  · 8 年前

    我有以下代码可以在我的应用程序中创建BottomAppBar,这很好。

    class MyBottomAppBar extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        final List<Widget> rowContents = <Widget> [
          new FlatButton(
              onPressed: () {
    
              },
              padding: EdgeInsets.all(10.0),
              child: new Row(
                children: <Widget>[
                  new Icon(Icons.menu),
                  new Text("Feed")
                ],
              ),
          ),
          const Expanded(child: const SizedBox()),
          new FlatButton(
            onPressed: () {
    
            },
            padding: EdgeInsets.all(10.0),
            child: new Row(
              children: <Widget>[
                new Icon(Icons.people),
                new Text("Profile")
              ],
            ),
          ),
        ];
    
    
    
        return new BottomAppBar(
          hasNotch: true,
          child: new Row(children: rowContents),
        );
      }
    }
    

    enter image description here

    我想用一个 Column() 作为一个孩子 FlatButton() ,以便 Text() 如下所示 Icon() 就在它旁边。

    当我改变的时候 Row() 到 列() ,我得到以下结果:

    enter image description here

    我在这里做错什么了?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Mans    8 年前

    默认情况下 Column 小部件填充所有可能的空间。你可以通过它 mainAxisSize 属于 mainAxisSize.min 告诉小部件占用尽可能少的空间。

    这是代码的修订版本,可以按您的需要工作:

    class MyBottomAppBar extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        final List<Widget> rowContents = <Widget> [
          new FlatButton(
            onPressed: () {
    
            },
            padding: EdgeInsets.all(10.0),
            child: new Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new Icon(Icons.menu),
                new Text("Feed")
              ],
            ),
          ),
          const Expanded(child: const SizedBox()),
          new FlatButton(
            onPressed: () {
    
            },
            padding: EdgeInsets.all(10.0),
            child: new Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new Icon(Icons.people),
                new Text("Profile")
              ],
            ),
          ),
        ];
    
    
    
        return new BottomAppBar(
          hasNotch: true,
          child: new Row(children: rowContents),
        );
      }
    }