Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertical align ListView in the middle

Tags:

flutter

dart

I want my list item in the middle of the ListView

@override Widget build(BuildContext context)
{
    List<String> listName = ['Woody', 'Buzz', 'Rex'];

    return Container
    (
      decoration: BoxDecoration(border: Border.all(color: Colors.pinkAccent)),
      width: 400,
      height: 700,
      alignment: Alignment.centerLeft,
      child: ListView.builder
      (
        itemCount: listName.length,
        itemBuilder: (context, index)
        {
          return OurName(listName[index]);
        },
      ),
    );
}

This is when I use shrinkWrap, when I scroll up it's cut off and it can't be scroll down shrinkWrap

This is what I expected expected

like image 220
cyclick Avatar asked Oct 26 '25 16:10

cyclick


2 Answers

The key to your issue is the shrinkWrap property of ListView. So to fix this you can do something like this:

Container(
        alignment: Alignment.centerLeft,
        child: ListView.builder(
          itemBuilder: (context, index) {
            return Text("The index$index");
          },
          shrinkWrap: true,
          itemCount: 30,
        ),
      ),
like image 112
danypata Avatar answered Oct 28 '25 06:10

danypata


For me works this code!

@override
  Widget build(BuildContext context) {
    return Container(
      height: double.infinity,
      width: double.infinity,
      alignment: Alignment.center,
      child: ListView.builder(
          shrinkWrap: true,
          itemCount:rows.length,
          itemBuilder: (
            context,
            index,
          ) {
            return ...;
          }),
    );
  }
like image 31
Nabijon Azamov Avatar answered Oct 28 '25 08:10

Nabijon Azamov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!