Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen to index changes #62

Closed
guihackmann opened this issue Jul 28, 2021 · 4 comments
Closed

Listen to index changes #62

guihackmann opened this issue Jul 28, 2021 · 4 comments

Comments

@guihackmann
Copy link

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
Copy link
Member

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

@guihackmann
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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"),
          ),
        )),
      )
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants