flutter - 定义floatingActionButtonLocation时如何使用堆栈添加FAB?
问题描述
在我的应用程序中,我定义了一个属性floatingActionButtonLocation
设置为FloatingActionButtonLocation.centerDocked
. 我已经定义了一个底部的应用栏,其中包含 centerDocked FAB。我还想使用一个Stack
小部件将两个额外的 FAB 放置在屏幕的右上角。它应该看起来与此类似 -预期结果
但是当我尝试使用 aStack
时,会显示底部应用栏中存在的 FAB,但堆栈小部件下的两个 FAB 是不可见的。
我定义的代码Stack
return new Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: Stack(
children: <Widget>[
Positioned(
// the below values for stack are experimental at the moment
left: MediaQuery.of(context).copyWith().size.width * 0.60,
right: MediaQuery.of(context).copyWith().size.width * 0.01,
// top: MediaQuery.of(context).copyWith().size.height * 0.20,
// bottom: MediaQuery.of(context).copyWith().size.height * 0.7,
child: Row(
children: <Widget>[
// invisible FABs
FloatingActionButton(
onPressed: (){
_animate2Pagenegative();
// print(widget.date);
},
child: Icon(Icons.fast_rewind),
heroTag: UniqueKey(),
),
FloatingActionButton(
onPressed: (){
_animate2Pageforward();
// print(widget.date);
},
heroTag: UniqueKey(),
child: Icon(Icons.fast_forward),
),
],
),
),
// FAB which is displayed correctly
FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () async{
String result1 = await Navigator.push( // string which stores the user entered value
context,
MaterialPageRoute(
builder: (context) => InputScreen(), //screen which has TextField
));
setState(() {
addItem(result1, false, "");
});
},
tooltip: 'Add a task',
child: Icon(Icons.add),
elevation: 0.0,
),
],
),
)
脚手架主体:
body: new Column(
children: <Widget>[
new Expanded(
child: new DaysPageView(
// onDaysChanged: getDatestring,
physics: new NeverScrollableScrollPhysics(),
dayoverride: getDate(widget.date),
scrollDirection: _scrollDirection,
pageSnapping: _pageSnapping,
reverse: _reverse,
controller: _daysPageController,
pageBuilder: _daysPageBuilder,
),
),
],
),
解决方案
您不需要其他 FloatingActionsButtons 来进行该设计。我的理解是,您需要上部不要滚动内容,因此您想到了 FAB。有多种方法可以实现这一目标。这是一个示例,我使用颜色向您展示屏幕的不同部分。当然,这些是随机尺寸来展示案例。
return new Scaffold(
body: Column(
children: <Widget>[
Container(
height: 150,
color: Colors.red,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 20, left: 20),
child: Text(
"- 2 TASKS MORE",
style: TextStyle(
fontSize: 35
),
),
),
GestureDetector(
onTap: () {
print('left arrow pressed');
},
child: Container(
margin: EdgeInsets.only(top: 20, left: 20),
child: Icon(Icons.arrow_left)
),
),
GestureDetector(
onTap: () {
print('right arrow pressed');
},
child: Container(
margin: EdgeInsets.only(top: 20, left: 20),
child: Icon(Icons.arrow_right)
),
)
],
),
SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
child: Column(
children: <Widget>[
Container(
height: 600,
color: Colors.deepPurpleAccent,
),
],
),
)
],
),
bottomNavigationBar: BottomAppBar(
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
],
),
shape: CircularNotchedRectangle(),
color: Colors.red,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: ()
async {
print('FAB pressed');
},
tooltip: 'Add a task',
child: Icon(Icons.add),
elevation: 0.0,
),
);
这是结果
推荐阅读
- postgresql - 如何在 PostgreSQL 中添加外键
- azure-pipelines - 为什么我需要在 CICD 管道中使用构建代理?
- r - 从R中的重复数据有条件地选择第n个obs
- java - 带有 Logback 的映射诊断上下文的类型化 JSON 输出
- linux - Busybox 重启对 buildroot Linux 没有影响
- azure-active-directory - 启用 Azure AD '需要用户分配',如何处理同一租户中的未经授权的用户
- apache-flink - Flink:我们应该使用什么来代替命令行选项'-yt'?
- python - 替换 NaN 值
- windows-terminal - 阻止 Windows 终端显示当前目录
- game-physics - 使用坐标查找形状所属的节点