How to get the height of the whole list.builder in flutter - flutter

I need to get the whole list.builder height including the part that is out of the screen view
The List children vary in height
i used the GlobalKey method but it is giving me only the height of the list shown in the screen
I found this solution and it's working and give me the full list.builder height after scrolling event
but when adding more children to the list.builder by changing the state
this code doesn't work anymore
scrollController.addListener(() {
double? fullListViewHeight;
double fullListHeightHelper = listKey.currentContext!.size!.height +
scrollController.position.extentAfter;
fullListView ??= fullListHeightHelper;
}

listview.builder only build the item that shown to user, in order to get the height with GlobalKey method, you can use listView which build all item at the same time. If you don't know how pass your list to listView , see this example:
List yourList = [...];
_buildList(){
List<Widget> _children =yourList.map((e) => YourListItem()).toList();
return ListView(
key:yourKey,
children: _children,
);
}

it is giving me only the height of the list shown in the screen
yes.. thats correct. because listview not render all the children. see the documentation of listview:
The ListView.builder ...constructor is appropriate for list views with
a large (or infinite) number of children because the builder is called
only for those children that are actually visible.
to get exact height of all children is impossible. since when the widget child is scrolled out of view, the associated element subtree, states and render objects are destroyed.
I think you need to calculate it manually or make the children height is same for all.

To calculate it, you need to make each item the same height. Then,
To get the height of the ListView.builder you simply multiply the height of each item by the length of the ListView.builder.

Related

Flutter scroll controller infinite scroll fail on larger screen size

I have a problem with infinite scroll in flutter, I am loading six items per page and loading more as I scroll , this works perfectly with a smaller screen size but the problem comes in when I use an emulator with a larger screen size, when the screen size is larger then the first six items don't fill the screen hence I cannot scroll in order to load the other items.
Does anybody have an idea of how I can get around this?
Thanks
You can use ListView.builder. The ListView.builder constructor takes an IndexedWidgetBuilder, which builds the children on demand. This constructor is appropriate for list views with a large (or infinite) number of children because the builder is called only for those children that are actually visible.
return ListView.builder(
padding: const EdgeInsets.all(10.0),
itemBuilder: (context, i) {
if (i.isOdd) return const Divider();
final index = i ~/ 2;
if (index >= _listItems.length) {
_listItems.addAll(_newItems); //load more items here
}
return yourWidget(_listItems[index]);
},
);
This way no need to use scrollController. You can refer to this codelab for more info.

Getting scroll index from scrolling position in pixels listview flutter

Is there a way we can use ScrollController.position.pixels to know the scrolling index of a listview in flutter? I'm trying to restore the last scrolling index between app restarts.
You can use the flutter_scrollview_observer lib to implement your desired functionality without invasivity
Create and use instance of ListView normally.
ListView _buildListView() {
return ListView.separated(
...
);
}
Pass the ListView instance to the ListViewObserver, then you can obtain the desired result in the onObserve callback
ListViewObserver(
child: _buildListView(),
onObserve: (resultModel) {
print('firstChild.index -- ${resultModel.firstChild?.index}');
print('displaying -- ${resultModel.displayingChildIndexList}');
},
)

Is there a way to automatically wrap widgets within a row

Sorry if this is a dup, I see a lot of questions regarding text wrapping, but not generic widget wrapping in a row.
I have a dynamic row of buttons within a row. I'd like to wrap the buttons to create a second row if the list becomes longer than the width of the screen. Is it possible to do this automatically, or do I need to manually detect and create the correct number of rows.
If the latter, does anybody have pointers to measuring a widget's width vs screen width? Right now I'm doing something like:
...
return Row(
children: List.generate(count*2 + 1, (i) {
if (i %2 == 0) {
return Spacer();
}
return RaisedButton(child: Text((i / 2).round().toString(), onPressed: (){...});
});
Row is useful when you need to organize child widgets in a single line. Wrap with default direction is an alternative to Row that moves child widgets to the next line if there is no enough space

Is it possible to use ReorderableListView in a "wrapping" mode?

I would like the ReorderableListView not to take up the whole height which is given to it, but to be as tall as the item views that it contains.
I could make the list either flex and fill the whole height it is given, or put it in a SizedBox and have a fixed height.
I'd like to add an + Add list tile directly under the list items (similar to Google Keep on Android)
Thank you
You can try this:
Wrap your List with an Expanded widget.
Fetch the number of items in your list and return the Add List tile widget at the length-1 position of your list.
You can alter the size of your ListView in correlation to the number of items fetched from the list.
Expanded will help you alter the length dynamically.
eg:
Expanded(
child: new ListView.builder
(
itemCount: litems.length,
itemBuilder: (BuildContext ctxt, int Index) {
return new Text(litems[Index]);
if(length==length-1){
//**call your Add List tile widget here**
}
}
)
)

Difference between ListView and Column?

When creating a very simple scrollable list in Flutter, what are the advantages and disadvantages of saying (where widgets is List<Widget> == true):
Option 1:
var widget = new SingleChildScrollView(
child: new Column(
chidren: widgets
));
Option 2:
var widget = new ListView(children: widgets);
ListView:
Listview Widget shows the unlimited number of children inside it, but the main advantage of using ListView is it renders only visible items on the screen perhaps more specifically I would say ListView.Builder()
Column
The column is used when we have to draw different widgets in the list. If items increase in the column then SingleChildScrollView is used for scrolling purposes.
For More Reference:
https://medium.com/flutterworld/flutter-problem-listview-vs-column-singlechildscrollview-43fdde0fa355
Definitely go for option 2.
ListView have a few cool optimisations. https://youtu.be/UUfXWzp0-DU?t=33m38s