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)
您可以设置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)
有关更多信息,请参阅此链接。
只需使用多个Scaffolds 即可。用外层包裹内层。用inner来放置属于它的所有东西,body,appBar,bottomNavigationBar,floatingActionButton等等。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)
| 归档时间: |
|
| 查看次数: |
3787 次 |
| 最近记录: |