How possible to keep scroll position in CustomScrollView

801 views Asked by At

I have a StatelessWidget

class MessagePage extends StatelessWidget {
  MessageProvider provider;
  ScrollController _scrollController = ScrollController();

  MessagePage() {
    _scrollController.addListener(() {
      if (_scrollController.position.maxScrollExtent ==
          _scrollController.position.pixels) {
        if (!isLoading) {
          isLoading = !isLoading;
          provider.getList(id: provider.lastItemVisited);
        }
      }
    });
  }

and a provider class to get data from webservice and then show's data in ui:

ChangeNotifierProvider<MessageProvider>(
          create: (context) => provider,
          child: Consumer<MessageProvider>(
            builder: (context, value, child) {
              if (value.state == ViewState.IDLE) {
                return CustomScrollView(
                  controller: _scrollController,
                  slivers: <Widget>[
                    SliverPadding(
                      padding: const EdgeInsets.only(
                          left: 28.0, right: 28.0, bottom: 28.0, top: 30),
                      sliver: SliverList(
                        delegate: SliverChildListDelegate(
                          initItems(context, value.messageModels),
                        ),
                      ),
                    ),
                  ],
                );
              }

When user swiped at the end of list I call my services and after getting result i consume this data child: Consumer<MessageProvider>( and shows into SliverList.

Everything is ok but the problem is here!!!

When i get new data my list position comes to the first list ? How possible to keep scroll position?

I see this post but i don't understand.

0

There are 0 answers