Home > Mobile >  How to get the height of the whole list.builder in flutter
How to get the height of the whole list.builder in flutter

Time:09-27

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;
}

CodePudding user response:

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,
   );
}

CodePudding user response:

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.

CodePudding user response:

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