【发布时间】:2019-11-09 20:53:56
【问题描述】:
我正在尝试创建一个包含一列的 SingleChildScrollView,并且我希望一个按钮位于屏幕的最底部。为此,我尝试使用具有 SingleChildScrollView 和 FlatButton 的堆栈作为子级。我最终得到的是:
即使我已定位按钮并将其与底部对齐,我也无法让按钮粘在底部。绿色只是为了显示柱子的高度,并且按钮被卡在柱子的底部。 Stack、SingleChildScrollView、Column 和 FlatButton 只占用它们显示所需的空间。我需要将该按钮粘贴到屏幕底部。
这是我用来创建它的代码。我错过了什么?
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Container(
width: double.infinity,
color: Colors.red,
child: Stack(
children: <Widget>[
Container(
color: Colors.green,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
child: TextField(
decoration: InputDecoration(
labelText: 'Protein',
),
),
margin: EdgeInsets.only(left: 20, right: 20),
),
Container(
child: TextField(
decoration: InputDecoration(
labelText: 'Fat',
),
),
margin: EdgeInsets.only(left: 20, right: 20),
),
Container(
child: TextField(
decoration: InputDecoration(
labelText: 'Fiber',
),
),
margin: EdgeInsets.only(left: 20, right: 20),
),
Container(
child: TextField(
decoration: InputDecoration(
labelText: 'Moisture',
),
),
margin: EdgeInsets.only(left: 20, right: 20),
),
Container(
child: TextField(
decoration: InputDecoration(
labelText: 'Ash',
),
),
margin: EdgeInsets.only(left: 20, right: 20),
),
],
),
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Align(
alignment: Alignment.bottomCenter,
child: ButtonTheme(
minWidth: double.infinity,
height: 50,
child: FlatButton(
color: Colors.blueAccent.shade400,
onPressed: () {},
child: Text(
'Calculate Carbs',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
),
),
),
)
],
),
),
);
编辑:下面给出的两种方法都可以扩展堆栈以填满整个屏幕。我添加了额外的 TextField 小部件来填满屏幕,然后单击底部的小部件以确保底部小部件在键盘打开时可见,并注意到按钮覆盖了底部字段。就像滚动视图正在向上滚动正确的数量一样,忽略了那里有一个按钮。
在下面的图片中,我点击了 End Field 3 小部件。按钮盖住了它,所以我看不到我正在输入的内容。
【问题讨论】:
-
您可能已经解决了这个问题,但以防万一,您可以在脚手架上使用 `resizeToAvoidBottomInset: false` 来避免这种情况
-
你是怎么解决这个问题的?我正在努力解决同样的问题。手动设置高度会破坏文本输入的 scolling