flutter_tindercard icon indicating copy to clipboard operation
flutter_tindercard copied to clipboard

TinderSwapCard empty when rebuilding after last swipe

Open fgarciaDev opened this issue 4 years ago • 4 comments

I am using this to present items and don't distinguish between L/R swipes. After swiping thru all items, I rebuild the view, however, the TinderSwapCard widget remains empty. Is there a way to tell the TinderSwapCard to rebuild after last item is swiped? Don't know if this is a bug or enhancement request.

Below is what I am trying to do, but even though I rebuild, TinderSwapCard never updates after the last card is dismissed.

import 'package:my_app/Components/itemRequest.dart';
import 'package:my_app/Screen/Request/requestDetail.dart';
import 'package:my_app/data/Model/item.dart';
import 'package:flutter_tindercard/flutter_tindercard.dart';

class CardsContainer extends StatefulWidget {
  final List<Item> items;

  const CardsContainer({Key key, @required this.items}) : super(key: key);

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


class _CardsContainerState extends State<CardsContainer> {
  TinderSwapCard cards;

  @override
  Widget build(BuildContext context) {
    if (cards == null) {
      cards = buildCards();
    }

    return Container(
      height: 330,
      child: cards,
    );
  }

  TinderSwapCard buildCards() {
    return TinderSwapCard(
      orientation: AmassOrientation.BOTTOM,
      totalNum: widget.items.length,
      stackNum: 5,
      maxWidth: MediaQuery.of(context).size.width,
      minWidth: MediaQuery.of(context).size.width * 0.9,
      maxHeight: MediaQuery.of(context).size.width * 0.9,
      minHeight: MediaQuery.of(context).size.width * 0.85,
      cardBuilder: (context, index) => ItemRequest(
        avatar: widget.items[index].avatar,
        userName: widget.items[index].name,
        date: widget.items[index].date.toString(),
        onTap: (){
          Navigator.of(context).push(MaterialPageRoute(builder: (context) => ItemDetail()));
        },
      ),
      swipeCompleteCallback: (CardSwipeOrientation orientation, int index) {
        // 
        // TRYING TO REBUILD CARD WIDGET TO DISPLAY ALL CARDS AGAIN
        // 
        if (index == widget.items.length-1) {
          setState(() {
            cards = null;
          });
        }
      }
    );
  }
}```

fgarciaDev avatar Aug 29 '20 20:08 fgarciaDev

@fgarciaDev Please have you been able to resolve this issue, if you have can you paste the solution here thanks.

Loisgenesis avatar Oct 23 '20 20:10 Loisgenesis

@fgarciaDev @Loisgenesis You need to detect the last swipe or 3-4 swipes before the last swipe based on index and total length of your list. Once you got that, you need to append data into the list you are passing in the widget. Let me know if you need any more help, I have resolved it in my project.

dzzzzmodi avatar Nov 10 '20 08:11 dzzzzmodi

I've got the same problem. I'm quite confused about the question how the cardBuilder is dealing with the index.

Let's say I've got a list of only two items: items[0] and items[1]. When swiping through this list I want to start with a certain index and then, if the end or the beginning of the list is reached 'switch' over to the beginning or the end respectively. Such as (if index > 1 then index = 0 or if index < 0 then index = 1.

I can't get this working :-( But why not? Let me explain...

Reason1: I can not set a starting index. The stack of cards always starts with 0. That's why I can not use buildCard(index) in the cardBuilder. If I want to start with items[myCurrentIndex] this fails. So I start with buildCard(myCurrentIndex) and that works! The correct card is shown.

Reason2: Let's say, when I swipe left, the index should increase and when I swipe right the index should decrease. OK! I can detect the direction of swiping :-) so simply increase / decrease myCurrentIndex dependent on the swipe direction! And when I reach the upper end (2 in the example) I set it to 0 and when I reach the lower end (-1 in the example) I set it to 1. Sounds easy.. BUT: Surprise... Surprise... This only works once :-O After buildCard(myCurrentIndex) the second time the card is empty :-O

This is a really good package. And I took the time to explain a failing simple but everyday usecase to make it possible to enhance it. If I overlooked a feature, please let me know.

akreienbring avatar Nov 20 '20 17:11 akreienbring

hello I also have the same problem when I append list with the set state that time it will start with first card

vitulgoyani avatar Mar 20 '21 10:03 vitulgoyani