Flutter ScrollController is not work properly

4.3k views Asked by At

In my flutter app, I try to use scrollController to scroll listview in listBuilder but ScrollController is not working properly. It doesn't show errors or exceptions but the List is not scrolling. Even I use scroll controller jumpTo or animateTo it does not work. Even I give the maximum value like 1000 and 20000 in animateTo and jumpTo it does not scroll.

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class Class extends StatefulWidget {
final int data;
final List list;
Class({required this.data, required this.list});
@override
_ClassState createState() => _ClassState();
}

class _ClassState extends State<Class> with TickerProviderStateMixin {
late Animation<double> animation;
late AnimationController animationController;
late double height = 0;

final List lst = [
 [true, true, true, true],
[false, false, false, false],
[true, false, false, false],
[true, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
];

void initState() {
  super.initState();

animationController =
    AnimationController(vsync: this, duration: Duration(seconds: 5));

animation = Tween<double>(begin: 0, end: 200).animate(animationController)
  ..addListener(() {
    setState(() {});
  })
  ..addStatusListener((status) {});
animationController.forward();
 }

Widget container(data) {
return Stack(
  children: [
    GestureDetector(
      onTap: () {
        setState(() {
          animationController.forward();
        });
      },
      child: Transform.translate(
        offset: Offset(0, animation.value),
        child: Container(
          color: lst[data][0] ? Colors.black : Colors.transparent,
          height: MediaQuery.of(context).size.height / 4,
          width: MediaQuery.of(context).size.width / 4,
          child: Text('$data'),
        ),
      ),
    ),
  ],
);
 }

 @override
 Widget build(BuildContext context) {
return container(widget.data);
  }

 @override
 void dispose() {
super.dispose();
animationController.dispose();
  }
 }

class Yes extends StatefulWidget {
 @override
_YesState createState() => _YesState();
  }

 class _YesState extends State<Yes> {
   List lst = [
[true, true, true, true],
[false, false, false, false],
[true, false, false, false],
[true, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
   ];

 ScrollController _scrollController = ScrollController();

 void initState() {
if (_scrollController.hasClients) {
  super.initState();
  // _scrollController.animateTo(0,
  //     // _scrollController.offset + MediaQuery.of(context).size.height/6,
  //     duration: Duration(seconds: 2),
  //     curve: Curves.linear);
  _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
    }
   }

 @override
 Widget build(BuildContext context) {
    return ListView(
  controller: _scrollController,
  //  physics: NeverScrollableScrollPhysics(),
  children: [
    ListView.builder(
          // physics: NeverScrollableScrollPhysics(),
        reverse: true,
        itemCount: lst.length,
        controller: _scrollController,
        itemBuilder: (BuildContext context, int position) {
          return Stack(children: [Class(data: position, list: lst)]);
        }),
  ],
);
  }
   }
1

There are 1 answers

0
Oscar Shaitan Tigreros On

your code is not clear enough but try this as example is working just fine

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: ListExample(),
    );
  }
}

class ListExample extends StatelessWidget {
  ListExample({Key? key}) : super(key: key);

  final ScrollController controller = ScrollController();
  double pos = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              controller: controller,
              itemCount: 10000,
              itemBuilder: (context, index) {
                return Text('item $index');
              },
            ),
          ),
          ElevatedButton(
              onPressed: () {
                controller.jumpTo(pos+=250);
              },
              child: Text(' Jump'))
        ],
      ),
    );
  }
}