(Flutter) Infinite Scroll `ListView.builder` with Finite Content

后端 未结 2 806
情书的邮戳
情书的邮戳 2021-01-27 07:00

1. The Problem

How do I make my ListView.builder be able to scroll to empty space both to the top and to the bottom?

For example,

2条回答
  •  鱼传尺愫
    2021-01-27 07:15

    That is possible to be achived with your "Container workaround". You could use a ScrollController and have its initialScrollOffset where the top Container ends. It is something I only later found out through this other StackOverflow question.

    1. Declare your ScrollController inside your widget's class.
      ScrollController scrollController = ScrollController(
        initialScrollOffset: 10, // or whatever offset you wish
        keepScrollOffset: true,
      );
      
    2. Add the controller to your ListView
      ListView.builder(
        controller: scrollController,
        itemCount: widgetsList.length,
        itemBuilder: (context, index){
          return widgetsList[index];
        },
      ),
      

    Additionally, you may even want to create animations for the background scrolling action. That is possible through using the .animateTo method inside your widget's initState.

提交回复
热议问题