flutter how to center widget inside list view

荒凉一梦 提交于 2019-12-06 19:37:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!