Search code examples
flutterflutter-layoutflutter-sliver

how can i make the headers from Custom Sticky Header stick below each other instead of overlapping in flutter?


I think I understood how the Custom Sticky Header work now, but I just can't find where can i add the functionality to stop the headers of Sticky Header from overlapping i would like to achieve something like in the picture below but without overlapping of the headers, i want each header to stick below each other. Any help on where to add additional functionalities would be a huge help, thanks in advance! image showing the overlapping sticky header

Edit: source link


Solution

  • Okay, The logic is simple. just add a header to your list and subitems of header to that list.

    So, you can implement your requirements by using the code :

        List<Widget> _buildStickySliverListTest(ListCount sliverList) {
        var widgetList = List<Widget>();
        for (int index = 0; index < sliverList.data.length; index++) {
          widgetList
            ..add(
              SliverAppBar(
                automaticallyImplyLeading: false,
                title: Text("Header $index"),
                pinned: true,
              ),
            )
            ..add(
              SliverFixedExtentList(
                itemExtent: 50.0,
                delegate:
                SliverChildBuilderDelegate((BuildContext context, int index) {
                  return Container(
                    alignment: Alignment.center,
                    color: Colors.lightBlue[100 * (index % 9)],
                    child: Text('Sublist item $index'),
                  );
                }, childCount: sliverList.data[index].length),
              ),
            );
        }
    
        return widgetList;
      }
    
      @override
      Widget build(BuildContext context) {
        var list1=["a","b","c"];
        var list2=["a","b","c","d","e"];
        var list3=["a","b"];
        var list4=["a","b","c","d"];
        var data=[list1,list2,list3,list4];
        var sliverList=ListCount(data);
    
        return Scaffold(
          appBar: AppBar(
            title: Text("Sticky Sliver List"),
          ),
          body: CustomScrollView(
          slivers: _buildStickySliverListTest(sliverList),
          ),
        );
      }
    

    Also, Create a class for ListCount as below,

    class ListCount{
      List<List<String>> data;
    
      ListCount(this.data);
    }
    

    This way you can easily build you header and sublist. implement your logic for listing and header as per your choice.

    I hope this will work for your requirements.