tp7309 / flutter_sticky_and_expandable_list

粘性头部与分组列表Sliver实现 Build a grouped list, which support expand/collapse section and sticky headers, support use it with sliver widget.
MIT License
140 stars 29 forks source link

Usage with Dismissible widget #3

Closed emvaized closed 4 years ago

emvaized commented 4 years ago

How to properly remove section from screen in onDismissed() callback of Dismissible widget? I tried to call widget.fetchedData.removeAt(position), but it seems to fail, resulting in 'RangeError (index)'.

tp7309 commented 4 years ago
          SliverExpandableList(
            builder: SliverExpandableChildDelegate<String, Section>(
              sectionList: sectionList,
              headerBuilder: _buildHeader,
              itemBuilder: (context, section, item, index) => Dismissible(
                key: Key(item),
                onDismissed: (DismissDirection direction) {
                  section.getItems().remove(item);
                },
                child: ListTile(
                  leading: CircleAvatar(
                    child: Text("$index"),
                  ),
                  title: Text(item),
                ),
              ),
            ),
          ),
emvaized commented 4 years ago

@tp7309 Unfortunately, with your implementation I get this error somehow:

A dismissed Dismissible widget is still part of the tree.

Make sure to implement the onDismissed handler and to immediately remove the Dismissible widget from the application once that handler has fired.
tp7309 commented 4 years ago

Sorry, example fixed:

SliverExpandableList(
            builder: SliverExpandableChildDelegate<String, Section>(
              sectionList: sectionList,
              headerBuilder: _buildHeader,
              itemBuilder: (context, section, item, index) => Dismissible(
                key: Key(item),
                onDismissed: (DismissDirection direction) {
                  setState(() {
                    section.getItems().remove(item);
                  });
                },
                child: ListTile(
                  leading: CircleAvatar(
                    child: Text("$index"),
                  ),
                  title: Text(item),
                ),
              ),
            ),
          ),
emvaized commented 4 years ago

@tp7309 Still get the RangeError :((

Maybe that's because item in my case is a String 'item$index', which is never used in a resulting widget from itemBuilder?

tp7309 commented 4 years ago

sectionList has List<item>, you need remove item object where from itemBuilder: (context, section, item, index). Try run example run normally first, then test you project.

emvaized commented 4 years ago

@tp7309 And section's header will be removed with item as well?

emvaized commented 4 years ago

I found the solution of my problem. Just had to replace

    for (int i = 0; i <= 10; i++) {
      var section = Section()
        ..expanded = true
        ..header = 'header$i'
        ..items = List.generate(1, (index) => "item$i");
     sections.add(section);
    }

in getExampleSections() with this:

   data.forEach((i) {
      var section = Section()
        ..expanded = true
        ..header = 'header$i'
        ..items = List.generate(1, (index) => "item$i");
     sections.add(section);
    });

The problem was in hard-coded amount of 10 sections 😅 Thanks for all help!