Flutter移动FloatingActionButton向上50像素

41

是否可能将floatingActionButton上移50像素?

我的应用程序中有一个floatingActionButton,使用了firebase_admob,广告横幅覆盖在floatingActionButton上面。

如何将floatingActionButton设置为距离屏幕底部50像素

根据floatingActionButton文档,我似乎找不到如何定位按钮的方法。

2个回答

95

将您的FloatingActionButton包装在一个Padding中,并添加所需的大小:

    floatingActionButton: Padding(
            padding: const EdgeInsets.only(bottom: 50.0),
            child: FloatingActionButton(
              child: Icon(Icons.remove),
              onPressed: () => null,
            ),
          ), 

1
谢谢您的帮助。这个更好了,尽管键盘的显示使按钮面板距离显示的键盘有50的距离。 - Phuthib
你可以检测键盘是否打开,然后将填充值更改为0。https://dev59.com/pVYN5IYBdhLWcg3wNFp4 - diegoveloper
1
在你的 Scaffold 中将 resizeToAvoidBottomInset 设置为 false,这样当键盘弹出时它就不会跳动。 - Tarek Badr

12

很简单。

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(),
      floatingActionButton: Align(
          child: FloatingActionButton(onPressed: null),
          alignment: Alignment(1, 0.7)),
    );
  }
}

在Flutter中,万物皆是Widget,因此使用对齐方式(Alignment)。


这确实有助于清除广告横幅,但是当键盘显示时,floatingActionButton会跳到顶部。希望它保持底部50像素的填充。 - Phuthib
修复中...请稍等。 - Doc
嗯,看起来它要么是使用填充要么是使用对齐方式跳动。你能分享一下代码或UI吗? - Doc
2
跳转发生在 Align 和 Padding 两种情况下,正如 diegoveloper 建议的那样。然而,使用 Padding 时的行为要好得多,因此我将将其作为正确答案奖励。感谢您的帮助。 - Phuthib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接