serenader2014 / flutter_carousel_slider

A flutter carousel widget, support infinite scroll, and custom child widget.
https://pub.dev/packages/carousel_slider
MIT License
1.55k stars 517 forks source link

Why pageViewIndex start from 10000 ? #392

Open DanielMohammadii opened 1 year ago

DanielMohammadii commented 1 year ago
Class SugarPhotoPage extends StatefulWidget {
  const SugarPhotoPage({super.key});

  @override
  State<SugarPhotoPage> createState() => _SugarPhotoPageState();
}

class _SugarPhotoPageState extends State<SugarPhotoPage> {
  final Stream<QuerySnapshot> _photoStream =
      FirebaseFirestore.instance.collection('photos').snapshots();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: StreamBuilder<QuerySnapshot>(
          stream: _photoStream,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return const Text('Something went wrong');
            }

            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text("Loading");
            }

            if (snapshot.hasData) {
              return CarouselSlider.builder(
                  options: CarouselOptions(height: 400.0),
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder:
                      (BuildContext context, int itemIndex, int pageViewIndex) {
                    return Container(
                      child: Text('Page number: $pageViewIndex'),
                    );
                  });
            }
            return Text('Loading');
          }),
    );
  }
}
  1. Its been couple days , I'm struggling with an issue and just found out the pageViewIndex start from 10000, plus here How we can control the number of page here , I
dhruv1710 commented 1 year ago

The item count property in carousel slider builder defines the number of pages. You may have some problem in sanpshot.data!.docs.length

spegoraro commented 1 year ago

I'm also seeing this, even if I manually set itemCount to a literal like 10 my pageViewIndex starts at 10000.

Edit: I just had a look and it's set manually in carousel_state.dart#L21 in order to allow infinite scrolling which makes sense. However I'm also noticing that it often gets the index wrong. E.g. if I scroll to the next or previous item but then continue to keep the drag going and reverse it back to the original item it reports the pageViewIndex incorrectly.

dhruv1710 commented 1 year ago

You can disable infinite scrolling in options and for the drag issue you will need to update controller about the page. You could add left and right buttons or pagination dots.