Flutter - 如何让 SnackBar 不向上推 FloatingActionButton?

jer*_*nho 1 android floating-action-button android-snackbar flutter

弹出时如何使Snackbar重叠FloatingActionButton而不是向上推?我附上了我的简化代码以供参考。先感谢您。

class Screen extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => ScreenState();
}

class ScreenState extends State<Screen>{

  BuildContext context;

  @override
  Widget build(BuildContext context) => Scaffold(
    floatingActionButton: FloatingActionButton(
      onPressed: action,
    ),
    body: Builder(
      builder : (BuildContext context){
        this.context = context;
        return Container();
      }
    )
  );

  action() => Scaffold.of(context).showSnackBar(SnackBar(
    duration: Duration(milliseconds : 1000),
    content: Container(height: 10)
  ));
}
Run Code Online (Sandbox Code Playgroud)

Sid*_*kar 8

您可以设置behavior的财产SnackBar,以SnackBarBehavior.floating将显示Snackbar上述其他部件。

这应该这样做 -

class Screen extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => ScreenState();
}

class ScreenState extends State<Screen>{

  BuildContext context;

  @override
  Widget build(BuildContext context) => Scaffold(
    floatingActionButton: FloatingActionButton(
      onPressed: action,
    ),
    body: Builder(
      builder : (BuildContext context){
        this.context = context;
        return Container();
      }
    )
  );

  action() => Scaffold.of(context).showSnackBar(SnackBar(
    duration: Duration(milliseconds : 1000),
    content: Container(height: 10)
    behavior: SnackBarBehavior.floating, // Add this line
  ));
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅链接。


jer*_*nho 2

只需使用多个Scaffolds 即可。用外层包裹内层。用inner来放置属于它的所有东西,bodyappBarbottomNavigationBarfloatingActionButton等等。showSnackBar从外地body打来的电话BuildContext。如果有的话,drawer从内到外移动,使其保持在SnackBar.

如果您喜欢弹出SnackBar经典吐司,请参阅 Siddharth Patankar 的回答。也谢谢你。

下面是我的答案的简化代码。

  @override
  Widget build(BuildContext context) => Scaffold(
    drawer : Drawer(child: Container()),
    body: Builder(
      builder : (BuildContext context){
        this.context = context;
        return Scaffold(
          body: Container(),
          floatingActionButton: FloatingActionButton(
            onPressed: () => Scaffold.of(context).showSnackBar(SnackBar(
              duration: Duration(milliseconds : 1000),
              content: Container(height: 30),
            )),
          ),
        );
      }
    )
  );
Run Code Online (Sandbox Code Playgroud)