2

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

Yahya
  • 210
  • 2
  • 12

3 Answers3

4

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

Yahya
  • 210
  • 2
  • 12
1
// 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;
  });
}
0
import 'dart:async';
import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final _controller = ScrollController();
  
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Timer(
      Duration(seconds: 2),
          () => _controller.animateTo(
            _controller.position.maxScrollExtent,
            duration: Duration(seconds: 2),
            curve: Curves.fastOutSlowIn,
          ),
    );
    return Scaffold(
      body: ListView.builder(itemBuilder:(context, index) {
        return Text('hi');
      },
     controller: _controller,
      )
    );
  }
}

Ujjwal Raijada
  • 867
  • 10
  • 21