[英]Flutter - How to center widget inside list view
I'm struggling with centering a widget inside listView
.我正在努力将一个小部件放在listView
。
I tried this, but Text('ABC')
is not centered vertically.我试过这个,但Text('ABC')
没有垂直居中。 How can I achieve this?我怎样才能做到这一点?
new Scaffold(
appBar: new AppBar(),
body: new ListView(
padding: const EdgeInsets.all(20.0),
children: [
new Center(
child: new Text('ABC')
)
]
)
);
Vertically Center & Horizontal Center:垂直居中和水平居中:
Scaffold(
appBar: new AppBar(),
body: Center(
child: new ListView(
shrinkWrap: true,
padding: const EdgeInsets.all(20.0),
children: [
Center(child: new Text('ABC'))
]
),
),
);
Only Vertical Center只有垂直中心
Scaffold(
appBar: new AppBar(),
body: Center(
child: new ListView(
shrinkWrap: true,
padding: const EdgeInsets.all(20.0),
children: [
new Text('ABC')
]
),
),
);
Solving this question with shrinkWrap = true
is a hack.用shrinkWrap = true
解决这个问题是一个技巧。 The whole point of centering widgets inside a ListView
is to have bounciness and scrolling enabled.在ListView
中居中小部件的重点是启用弹性和滚动。 Using shrinkWrap
doesn't achieve this, it looks visually correct but it behaves completely wrong.使用shrinkWrap
不能实现这一点,它在视觉上看起来是正确的,但它的行为完全错误。
The solution is to place a Container
as the only children in the ListView
, and give it a minimum height equal to the available space for the height (Use LayoutBuilder
to measure the maximum height for the widget).解决方案是将一个Container
作为ListView
唯一的子项,并为其指定一个等于该高度可用空间的最小高度(使用LayoutBuilder
测量小部件的最大高度)。 Then as the Container
's child, you can either place a Center
or Column
(with MainAxisAlignment.center) widget and from there you can add whatever widgets you intended to center.然后作为Container
的孩子,您可以放置一个Center
或Column
(使用 MainAxisAlignment.center)小部件,然后您可以从那里添加您想要居中的任何小部件。
Below is the code to solve the example in the question:以下是解决问题中示例的代码:
Scaffold(
appBar: AppBar(),
body: LayoutBuilder(
builder: (context, constraints) => ListView(
children: [
Container(
padding: const EdgeInsets.all(20.0),
constraints: BoxConstraints(
minHeight: constraints.maxHeight,
),
child: Center(
child: Text('ABC'),
),
)
],
),
),
);
Wrap your widget into container将您的小部件包装到容器中
Container(alignment: Alignment.center, ...)
or或者
Container(alignment: Alignment.centerLeft, ...)
Simply wrap your widget in Align
and provide alignment
accordingly.只需将您的小部件包装在Align
并相应地提供alignment
。
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView( // your ListView
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: _button("Left"),
),
Align(
alignment: Alignment.center,
child: _button("Middle"),
),
Align(
alignment: Alignment.centerRight,
child: _button("Right"),
),
],
),
);
}
Widget _button(text) => RaisedButton(onPressed: () {}, child: Text(text));
}
Use Align
inside your ListView
which will render Widgets at the center, Also we don't have to give any alignment property as by default it is center在ListView
使用Align
,它将在中心呈现小部件,而且我们不必提供任何对齐属性,因为默认情况下它是中心
This will add ListView
at the center of the screen, as the list item increases it will grow from the center only.这将在屏幕中心添加ListView
,随着列表项的增加,它只会从中心增长。
Center(
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical, // Axis.horizontal for horizontal list view.
itemCount: 2,
itemBuilder: (ctx, index) {
return Align(child: Text('Text'));
},
),
),
Output:输出:
Just use the Align widget to center a child widget vertically and/or horizontally:只需使用 Align 小部件垂直和/或水平居中一个子小部件:
Align(
alignment: Alignment.center,
child: Text(
'middle',
),
Note : if you have a column as a child, you will have to add :注意:如果您小时候有一列,则必须添加:
mainAxisAlignment: MainAxisAlignment.center
to the column to align it vertically.到列以将其垂直对齐。
If you need the item in the center to scroll then do the following.如果您需要滚动中心的项目,请执行以下操作。
Center(
child: ListView(
shrinkWrap: true,
children: [
// the container with height will make the screen scroll
Container(
height:
MediaQuery.of(context).size.height / 1.2,
child: Text('ABC'),
),
],
),
),
Tip: give the container a color to visualize the areas that can scroll提示:给容器一个颜色来可视化可以滚动的区域
Scaffold(
backgroundColor: Color.fromARGB(255, 238, 237, 237),
body: Center(
child: Container(
child: Column(
children: <Widget>[
Column(children: <Widget>[
Image.asset(
'images/logo.png',
fit: BoxFit.contain,
width: 130,
)
]),
Column(children: <Widget>[
RaisedButton(
onPressed: () => {},
child: TextStyle_Title(
text: 'سلاااام',
),
)
]),
],
),
),
),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.