Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the shrink wrap property do in flutter?

Tags:

flutter

I am new to Flutter and very eager to learn this technology. I cannot understand the work of shrinkWrap property in ListView. I couldn't understand the Flutter documentation.

like image 636
Harsh Gupta Avatar asked Jan 02 '19 13:01

Harsh Gupta


People also ask

What is Nestedscrollview in flutter?

A scrolling view inside of which can be nested other scrolling views, with their scroll positions being intrinsically linked.

What is PageView in flutter?

A PageView is a widget that generates the pages of the screen that are scrollable. The pages can be fixed or repeating pages that is built by the builder function. PageView behaves like ListView in constructing the elements.

What is ListView builder in flutter?

ListView is a very important widget in a flutter. It is used to create the list of children But when we want to create a list recursively without writing code again and again then ListView. builder is used instead of ListView. ListView. builder creates a scrollable, linear array of widgets.


2 Answers

Usually a ListView (as well as GridView, PageView and CustomScrollView) tries to fill all the available space given by the parent element, even when the list items would require less space.

With shrinkWrap: true, you can change this behavior so that the ListView only occupies the space it needs (it will still scroll when there more items).

Take a look at this example:

import 'package:flutter/material.dart';  void main() => runApp(App());  class App extends StatelessWidget {   @override   Widget build(BuildContext context) {     return MaterialApp(       home: Scaffold(         appBar: AppBar(),         body: Center(           child: Container(             margin: EdgeInsets.all(32),             decoration: BoxDecoration(border: Border.all(color: Colors.red)),             child: ListView(               shrinkWrap: false,               children: <Widget>[                 ListTile(title: Text('Item 1')),                 ListTile(title: Text('Item 2')),                 ListTile(title: Text('Item 3')),               ],             ),           ),         ),       ),     );   } } 

With shrinkWrap: false:

without shrinkWrap

With shrinkWrap: true:

with shrinkWrap

You can use this in AlertDialogs: When there are only a few items, make the dialog as small as possible. When there are many items, fill the screen height and make the list scrollable:

Dialog

Dialog

like image 177
boformer Avatar answered Sep 18 '22 03:09

boformer


If you do not set the shrinkWrap property, your ListView will be as big as its parent.

If you set it to true, the list will wrap its content and be as big as it children allows it to be.

like image 22
Daniel Oliveira Avatar answered Sep 17 '22 03:09

Daniel Oliveira