close

How to add a ListView to a Column in Flutter?

A ton of new vacillate software engineer while choosing the above point will get confounded, as they perform the vast majority of the things comparable

We should take an illustration of a shopping application where we see a rundown of items through and through

How about we perceive how Listview functions

Listview Widget shows the limitless number of youngsters inside it, yet the fundamental preferred position of utilizing ListView is it delivers just obvious things on the screen maybe more explicitly I would state ListView.Builder()

Barely any Advantages:

  1. It will be utilized when we need to deliver a similar gadget nth number of times.
  2. ListView, just the things that are noticeable are mounted and painted.

Note: One significant point you should fare thee well while picking ListView

ListView() — Render all the things in the rundown, regardless of whether it isn’t noticeable.

‘ListView.Builder()’ — Render just obvious things on the screen.

Code Sample:

  1. ListView
ListView(
  padding: const EdgeInsets.all(8),
  children: <Widget>[
    Container(
      height: 50,
      color: Colors.red[600],
      child: const Center(child: Text('Entry A')),
    ),
    Container(
      height: 50,
      color: Colors.red[500],
      child: const Center(child: Text('Entry B')),
    ),
    Container(
      height: 50,
      color: Colors.red[100],
      child: const Center(child: Text('Entry C')),
    ),
  ],
)

2. ListView.Builder()

final List<String> entries = <String>['A', 'B', 'C'];
final List<int> colorCodes = <int>[600, 500, 100];
ListView.builder(
    padding: const EdgeInsets.all(8),
    itemCount: entries.length,
    itemBuilder: (BuildContext context, int index) {
      return Container(
        height: 50,
        color: Colors.red[colorCodes[index]],
        child: Center(child: Text('Entry ${entries[index]}')),
      );
    }
)

Output:

Image for post
Both above code will produce the above output

Now check how SingleChildScrollView + Column works

The segment is utilized when we need to list gadgets vertically on the screen and SingleChildScrollView gadget gives scroll usefulness to Column gadgets.

At the point when we utilized SingleChildScrollView+Column, the whole thing list is delivered regardless of whether a couple of things are noticeable.

So for complex formats with few things, the exhibition gain may not merit the difficulty, wherein case we can utilize SingleChildScrollView.

Scarcely any Advantages:

  1. It will be helpful when various gadgets are required with scroll usefulness.
  2. For complex formats, when things are less and execution, not a worry we can utilize it(Like when each other gadget required some adjustment which is not the same as other)

Code:

SingleChildScrollView( 
  child: Column(
    children: <Widget>[
      Container(
        height: 250,
        color: Colors.red[600],
        child: const Center(child: Text('Entry A')),
      ),
      Container(
        height: 250,
        color: Colors.red[500],
        child: const Center(child: Text('Entry B')),
      ),
      Container(
        height: 250,
        color: Colors.yellow[100],
        child: const Center(child: Text('Entry C')),
      ),
    ],
  ),
)

Output:

Image for post
SingleChildScrollView + Column.

Summary:

  1. You could consider ListView as an optimization to the combination of SingleChildScrollView + Column.
  2. ListView is less flexible. So for complex layouts with a small number of items, the performance gain may not be worth the trouble, in which case we can use SingleChildScrollView.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment