Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to evenly space ListTiles in ListView

Tags:

I am trying to evenly space my list tiles in my list view with the code below and it is not working. The greater objective is to get both scroll and even distribution on rotation. Thanks for the help.

      Widget _buildBodyListView() {         return new Container(           padding: EdgeInsets.all(12.0),           child: Container(color: Colors.green,             child: ListView(shrinkWrap: false,               children: <Widget>[                 ListTile(                   trailing: Icon(Icons.keyboard_arrow_right),                   title: Text('LATEST NEWS', textAlign: TextAlign.center,),                 ),                 ListTile(                   trailing: Icon(Icons.keyboard_arrow_right),                   title: Text('MARKET NEWS ', textAlign: TextAlign.center,),                 ),                 ListTile(                   trailing: Icon(Icons.keyboard_arrow_right),                   title: Text('MARKET REPORT', textAlign: TextAlign.center,),                 ),              ListTile(               trailing: Icon(Icons.keyboard_arrow_right),               title: Text('LATEST NEWS', textAlign: TextAlign.center,),             ),             ListTile(               trailing: Icon(Icons.keyboard_arrow_right),               title: Text('MARKET NEWS ', textAlign: TextAlign.center,),             ),             ListTile(               trailing: Icon(Icons.keyboard_arrow_right),               title: Text('MARKET REPORT', textAlign: TextAlign.center,),             ),                ],             ),           ),         );       } 
like image 507
AhabLives Avatar asked Mar 12 '19 17:03

AhabLives


People also ask

How do you reduce the space between ListTile flutters?

You can use the visualDensity property to reduce the space. ListTile( visualDensity: VisualDensity(horizontal: 0, vertical: -4), title: Text("xyz") ); The visualDensity value can be changed from -4.0 to 4.0 . Lower the value, more compact the view.

How do you put a space in a ListTile Flutter?

Wrap your ListTile in a column and use SizedBox to separate list items. Use key in column, not inside ListTile.

How do you reduce the space between leading and title in ListTile Flutter?

As Dinesh has pointed out here, ListTile has received a minLeadingWidth property. Default value is 40 , so to reduce space between leading and title by x pass minLeadingWidth: 40 - x .


1 Answers

I recommend you use the ListView.separated widget. It has a property called the separatorBuilder. It return a Widget, here a SizedBox of a particular height, that will act as a separator.

Here is how you should do it:

ListView.separated(   physics: BouncingScrollPhysics(),   shrinkWrap: true,   itemCount: 6,   itemBuilder: (context, index) => Container(     padding: EdgeInsets.all(12.0),     child: Container(       color: Colors.green,       child: ListView(         children: const <Widget>[           ListTile(             trailing: Icon(Icons.keyboard_arrow_right),             title: Text('LATEST NEWS', textAlign: TextAlign.center,),           ),           ListTile(             trailing: Icon(Icons.keyboard_arrow_right),             title: Text('MARKET NEWS ', textAlign: TextAlign.center,),           ),           ListTile(             trailing: Icon(Icons.keyboard_arrow_right),             title: Text('MARKET REPORT', textAlign: TextAlign.center,),           ),           ListTile(             trailing: Icon(Icons.keyboard_arrow_right),             title: Text('LATEST NEWS', textAlign: TextAlign.center,),           ),           ListTile(             trailing: Icon(Icons.keyboard_arrow_right),             title: Text('MARKET NEWS ', textAlign: TextAlign.center,),           ),           ListTile(             trailing: Icon(Icons.keyboard_arrow_right),             title: Text('MARKET REPORT', textAlign: TextAlign.center,),           ),          ],       ),     ),   ),   separatorBuilder: (context, index) => SizedBox(     height: 10,   ) ); 
like image 100
Tayormi Avatar answered Sep 19 '22 13:09

Tayormi