AnupKumarPanwar / swipe_cards

Tinder like swipe cards for flutter.
https://pub.dev/packages/swipe_cards
BSD 3-Clause "New" or "Revised" License
52 stars 47 forks source link

How to add more cards underneath existing cards dynamically? #37

Open topex-psy opened 4 months ago

topex-psy commented 4 months ago

This is my need: After swiping, when there are only 5 cards left, I need to insert/append more cards

How to achieve this?

AnupKumarPanwar commented 4 months ago

As of swipe_cards: ^2.0.0+1, there is no direct method to handle this. But you can achieve this with the following approach.

Check addNewCards(); // Add this line on the 5th last card you add.

_swipeItems.add(SwipeItem(
          content: Content(text: _names[i], color: _colors[i]),
          likeAction: () {
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text("Liked ${_names[i]}"),
              duration: Duration(milliseconds: 500),
            ));
            addNewCards();  // Add this line on the 5th last card you add.
          },
          nopeAction: () {
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text("Nope ${_names[i]}"),
              duration: Duration(milliseconds: 500),
            ));
            addNewCards();  // Add this line on the 5th last card you add.
          },
          superlikeAction: () {
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text("Superliked ${_names[i]}"),
              duration: Duration(milliseconds: 500),
            ));
            addNewCards();  // Add this line on the 5th last card you add.
          },
          onSlideUpdate: (SlideRegion? region) async {
            print("Region $region");
          }));
topex-psy commented 4 months ago

As of swipe_cards: ^2.0.0+1, there is no direct method to handle this. But you can achieve this with the following approach.

Check addNewCards(); // Add this line on the 5th last card you add.

_swipeItems.add(SwipeItem(
          content: Content(text: _names[i], color: _colors[i]),
          likeAction: () {
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text("Liked ${_names[i]}"),
              duration: Duration(milliseconds: 500),
            ));
            addNewCards();  // Add this line on the 5th last card you add.
          },
          nopeAction: () {
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text("Nope ${_names[i]}"),
              duration: Duration(milliseconds: 500),
            ));
            addNewCards();  // Add this line on the 5th last card you add.
          },
          superlikeAction: () {
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text("Superliked ${_names[i]}"),
              duration: Duration(milliseconds: 500),
            ));
            addNewCards();  // Add this line on the 5th last card you add.
          },
          onSlideUpdate: (SlideRegion? region) async {
            print("Region $region");
          }));

Thank you. But what does the addNewCards() function looks like? Is it just adding new items to _swipeItems list variable? Should we recreate the MatchEngine?

Can you give more complete example?

AnupKumarPanwar commented 4 months ago

You can just add new items to _swipeItems and call setState()