buildListViewSeparated function

Widget buildListViewSeparated(
  1. {bool closeToTrailing = false,
  2. bool useKeFrame = true,
  3. ViewPortBuilder viewportBuilder,
  4. @required int itemCount,
  5. @required IndexedWidgetBuilder itemBuilder,
  6. Axis scrollDirection = Axis.vertical,
  7. double dividerHeight = 15,
  8. Color dividerColor = Colors.transparent,
  9. ScrollPhysics physics,
  10. EdgeInsets parentPadding = EdgeInsets.zero,
  11. EdgeInsets itemPadding = EdgeInsets.zero,
  12. bool useSliver = false,
  13. bool shrinkWrap = false,
  14. bool useListView = true,
  15. CollectGarbage collectGarbage,
  16. bool includeStart = true,
  17. bool includeEnd = true,
  18. Widget placeHolder,
  19. int estimateCount = 0,
  20. ScrollController controller}
)

间隔listview

Implementation

Widget buildListViewSeparated({
  bool closeToTrailing = false,
  bool useKeFrame = true,
  ViewPortBuilder viewportBuilder,
  @required int itemCount,
  @required IndexedWidgetBuilder itemBuilder,
  Axis scrollDirection = Axis.vertical,
  double dividerHeight = 15,
  Color dividerColor = Colors.transparent,
  ScrollPhysics physics,
  EdgeInsets parentPadding = EdgeInsets.zero,
  EdgeInsets itemPadding = EdgeInsets.zero,
  bool useSliver = false,
  bool shrinkWrap = false,
  bool useListView = true,
  CollectGarbage collectGarbage,
  bool includeStart = true,
  bool includeEnd = true,
  Widget placeHolder,
  int estimateCount = 0,
  ScrollController controller,
}){

  int count = itemCount * 2 - 1;
  bool useEven = false;
  if(includeStart && includeEnd){
    count = itemCount * 2 + 1;
    useEven = true;
  }else if(includeStart){
    count = itemCount * 2;
    useEven = true;
  }else if(includeEnd){
    count = itemCount * 2;
    useEven = false;
  }else{
    count = itemCount * 2 - 1;
    useEven = false;
  }

  Widget child = Container();

  if (useSliver) {
    if (itemCount <= 0) {
      child = SliverToBoxAdapter(
        child: Container(
          width: 0,
          height: 0,
        ),
      );
    }
    if (useListView) {
      if (scrollDirection == Axis.vertical) {
        child = ExtendedSliverList(
          delegate: SliverChildBuilderDelegate((context, index){
            return _buildListViewItem(
                useEven: useEven,
                useKeFrame: useKeFrame,
                scrollDirection: scrollDirection,
                dividerHeight: dividerHeight,
                dividerColor: dividerColor,
                padding: itemPadding,
                context: context,
                index: index,
                itemBuilder: itemBuilder
            );}, childCount: count
          ),
          extendedListDelegate: ExtendedListDelegate(
            ///追踪进入 Viewport 的列表元素的 index(即你看到的可视区域,并不包括缓存距离)
              viewportBuilder: (int fistIndex, int lastIndex){
                if(isNotEmpty(viewportBuilder)){
                  viewportBuilder(fistIndex, lastIndex);
                }
              },
              closeToTrailing: closeToTrailing,
              collectGarbage: collectGarbage,
          ),
        );
      }else{
        child = SliverToBoxAdapter(
          child: ExtendedListView.builder(
            controller: controller,
            shrinkWrap: shrinkWrap,
            physics: physics,
            itemCount: count,
            itemBuilder: (context, index){
              return _buildListViewItem(
                  useEven: useEven,
                  useKeFrame: useKeFrame,
                  scrollDirection: scrollDirection,
                  dividerHeight: dividerHeight,
                  dividerColor: dividerColor,
                  padding: itemPadding,
                  context: context,
                  index: index,
                  itemBuilder: itemBuilder
              );
            },
            scrollDirection: scrollDirection,
            extendedListDelegate: ExtendedListDelegate(
              ///追踪进入 Viewport 的列表元素的 index(即你看到的可视区域,并不包括缓存距离)
                viewportBuilder: (int fistIndex, int lastIndex){
                  if(isNotEmpty(viewportBuilder)){
                    viewportBuilder(fistIndex, lastIndex);
                  }
                },
                closeToTrailing: closeToTrailing,
                collectGarbage: collectGarbage
            ),

          ),
        );
      }
    } else{
      List<Widget> widgetList = List.generate(count, (index){
        return _buildListViewItem(
            useEven: useEven,
            scrollDirection: scrollDirection,
            dividerHeight: dividerHeight,
            dividerColor: dividerColor,
            padding: itemPadding,
            context: context,
            index: index,
            itemBuilder: itemBuilder
        );
      });

      ///使用Column或者Row
      child = SliverToBoxAdapter(
        child: SingleChildScrollView(
          controller: controller,
          scrollDirection: scrollDirection,
          physics: physics,
          child: scrollDirection == Axis.vertical? Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: widgetList,
          ) : Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: widgetList,
          ),
        ),
      );
    }
  }else{
    if (itemCount <= 0) {
      return Container(
        width: 0,
        height: 0,
      );
    }
    if (useListView) {
      child = ExtendedListView.builder(
          controller: controller,
          shrinkWrap: shrinkWrap,
          physics: physics,
          itemCount: count,
          scrollDirection: scrollDirection,
          extendedListDelegate: ExtendedListDelegate(
            ///追踪进入 Viewport 的列表元素的 index(即你看到的可视区域,并不包括缓存距离)
              viewportBuilder: (int fistIndex, int lastIndex){
                if(isNotEmpty(viewportBuilder)){
                  viewportBuilder(fistIndex, lastIndex);
                }
              },
              closeToTrailing: closeToTrailing,
              collectGarbage: collectGarbage
          ),
          itemBuilder: (context, index){
            return _buildListViewItem(
                useEven: useEven,
                useKeFrame: useKeFrame,
                scrollDirection: scrollDirection,
                dividerHeight: dividerHeight,
                dividerColor: dividerColor,
                context: context,
                index: index,
                padding: itemPadding,
                itemBuilder: itemBuilder
            );
          }
      );
    }else{

      List<Widget> widgetList = List.generate(count, (index){
        return _buildListViewItem(
            useEven: useEven,
            context: context,
            useKeFrame: useKeFrame,
            index: index,
            scrollDirection: scrollDirection,
            dividerHeight: dividerHeight,
            dividerColor: dividerColor,
            padding: itemPadding,
            itemBuilder: itemBuilder
        );
      });
      ///使用Column或者Row
      child = SingleChildScrollView(
        controller: controller,
        scrollDirection: scrollDirection,
        physics: physics,
        child: scrollDirection == Axis.vertical? Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: widgetList
        ) : Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: widgetList
        ),
      );
    }
  }

  if(useKeFrame){
    return SizeCacheWidget(
      estimateCount: estimateCount,
      child: Padding(
        padding: parentPadding,
        child: child,
      ),
    );
  }else{
    return Padding(
      padding: parentPadding,
      child: child,
    );
  }

}