android - Flutter - 如何让 SnackBar 不向上推动 FloatingActionButton?
问题描述
弹出时如何使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)
));
}
解决方案
您可以设置将显示上述其他小部件的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
));
}
有关更多信息,请参阅此链接。
推荐阅读
- c++ - 我是否需要通过锁来保护对 Boost MultiIndex 索引(索引本身)的访问?
- java - 消除浮点舍入错误的技巧?
- pygame - Pygame,我的圆圈在我将它存储在一个变量中后变成了一个矩形,我该如何防止这种情况发生
- javascript - 如果在表单提交时未选中收音机,则会发出警报
- vb.net - DataGridView 添加行
- c++ - 与多个绑定相结合的 ForwardIterator 迭代速度太快
- sql - 加入两个选择查询
- html - github.io 和 css 链接
- python - 大型数据集的 Numpy
- swift - UIScrollView contentOffset 在点击时重置