问题
I'm trying to bottom-center a widget at the bottom of a Column, but it keeps aligning to the left.
return new Column(
new Stack(
new Positioned(
bottom: 0.0,
new Center(
new Container(),
),
),
),
);
The existence of the Positioned forces the Container to the left, instead of centering. Removing the Positioned, however, puts the Container in the middle-center.
回答1:
Align is the way to go is you have only one child.
If you have more, consider doing something like this :
return new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
//your elements here
],
);
回答2:
1) You can use an Align widget, with FractionalOffset.bottomCenter
.
2) You can also set left: 0.0
and right: 0.0
in the Positioned
.
回答3:
I have used this approach,
What i want is, A layout
always in bottom but whenever Keyboard pops-up that layout
also comes over it
body: Container(
color: Colors.amber,
height: double.maxFinite,
child: new Stack(
//alignment:new Alignment(x, y)
children: <Widget>[
new Positioned(
child: myWidget(context, data.iconName, Colors.red),
),
new Positioned(
child: new Align(
alignment: FractionalOffset.bottomCenter,
child: myWidget(context, data.iconName, Colors.green)
),
)
],
),
),
回答4:
Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: //Your widget here,
)
),
)
回答5:
Wrap your Container in SingleChildScrollView() widget. Then it will not come above when keyboard pops up.
回答6:
Just expanding the answers:
- Spacer is an option no one mentioned yet; it is used in case you prefer not to use
Positioned
/Align
. - Align works if you want to specify the alignment of a child inside a parent. Use it anywhere but directly inside
Stack
- Positioned is similar to Align, but works only under
Stack
directly.
来源:https://stackoverflow.com/questions/45746636/flutter-trying-to-bottom-center-an-item-in-a-column-but-it-keeps-left-aligning