Quantcast
Channel: Active questions tagged listview - Stack Overflow
Viewing all articles
Browse latest Browse all 611

Flutter Nisted Listview builder with lazyload for each one

$
0
0

enter image description hereIn my case, there is a ListView builder for the Group header, and for each Group, there is a ListView builder for the items of the Group. Both the internal and external lists use lazy loading. What is the best solution for this?the issue in this code is when the inner list ends the outter not scrollingwhat I need is to start scrolling the out when the inner ends

import 'package:flutter/material.dart';void main() {  runApp(MyApp());}class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      home: GroupedLazyLoadList(),    );  }}class Group {  final int id;  final List<int> items;  bool isLoading;  Group(this.id, this.items, {this.isLoading = false});}class GroupedLazyLoadList extends StatefulWidget {  @override  _GroupedLazyLoadListState createState() => _GroupedLazyLoadListState();}class _GroupedLazyLoadListState extends State<GroupedLazyLoadList> {  final List<Group> _groups = List.generate(5, (index) => Group(index, List.generate(5, (itemIndex) => itemIndex)));  final ScrollController _scrollController = ScrollController();  @override  void initState() {    super.initState();    _scrollController.addListener(() {      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {        _loadMoreGroups();      }    });  }  @override  void dispose() {    _scrollController.dispose();    super.dispose();  }  Future<void> _loadMoreGroups() async {    if (_groups.isNotEmpty && _groups.last.isLoading) return;    setState(() {      if (_groups.isNotEmpty) _groups.last.isLoading = true;    });    await Future.delayed(Duration(seconds: 2)); // Simulate network delay    setState(() {      if (_groups.isNotEmpty) _groups.last.isLoading = false;      _groups.add(Group(_groups.length, List.generate(5, (itemIndex) => itemIndex)));    });  }  Future<void> _loadMoreItems(Group group) async {    if (group.isLoading) return;    setState(() {      group.isLoading = true;    });    await Future.delayed(Duration(seconds: 2)); // Simulate network delay    setState(() {      group.isLoading = false;      group.items.addAll(List.generate(5, (itemIndex) => group.items.length + itemIndex));    });  }  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text('Grouped Lazy Load List'),      ),      body: CustomScrollView(        controller: _scrollController,        slivers: <Widget>[          SliverList(            delegate: SliverChildBuilderDelegate(              (BuildContext context, int groupIndex) {                if (groupIndex == _groups.length) {                  return Center(                    child: CircularProgressIndicator(),                  );                }                Group group = _groups[groupIndex];                return Column(                  crossAxisAlignment: CrossAxisAlignment.start,                  children: <Widget>[                    ListTile(                      title: Text('Group ${group.id}'),                    ),                    ListView.builder(                      shrinkWrap: true,                      physics: NeverScrollableScrollPhysics(),                      itemCount: group.items.length + (group.isLoading ? 1 : 0),                      itemBuilder: (context, itemIndex) {                        if (itemIndex == group.items.length) {                          return Center(                            child: CircularProgressIndicator(),                          );                        }                        if (itemIndex == group.items.length - 1) {                          // Check if we need to load more items for this group                          WidgetsBinding.instance.addPostFrameCallback((_) {                            _loadMoreItems(group);                          });                        }                        return ListTile(                          title: Text('Item ${group.items[itemIndex]}'),                        );                      },                    ),                  ],                );              },              childCount: _groups.length + 1,            ),          ),        ],      ),    );  }}

Viewing all articles
Browse latest Browse all 611

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>