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.
A scrolling view inside of which can be nested other scrolling views, with their scroll positions being intrinsically linked.
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.
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.
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
:
With shrinkWrap: true
:
You can use this in AlertDialog
s: 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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With