如何在一行中放置两个按钮?扑

How to fit two button within a row ? Flutter

我是 flutter 的新手,我想知道是否有人可以帮助我处理我的按钮行。他们都在行外工作,但不在行内。我附上了下面的代码,如果您能提供帮助或需要更多信息,请告诉我。

 Expanded(
              child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                IconButton(
                icon: Icon(Icons.edit),
                )
                      floatingActionButton: FloatingActionButton(
                        child: Icon(Icons.add),
                        onPressed: () async {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => StudyPage(
                                title: 'Add a study',
                                selected: 0,
                              ),
                            ),
                          );
                        },
                      ),
        ],
            )
          ],
            ),

据我所知,你可以试试这个

Row(children:[
    Expanded(
      child:
            IconButton(
                icon: Icon(Icons.edit),
                )),
     Expanded(
      child:
            IconButton(
                icon: Icon(Icons.edit),
                )),
     
    ]);

如果解决了您的问题,请采纳为答案,以便其他人得到帮助

首先。 Expanded 仅当放在 Colum/Row children.

中时有效
  • Expanded 将占用父 Colum/Row 的所有 space 并尽可能扩展 Colum/Row 大小。
  • 如果您希望 Colum/Row 达到最大尺寸,请尝试将 mainAxisSize: MainAxisSize.max 添加到 Colum/Row

其次。当您将 floatingActionButton 放置在 Colum/Row 个子项中时,您的代码看起来有错误,它不是一个 Widget,它是 Scaffold 的一个道具。尝试用 IconButton/ElevatedButton/etc... 等有效按钮替换它。你的 IconButton 也缺少 onPressed

示例:

            Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                Expanded( // Place `Expanded` inside `Row`
                  child: ElevatedButton(
                    child: Text('First button'),
                    onPressed: () {}, // Every button need a callback
                  ),
                ),
                Expanded( // Place 2 `Expanded` mean: they try to get maximum size and they will have same size
                  child: ElevatedButton(
                    child: Text('Second button'),
                    onPressed: () {},
                  ),
                ),
              ],
            )

我稍微修改了您的 (Row) 代码如下,现在可以使用了。

Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[

    IconButton(
      icon: const Icon(Icons.edit),
      onPressed: () {},
    ),

        FloatingActionButton(
          child: const Icon(Icons.add),
          onPressed: () async {
             Navigator.push(
               context,
               MaterialPageRoute(
                 builder: (context) => StudyPage(
                   title: 'Add a study',
                   selected: 0,
                 ),
               ),
             );
          },
        ),


  ]),