flutter - 是否可以向停靠的浮动操作按钮添加填充或边距?
问题描述
如下图所示,我的按钮在靠近屏幕边缘时感觉不舒服。有没有办法在保持缺口外观的同时将按钮移动到远离屏幕边缘的位置?如果我向按钮添加填充,它会正确移动按钮,但槽口会弄乱..
参考代码:
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
child: Container(
height: 70.0,
),
),
floatingActionButton: Container(
child: FloatingActionButton(
onPressed: null,
child: Icon(Icons.menu),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
解决方案
我有 x 范围的硬编码 100,您可以根据您的要求进行修改
步骤1:
static const double fabIconHeight= 50.0;
@override
Widget build(BuildContext context) {
return SafeArea(
child: LayoutBuilder(
builder: (context, constraint) {
return Scaffold(
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
child: Container(
height: 70.0,
)
),
floatingActionButton: new FloatingActionButton(
child: Icon(Icons.dehaze),
onPressed: () {},
),
floatingActionButtonLocation: CustomFloatingActionButtonLocation(
constraint.maxWidth - 100,
constraint.maxHeight - fabIconHeight - (fabIconHeight / 2)),
);
},
),
);
}
第2步:
class CustomFloatingActionButtonLocation implements FloatingActionButtonLocation {
final double x;
final double y;
const CustomFloatingActionButtonLocation(this.x, this.y);
@override
Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
return Offset(x, y);
}
}
或者
class CustomFloatingActionButtonLocation
implements FloatingActionButtonLocation {
static const double fabIconHeight = 50.0;
const CustomFloatingActionButtonLocation();
@override
Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
return Offset(
scaffoldGeometry.scaffoldSize.width - 100,
scaffoldGeometry.scaffoldSize.height - (70.0 / 2) -
fabIconHeight -
(fabIconHeight / 2));
}
}
推荐阅读
- r - 在 R 中编辑轴刻度的特定元素 - ggplot
- netlogo - 未设置全局变量的代码如何不返回“nobody”?
- xamarin - 使用要领获取 uikit 一致性错误
- sql-server - 使用来自不同数据库和服务器的 sys.columns
- printf - 如何将实际变量 n 放入 printf("%nd",var) 而不是 C 中的任何整数?
- scala - 在 Scala 中优雅地有条件地应用方法
- reactjs - 将数组道具传递给组件
- python - 是否有关于使用哪些版本的外部库(libfreetype 等)来生成 Pillow 的 PyPi 版本的文档?
- python - 如何使用 Scrapy 获取亚马逊搜索的所有结果?
- c++ - 有没有办法打印 2 个不同字体大小的文本?C++