1

The title might not be totally accurate but forgive me I don't really know what to write. I am trying to create an effect where a noScroll phyicas Listview that is nested with other elements in a SingleChildScrollView, would need to change to a scrollable physics when it reaches the top of the SingleChildScrollView (thus occupying the whole view) in order to utilize the lazy loading of items.

I must say I ma trying to avoid rebuilding the nested Listview because of that cost and especially the cost of building it with the NeverScrolling physics since that would stall the main UI thread until all items are built.

The necessity of using the NeverScrolling physics in the first place it to allow the SingleChildScrollView to properly scroll (if the list view is set to normal scrolling, the SingleChildScrollView will not be able to scroll if the gesture is coming from the nested Listview)

My code looks like this :

SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: Material(
                  child: Container(
                    child: Column(
                      children: <Widget>[
                        ItemListDevider(),
                        Container(
                          height: 120,
                          child: Container(height:120)
                        ),
                        ItemListDevider(),
                        Flexible(
                          fit: FlexFit.loose,
                          child: THeWidgetHoldingTheListView(album),
                        )

                      ],
                    ),
                  ),
                ),
              )
// the build method of the THeWidgetHoldingTheListView widget would return this :
 Container(
          child: Row(
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Expanded(
                child: Column(
                  children: <Widget>[
                    Flexible(
                      child: ListView.builder(
                        controller: controller,
                        shrinkWrap: true,
                        itemExtent: FixedItemExtent,
                        physics: AlwaysScrollableScrollPhysics(),
                        itemCount: FixedItemCount,
                        itemBuilder: (context, index) {
                          return MyCard()
                        },
                      ),
                    ),
                  ],
                ),
              ),
              MyScrollbar(
                controller: controller,
              ),
            ],
          ),
        )

*Note: * I had the idea of creating a custom ScrollPhysics that starts as a NeverScrollPhysics and the switches on a certain scroll offset but that wouldn't change the fact that I have to load the entire list as a Neverscroll and thus stalling the main UI thread.

Is there a way to get around this change of scroll physics, if not how can I implement it without compromising on performance like mentioned above.

Kaki Master Of Time
  • 1,428
  • 1
  • 21
  • 39
  • Perhaps this answer might help you? https://stackoverflow.com/questions/56291223/flutter-how-to-avoid-listview-to-scroll-or-change-its-physics-dynamically – William Terrill May 05 '20 at 03:46
  • 1
    you have to use `CustomScrollView` and forget about those workarounds with scroll physics – pskink May 05 '20 at 03:52
  • @WilliamTerrill that actually is something I want to avoid since I have a huge potential list to rebuild and that costs. – Kaki Master Of Time May 05 '20 at 16:50
  • @pskink I am trying to use it but I don't think it will solve my problem differently than a standard list view with dynamic extent would – Kaki Master Of Time May 05 '20 at 20:49
  • 1
    it will - for example if your list has 1000 items but only 5 are visible your builder will be called 5-10 times maybe - in your current solution it will be called 1000 times – pskink May 06 '20 at 05:19
  • @pskink I implemented it with some more widgets like persistent header one and got a fantastic result, thank you – Kaki Master Of Time May 06 '20 at 22:25
  • sure, your welcome, if you used persistent header you could try `SliverTransformer` too: https://gist.github.com/pskink/d9775e4dc8bbc5586070229760106031 – pskink May 07 '20 at 04:44
  • @KakiMasterOfTime can you please share your result that worked out well for you? – Tomas Baran May 05 '23 at 19:40

0 Answers0