quire-io / scroll-to-index

scroll to index with fixed/variable row height inside Flutter scrollable widget
MIT License
511 stars 104 forks source link

Listen to index changes #62

Closed guihackmann closed 3 years ago

guihackmann commented 3 years ago

Hi @jerrywell,

Is it possible to listen to index changes? That would be really awesome.

I've seen this features in other packages, but then they don't other good features scroll-to-index has.

Thanks.

jerrywell commented 3 years ago

not pretty sure what the index changes means? may you explain more about it?

guihackmann commented 3 years ago

Hi @jerrywell,

Sure. So the way this works, whenever we click on an index, the list scrolls down to that index. Which is great. Now, if we scroll down the list manually, I’d like to make an action every time the index changes (from 0 to 1, 1 to 2, etc). But I couldn’t find a way to listen for the index changes, I could only listen to pixel changes.

Any thoughts?

jerrywell commented 3 years ago

you can listen to the scroll controller's event to get the offset change, meanwhile, just look up the index of the position you specified by following the comment

guihackmann commented 3 years ago

Hi @jerrywell,

I followed these steps, but unfortunately I couldn't make this work.

Please find below an example code of what I need. Would you be able to guide me what I need to do where I have the "???"?

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

class Teste extends StatefulWidget {
  @override
  _TesteState createState() => _TesteState();
}

class _TesteState extends State<Teste>  with TickerProviderStateMixin {
  AutoScrollController controller = AutoScrollController();

  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      //Listen to offset changes as I scroll - Working
      print(controller.offset);
      //Listen to what index is at the top of the screen
      //???
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        controller: controller,
        children: List.generate(50, (index) => AutoScrollTag(
          key: ValueKey(index),
          controller: controller,
          index: index,
          child: Padding(
            padding: const EdgeInsets.all(30.0),
            child: Text("Index $index"),
          ),
        )),
      )
    );
  }
}