appinioGmbH / flutter_packages

Dart and Flutter plugins/packages used and maintained by @appinioGmbH
193 stars 224 forks source link

[Appinio Swiper] PageController inside a card does not reset when I swipe to another card #128

Closed giacomomasseron closed 1 year ago

giacomomasseron commented 1 year ago

Hi,
I have a card with a PageController inside. If I change a page and swipe to another card, I have the PageController index already set to the previous card index, not a 0 like it supposed to be.

This it the widget container:

AppinioSwiper(
  onEnd: () {},
  onSwipe: (index, direction) {},
  cardsCount: state.filteredItems.length,
  onSwiping: (AppinioSwiperDirection direction) {},
  cardsBuilder: (BuildContext context, int index) {
    var currentItem = state.filteredItems[index];
    if (currentItem is MyItem) {
      return MyTinderCard(
        item: currentItem,
        backgoundColor: Colors.white,
        heroKey: index.toString(),
        state: CardExpansionState.Animating,
      );
    }
    return Container(
      color: Colors.green,
    );
  },
)

This is the MyTinderCard widget:

class MyTinderCard extends StatefulWidget {
  final MyItem item;

  const MyTinderCard({
    super.key,
    required this.item,
  });

  @override
  State<MyTinderCard> createState() => _MyTinderCardState();
}

class _MyTinderCardState extends State<MyTinderCard> {
  late final PageController _controller;

  @override
  void initState() {
    debugPrint("init");
    super.initState();

    _controller = PageController(initialPage: 0);
  }

  @override
  void didChangeDependencies() {
    debugPrint("didChange");
    super.didChangeDependencies();
  }

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

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(40),
      color: Colors.cyanAccent,
      child: PageView.builder(
        itemCount: (widget.item.children + 1),
        pageSnapping: true,
        reverse: false,

        controller: _controller,
        itemBuilder: (context, paginatedIndex) {
          if (paginatedIndex == 0) {
            return Container(
              color: Colors.red,
              width: 100,
              child: Image.asset(
                "img_1.png",
                fit: BoxFit.fitHeight,
              ),
            );
          }

          return Container(
            color: Colors.amberAccent,
            width: 100,
            child: Image.asset(
              "img_2.png",
              fit: BoxFit.fitHeight,
            ),
          );
        },

        onPageChanged: (index) {
          _currentPageBottle = index;
          _resizeBottle(index: index);
          widget.onIndexChanged?.call(index);
        },
      ),
    );
  }
}

As you can see, the widgets are pretty simple. If I change PageViewController page and swipe the card, in the next card the controller does have the index different from 0.

khanmujeeb687 commented 1 year ago

Hey @giacomomasseron, So you want the index of the card at the top to always be 0 right??

mt-ks commented 1 year ago

Same issue.

khanmujeeb687 commented 1 year ago

Hey @giacomomasseron , @mehmetbeyHZ , Did you tried adding unique key for every card? This is the same issue which previously existed here. : https://github.com/appinioGmbH/flutter_packages/issues/54