serenader2014 / flutter_carousel_slider

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

carousel slider not update after setState #167

Open nitinsun opened 4 years ago

nitinsun commented 4 years ago

i want custom carousel slider that update when every item add to array that assigned to array.

madhanKMani commented 4 years ago

I'm also facing the same issue. Did you find any solution?

akshaybhange commented 3 years ago

maybe adding key:GlobalKey() in CarouselSlider will help, let me know if that helps

pedro-santiago commented 3 years ago

I'm having the same issue, tried GlobalKey without success. It animates the slider but when it finishes and calls setState on onPageChanged it restarts to the first slide.

pedro-santiago commented 3 years ago

Custom Slider Code

                    CardSlider(
                        items: this._cards.map((card) => CardImageBox(card: card)).toList(),
                        onPageChanged: (index) => {
                          Timer(Duration(milliseconds: 500), (){
                            setState(() {
                              _currentCard = _cards[index];
                            });
                          })
                        }
                    )

Widget Code

class CardSlider extends StatefulWidget {
  final List<Widget> items;
  final Function(int index) onPageChanged;

  CardSlider({
    @required this.items,
    this.onPageChanged
  });

  @override
  _CardSliderState createState() => _CardSliderState();
}

class _CardSliderState extends State<CardSlider> {
  int _current = 0;
  GlobalKey key;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        CarouselSlider(
          key: key,
          options: CarouselOptions(
            disableCenter: false,
            height: 175,
            enableInfiniteScroll: false,
            viewportFraction: 0.70,
            onPageChanged: (index, reason) {
              widget.onPageChanged(index);
            },
          ),
          items: widget.items,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: widget.items.map(
            (idx) {
              int index = widget.items.indexOf(idx);
              Color color = _current == index ? Theme.of(context).primaryColor : Color.fromRGBO(0, 0, 0, 0.3);

              return SliderBullet(color: color);
            },
          ).toList(),
        ),
      ],
    );
  }
}
domtom1126 commented 2 years ago

@pedro-santiago did you ever find a fix for this? I am having the same problem.