HeavenOSK / flutter_swipable_stack

A widget for stacking cards, which users can swipe horizontally and vertically with beautiful animations like Tinder UI.
https://pub.dev/packages/swipable_stack
MIT License
117 stars 48 forks source link

Builder Index not getting reset when list is initialize with new data. #35

Closed TouseefAQ closed 2 years ago

TouseefAQ commented 2 years ago

I have two mode is my app with two differnt list. In mode 1 i swipe for example 3 profile but when i switch to mode 2 my builder index remains 2. I don't if it is an isssue but this is not working as it should be. It should build from index 0 every time new list is feed.

HeavenOSK commented 2 years ago

@TouseefAQ I think that's not bug of swipable_stack 🤔

I had a similar situation, and I did the following :

  1. prepare two different Widgets which builds different SwipableStack.
  2. switch visibility for the widgets of procedure 1.

sample code is like following (it's not full code):

class ModeSwitchingWidget extends StatelessWidget {
  const ModeSwitchingWidget({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return CircularProgressIndicator();
    }
    if (mode === Mode.a) {
      return ASwipableStack();
    }
    return BSwipableStack();
  }
}

class ASwipableStack extends StatefulWidget {
  const ASwipableStack({Key? key}) : super(key: key);

  @override
  State<ASwipableStack> createState() => _ASwipableStackState();
}

class _ASwipableStackState extends State<ASwipableStack> {
  late final SwipableStackController _controller = SwipableStackController();

  @override
  Widget build(BuildContext context) {
    return SwipableStack(
      controller: _controller,
    );
  }
}

class BSwipableStack extends StatefulWidget {
  const BSwipableStack({Key? key}) : super(key: key);

  @override
  State<ASwipableStack> createState() => _BSwipableStackState();
}

class _BSwipableStackState extends State<BSwipableStack> {
  late final SwipableStackController _controller = SwipableStackController();

  @override
  Widget build(BuildContext context) {
    return SwipableStack(
      controller: _controller,
    );
  }
}
TouseefAQ commented 2 years ago

Thank you for your solution , I have used different approach , as i am using statemanagement , every time if switch the mode, i initialize the controller with initial index 0. is there any drawback of using my approach ? resetControllerIndex() { _Swipecontroller = SwipableStackController(initialIndex: 0) ..addListener(notifyListeners); notifyListeners(); }

HeavenOSK commented 2 years ago

@TouseefAQ I feel your solution is not bad 🤔 But ‘SwipableStack’ internally uses unique GlobalKey, so that maybe it could be broken depending on frame updating timing.

So I recommend you to regenerate both controller and SwipableStack when replacing content.

TouseefAQ commented 2 years ago

Thank you very much.