繁体   English   中英

如何在 Flutter 的 Stack Widget 中添加多个浮动按钮

[英]How to add Multiple Floating button in Stack Widget in Flutter

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

在此处输入图像描述

任何帮助都会很明显

您可以使用Align小部件在Stack定位您的FloatingActionButton

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

一个按钮使用常量Alignment.bottomLeft作为它的alignment属性,另一个分别使用Alignment.bottomRight

您也可以使用 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),
                )
              ],
            ),
          )

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

Stack(
  children: <Widget>[
    Align(
      alignment: Alignment.bottomLeft,
      child: FloatingActionButton(
                heroTag: null,
             ...),
    ),
    Align(
      alignment: Alignment.bottomRight,
      child: FloatingActionButton(
                heroTag: null,
             ...),
    ),
  ],
)
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),),
        ),
      ],
    )
  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),
        ),
      ],
    ),
  ),

在此处输入图片说明

只需在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
          ],
        ),
      ),
    );

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

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');
                }),
          ),
        ],
      ),

就这么聪明 ;)

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),
      ),
    ),
  ],
)
Scaffold(
  floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  floatingActionButton: Padding(
    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 12),
    child: Row(
      children: [
        FloatingActionButton(
          onPressed: () {},
          backgroundColor: Colors.red,
          child: Icon(Icons.remove),
        ),
        Spacer(),
        FloatingActionButton(
          onPressed: () {},
          child: Icon(Icons.add),
        ),
      ],
    ),
  ),
);

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM