问题
I'm struggling with centering widget inside listView
.
I did like this but Text('ABC')
is not centered vertically.
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')
)
]
)
);
回答1:
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')
]
),
),
);
回答2:
Wrap your widget into container
Container(alignment: Alignment.center, ...)
or
Container(alignment: Alignment.centerLeft, ...)
来源:https://stackoverflow.com/questions/52991376/flutter-how-to-center-widget-inside-list-view