如何在Flutter中的Stack Widget中添加Multiple Floating按钮

时间:2018-06-13 14:06:34

标签: dart widget stack flutter

使用Stack Widget在另一个视图中查看一个视图。它的工作正常。现在我需要在屏幕底部左右两侧添加两个浮动按钮。我在右侧添加了一个按钮,但我不知道如何在左侧添加浮动按钮。简单如下图。

enter image description here

任何帮助都会明显

8 个答案:

答案 0 :(得分:11)

不要忘记为每个浮动操作按钮设置“ heroTag:null”。否则您将得到黑屏!

Stack(
  children: <Widget>[
    Align(
      alignment: Alignment.bottomLeft,
      child: FloatingActionButton(
                heroTag: null,
             ...),
    ),
    Align(
      alignment: Alignment.bottomRight,
      child: FloatingActionButton(
                heroTag: null,
             ...),
    ),
  ],
)

答案 1 :(得分:7)

您可以使用Align窗口小部件将FloatingActionButton's置于Stack

Stack(
  children: <Widget>[
    Align(
      alignment: Alignment.bottomLeft,
      child: FloatingActionButton(...),
    ),
    Align(
      alignment: Alignment.bottomRight,
      child: FloatingActionButton(...),
    ),
  ],
)

一个按钮对Alignment.bottomLeft属性使用常量alignment,另一个按钮Alignment.bottomRight使用path

答案 2 :(得分:3)

您还可以使用将location作为centerDocked的类似内容,以免出现怪异的左对齐。

floatingActionButtonLocation:
              FloatingActionButtonLocation.centerDocked,
          floatingActionButton: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                FloatingActionButton(
                  onPressed: () {},
                  child: Icon(Icons.navigate_before),
                ),
                FloatingActionButton(
                  onPressed: () {},
                  child: Icon(Icons.navigate_next),
                )
              ],
            ),
          )

答案 3 :(得分:3)

  floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
  floatingActionButton: Container(
    padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10.0),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        FloatingActionButton(
          onPressed: _someBackMethod,
          child: Icon(Icons.arrow_back),
        ),
        FloatingActionButton(
          onPressed: _someForwardMethod,
          child: Icon(Icons.arrow_forward),
        ),
      ],
    ),
  ),

enter image description here

答案 4 :(得分:0)

只需将添加为 Scafold 中的 floatingActionButton ,并设置位置 centerFloat

EX

Scaffold(
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      //store btn
      floatingActionButton: Container(
        child: Row(
          children: <Widget>[
            //add left Widget here with padding left
            Spacer(
              flex: 1,
            ),
            //add right Widget here with padding right
          ],
        ),
      ),
    );

答案 5 :(得分:0)

如果这棵树发生 子树中有多个共享相同标签的英雄。

floatingActionButton: Stack(
  children: <Widget>[
   Padding(
    padding: EdgeInsets.only(right: 70),
      child: Align(
        alignment: Alignment.bottomRight,
           child: FloatingActionButton.extended(
                  heroTag: "btn1",
                  // backgroundColor: Color(0XFF0D325E),
                  backgroundColor: Colors.red,
                  // child: Icon(Icons.refresh),
                  label: Text('Clear All Wifi'),
                  onPressed: () {
                    _sendMessage(all: 'Clear Wifi');
                  }),
            ),
          ),
          Align(
            alignment: Alignment.bottomRight,
            child: FloatingActionButton(
                heroTag: "btn2",
                backgroundColor: Color(0XFF0D325E),
                child: Icon(Icons.refresh),
                onPressed: () {
                  _sendMessage(all: 'GETALLWIFI');
                }),
          ),
        ],
      ),

答案 6 :(得分:0)

很聪明;)

Stack(
  children: [
    Container(...),
    Positioned(
      right: 15,
      bottom: 15,
      child: FloatingActionButton(
        heroTag: 'edit',
        onPressed: () {},
          child: Icon(Icons.edit),
      ),
      ),
      Positioned(
      left: 15,
      bottom: 15,
      child: FloatingActionButton(
        heroTag: 'delete',
        onPressed: () {},
          child: Icon(Icons.delete),
      ),
    ),
  ],
)

答案 7 :(得分:-1)

floatingActionButton: Stack(
      children: <Widget>[
        Padding(padding: EdgeInsets.only(left:31),
        child: Align(
          alignment: Alignment.bottomLeft,
          child: FloatingActionButton(
            onPressed: picker,
            child: Icon(Icons.camera_alt),),
        ),),

        Align(
          alignment: Alignment.bottomRight,
          child: FloatingActionButton(
            onPressed: picker2,
          child: Icon(Icons.add_photo_alternate),),
        ),
      ],
    )