Search code examples
flutterflutter-layout

Flutter - How to center widget inside list view


I'm struggling with centering a widget inside listView.

I tried 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')
      )
    ]
  )
);

Solution

  • 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')
            ]
        ),
      ),
    );