Flutter scroll slowly to the bottom of a dynamic Listview

449

Solution 1

The issue was caused from the animation itself not the duration. I solved it by increasing the duration and setting curve: Curves.linear.

Solution 2

// Declaring the controller and the item size
ScrollController _scrollController;
final itemSize = 100.0;

// Initializing
@override
void initState() {
  _scrollController = ScrollController();
  _scrollController.addListener(_scrollListener);
  super.initState();
}

// Your list widget (must not be nested lists)
ListView.builder(
      controller: _scrollController,
      itemCount: <Your list length>,
      itemExtent: itemSize,
      itemBuilder: (context, index) {
          return ListTile(<your items>);
      },
),

// With the listener and the itemSize, you can calculate which item
// is on screen using the provided callBack. Something like this:
void _scrollListener() {
  setState(() {
    var index = (_scrollController.offset / itemSize).round() + 1;
  });
}
Share:
449
Yahya
Author by

Yahya

Graduate With CS Degree from College. Proficient in: HTML CSS JQuery PHP VB.Net C# Java for Android Development Swift 4 &amp; 5 for IOS Development Swift UI Flutter

Updated on December 29, 2022

Comments

  • Yahya
    Yahya over 1 year

    I have a list view that its item may vary each time. I want to make auto scroll for it.

    I tried this code

    scrollController.animateTo(
          scrollController.position.maxScrollExtent,
          duration: Duration(seconds: 10),
          curve: Curves.easeOut);
    

    It works perfectly for small list view but when the list view items are 100 or more it start moving so fast.

    I also tried to make the duration longer when the list view have more items but it mess up

    • Ujjwal Raijada
      Ujjwal Raijada about 3 years
      are you getting the data through api?
    • Yahya
      Yahya about 3 years
      the data isn't a problem I'm getting the data correctly from local db.
    • Ujjwal Raijada
      Ujjwal Raijada about 3 years
      are you getting all the data at once? Or you are using pagination?
    • Yahya
      Yahya about 3 years
      Yes im getting the data at once and filling the listview then I want It to start scrolling.
  • Yahya
    Yahya about 3 years
    No time isn't the problem. I already said that when I set longer time it still works put it moves in a messy way. like it stops then move
  • Tasnuva Tavasum oshin
    Tasnuva Tavasum oshin about 3 years
    scrollController.position.maxScrollExtent Change this
  • Yahya
    Yahya about 3 years
    Change it to what ?
  • Tasnuva Tavasum oshin
    Tasnuva Tavasum oshin about 3 years
    i have changed my answer have a look
  • Yahya
    Yahya about 3 years
    The items size aren't static it will change based on the content inside it. Is there any other solution without using the height
  • Tasnuva Tavasum oshin
    Tasnuva Tavasum oshin about 3 years
    use custom scroll
  • Yahya
    Yahya about 3 years
    This code waits two seconds then it starts scrolling very fast. My problem is with the fast scrolling but its not a time issue
  • Ujjwal Raijada
    Ujjwal Raijada about 3 years
    you can use Curves.easeIn or any other class. There are many options available.
  • Ujjwal Raijada
    Ujjwal Raijada about 3 years
  • Yahya
    Yahya about 3 years
    Thank you Ujjwal. I solved it by setting the curve to Curves.linear.
  • Yahya
    Yahya about 3 years
    No need for that.