【问题标题】:Flutter align button to bottom of Drawer颤动对齐按钮到抽屉底部
【发布时间】:2018-05-22 15:08:19
【问题描述】:

我试图让一个小部件与我的 NavDrawer 的底部对齐,同时仍然在 Drawer 的顶部保留一个 DrawerHeader 和一个列表。这是我正在尝试的:

drawer: new Drawer(
    child: new Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
        new Text('Top'),
        new Align(
          alignment: FractionalOffset.bottomCenter,
          child: new Text('Bottom'),
        ),
      ],
    ),
  ),

底部文本应该与抽屉底部对齐,但不是!

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您需要将Align 小部件包装在Expanded 中。

    drawer: Drawer(
      child: Column(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Text('Top'),
          Expanded(
            child: Align(
              alignment: Alignment.bottomCenter,
              child: Text('Bottom'),
            ),
          ),
        ],
      ),
    ),
    

    【讨论】:

    • 如何将按钮对齐到底部,但将其他孩子对齐到中心(而不是顶部)? Mb 更好地使用堆栈?
    • 您能解释一下“扩展”的工作原理吗?每次我使用它时,我都会遇到溢出错误或Unhandled Exception: Cannot hit test a render box that has never been laid out.
    • 这个答案应该被审查。将 ListView() 放在 Text("Top") 所在的位置时,我得到了“hasSize()”断言。你必须反过来做。展开 ListView,然后展开底部文本。就像 Samuel Huff 的回答 stackoverflow.com/a/54193080/8666381
    【解决方案2】:

    编辑:

    多年过去了,有一个更简单的解决方案:

    return Drawer(
      child: Column(
        children: [
          ListView(), // <-- Whatever actual content you want goes here
          Spacer(), // <-- This will fill up any free-space
          // Everything from here down is bottom aligned in the drawer
          Divider(),
          ListTile(
            title: Text('Settings'),
            leading: Icon(Icons.settings),
          ),
          ListTile(
            title: Text('Help and Feedback'),
            leading: Icon(Icons.help),
          ),
        ]
    );
    

    聚会有点晚了,但这是我对这个问题的解决方案:

      @override
      Widget build(BuildContext context) {
        return Drawer(
          // column holds all the widgets in the drawer
          child: Column(
            children: <Widget>[
              Expanded(
                // ListView contains a group of widgets that scroll inside the drawer
                child: ListView(
                  children: <Widget>[
                    UserAccountsDrawerHeader(),
                    Text('In list view'),
                    Text('In list view too'),
                  ],
                ),
              ),
              // This container holds the align
              Container(
                  // This align moves the children to the bottom
                  child: Align(
                      alignment: FractionalOffset.bottomCenter,
                      // This container holds all the children that will be aligned
                      // on the bottom and should not scroll with the above ListView
                      child: Container(
                          child: Column(
                        children: <Widget>[
                          Divider(),
                          ListTile(
                              leading: Icon(Icons.settings),
                              title: Text('Settings')),
                          ListTile(
                              leading: Icon(Icons.help),
                              title: Text('Help and Feedback'))
                        ],
                      )
                    )
                  )
                )
            ],
          ),
        );
      }
    

    这会产生下面的输出,其中 UserAccountDrawerHeader 和文本项可以在抽屉内滚动,但 Divider 和两个 ListTiles 在抽屉底部保持静态。

    【讨论】:

      【解决方案3】:

      看看你的代码有什么问题你已经将一个列作为子项添加到抽屉中,所以无论你添加什么都是垂直放置的,列的高度默认情况下缩小到其子项的高度,并且随着子项的增加而变大得到,所以在 Column 中添加 Align 是没有意义的

      更简单的解决方案是使用展开的小部件,该小部件采用剩余的空间外观我使用了一个列并在展开的小部件上方和下方添加了一个小部件。

            Drawer(
                  elevation: 1.5,
                  child: Column(children: <Widget>[
                    DrawerHeader(
                        decoration: BoxDecoration(
                      color: Colors.redAccent,
                    )),
                    Expanded(
                        child: ListView(
                      padding: EdgeInsets.zero,
                      children: <Widget>[
                        ListTile(
                          title: Text('My Cart'),
                          leading: Icon(Icons.shopping_cart),
                          onTap: () {},
                        ),
                        ListTile(
                          title: Text('My Orders'),
                          leading: Icon(Icons.add_shopping_cart),
                          onTap: () {},
                        ),
                        ListTile(
                            title: Text('Logout'),
                            leading: Icon(Icons.exit_to_app),
                            onTap: () {})
                      ],
                    )),
                    Container(
                      color: Colors.black,
                      width: double.infinity,
                      height: 0.1,
                    ),
                    Container(
                        padding: EdgeInsets.all(10),
                        height: 100,
                        child: Text("V1.0.0",style: TextStyle(fontWeight: FontWeight.bold),)),
                  ])),
      

      【讨论】:

        【解决方案4】:

        一种简单的方法是使用Spacer(),例如:

        Scaffold(
          drawer: Drawer(
            child: Column(
              children: <Widget>[
                Text('Top'),
                Spacer(), // use this
                Text('Bottom'),
              ],
            ),
          )
        )
        

        【讨论】:

          【解决方案5】:

          这是我在抽屉末端带有图标的垂直Row 的解决方案。

          @override
            Widget build(BuildContext context) {
              return Drawer(
                child: Column(
                  children: <Widget>[
                    Expanded(
                      child: ListView(
                        children: <Widget>[
                          DrawerHeader(
                            padding: const EdgeInsets.all(7),
                            decoration: BoxDecoration(
                              color: AppColors.menuHeaderColor,
                            ),
                            child: buildHeader(),
                          ),
                          AccountDrawerRow(),
                          ListTile(
                            leading: Icon(Icons.directions_car),
                            title: Text(translations.button.vehicles),
                          ),
                          ListTile(
                            leading: Icon(Icons.calendar_today),
                            title: Text(translations.button.appointments,),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      child: Align(
                        alignment: FractionalOffset.bottomCenter,
                        child: Container(
                          padding: EdgeInsets.all(15.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: <Widget>[
                              InkWell(
                                  onTap: () => Navigator.of(context).push(MaterialPageRoute(
                                      builder: (context) => SettingsPage())),
                                  child: Icon(Icons.settings)),
                              Icon(Icons.help),
                              Icon(Icons.info),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              );
            }
          

          【讨论】:

            【解决方案6】:

            我会将它排成一排,并使用crossAxisAlignment: CrossAxisAlignment.baseline 将所有内容对齐到底部

            Row(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.baseline,
                          children: <Widget>[
                            Text(
                              '12.00',
                              style: Theme.of(context).textTheme.headline2,
                              textAlign: TextAlign.start,
                            ),
                            Text(
                              'USD',
                              style: Theme.of(context).textTheme.bodyText2,
                              textAlign: TextAlign.start,
                            ),
                          ]),
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-11-26
              • 2021-02-17
              • 1970-01-01
              • 1970-01-01
              • 2020-06-23
              相关资源
              最近更新 更多