Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to scroll a list inside of Wrap widget?

Tags:

flutter

dart

I am using wrap to display some images, the direction of Wrap is set to Axis.horizontal. When it runs out of horizontal space, it is creating new row. As long as there is enough space vertically, everything is getting displayed as it should. But the moment there are a lot of images, it is giving a Bottom Overflow.

I tried wrapping the Wrap widget inside SingleChildScrollView, ListView and Expanded but nothing seems to work. Here is my code:

@override
  Widget build(BuildContext context) {
    return Wrap(
      direction: Axis.horizontal,
      spacing: 8,
      runSpacing: 12,
      children: [
        _buildImageCard('abc'),
        _buildImageCard('def'),
        _buildImageCard('ghi'),
        _buildImageCard('jkl'),
      ],
    );
  }
}

and the _buildImageCard

Widget _buildImageCard(String imagePath) {
    return SizedBox(
      width: 500,
      height: 400,
      child: Card(
        elevation: 5,
      ),
    );
  }

The result is:

enter image description here

like image 993
gegobyte Avatar asked Nov 07 '20 12:11

gegobyte


People also ask

How do you scroll a list in flutter?

All you have to do is set Global Keys for your widgets and call Scrollable. ensureVisible on the key of your widget you want to scroll to. For this to work your ListView should be a finite List of objects. If you are using ListView.


1 Answers

try SingleChildScrollView

  SingleChildScrollView(
    scrollDirection: Axis.vertical,
    child: Wrap(
      direction: Axis.horizontal,
      spacing: 8,
      runSpacing: 12,
      children: [
        _buildImageCard('abc'),
        _buildImageCard('def'),
        _buildImageCard('ghi'),
        _buildImageCard('jkl'),
        _buildImageCard('jkl'),
        _buildImageCard('jkl'),
        _buildImageCard('jkl'),
      ],
    ),
  );

enter image description here

like image 120
M Alkhatib Avatar answered Sep 26 '22 14:09

M Alkhatib