0

I have a list of report items.

I initialised a scrollController here:

 class ReportList extends StatelessWidget {

  ReportList({Key? key}) : super(key: key);

  final ScrollController scrollController = ScrollController();

  scrollDown() {
    scrollController.animateTo(scrollController.position.maxScrollExtent,
        duration: const Duration(seconds: 1), curve: Curves.bounceOut);
  }

I also have a list view with the controller, and I want the list view to stroll to bottom when the last item is selected:

ListView.separated(
        controller: scrollController,
        itemCount: 13,
        itemBuilder: (_, index) {
          return Column(
            children: [
              ReportItem(
                onClick: () {
                  viewModel.setCommentReportStat(index);
                  if (index == 12 && viewModel.commentReportStat[12]) {
                    scrollDown();
                  }
                  debugPrint(viewModel.commentReportStat.toString());
                },
                isSelected: viewModel.commentReportStat[index],
                title: CommentReport.items[index],
              ),
              //show additional message field to the user
              if (index == 12 && viewModel.commentReportStat[12])
                AnimatedContainer(
                  duration: const Duration(seconds: 1),
                  child: MessageField(commentId: commentId),
                )
            ],
          );
        },
        separatorBuilder: (_, index) => const SizedBox(
          height: 16,
        ),
      )

Everything about the state management works since the message field appears as the user clicks on the last item.

My problem in general is that neither does message field appear with the animated container which is wrapped around it, nor does it scroll down!

How it is right now: enter image description here

What I want to happen: enter image description here

(If I scroll manually, I can see the entire message field, but I want this to be automated using the scroll controller)

2
  • when last item showed, the list already is at the bottom. why do you want to scroll? Commented Oct 4, 2022 at 7:25
  • I added two images to better illustrate what I mean Commented Oct 4, 2022 at 7:38

1 Answer 1

1

instead of this

 if (index == 12 && viewModel.commentReportStat[12])
                AnimatedContainer(
                  duration: const Duration(seconds: 1),
                  child: MessageField(commentId: commentId),
                )

use this

AnimatedCrossFade(
  duration: const Duration(seconds: 1),
  firstChild: const SizedBox(),
  secondChild: MessageField(commentId: commentId),
  crossFadeState: (index == 12 && viewModel.commentReportStat[12]) ? CrossFadeState.showSecond : CrossFadeState.showFirst,
)

You can also use this function if you want to scroll to the end of the page when an event occurs.

toMaxScroll() {
       _scrollController.animateTo(_scrollController.position.maxScrollExtent,duration:const Duration(milliseconds:300),curve:Curves.easeIn);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This helped me with the animation of my MessageField; however, It still doesn't scroll down as I want it to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.