2

If there is a way to implement automatically scrolling the SliverAppBar to a collapsed or expanded state at the moment the user stops scrolling in an intermediate position (between collapsedHeight and expandedHeight of SliverAppBar).

enter image description here

Viktar K
  • 1,409
  • 2
  • 16
  • 22
  • I think you'd have to implement it yourself. Afaik, there is no flutter widget that provides that functionality. You would be using animations and scrollcontrollers to make that effect. I'm not entirely sure if there is any package for this but it would be better to use packages since someone has already written it for you – CoderUni Nov 07 '20 at 10:02

1 Answers1

0

Not sure if this is the best approach, but it works:

     final _controller = ScrollController();

     ...

     Scaffold(
        body: NotificationListener<ScrollNotification>(
      onNotification: (scrollNotification) {
        if (scrollNotification is ScrollEndNotification &&
            scrollNotification.depth == 0) {
          final minExtent = scrollNotification.metrics.minScrollExtent;
          final maxExtent = scrollNotification.metrics.maxScrollExtent;
          final middle = (maxExtent - minExtent) / 2;
          final pos = scrollNotification.metrics.pixels;

          double scrollTo;
          if (minExtent < pos && pos <= middle)
            scrollTo = minExtent;
          else if (middle < pos && pos < maxExtent) scrollTo = maxExtent;
          if (scrollTo != null)
            // Doesn't work without Timer
            Timer(
                Duration(milliseconds: 1),
                () => _controller.animateTo(scrollTo,
                    duration: Duration(milliseconds: 300),
                    curve: Curves.ease));
        }
        return false;
      },
      child: NestedScrollView(
          controller: _controller,
          ...

working example

Viktar K
  • 1,409
  • 2
  • 16
  • 22