简体   繁体   English

无法在 Flutter/Dart 中添加操作按钮

[英]Unable to add an action button in Flutter/Dart

I have spent my whole day trying to add a simple button that would perform the same action as a swipe right on the cards in the following flutter widget.我花了一整天的时间尝试添加一个简单的按钮,该按钮将执行与在以下 flutter 小部件中的卡片上向右滑动相同的操作。 I however can't seem to make it work.然而,我似乎无法让它发挥作用。 Here is my code for now:这是我现在的代码:

class TinderCards2 extends StatefulWidget {
  @override
  _TinderCards2State createState() => _TinderCards2State();
}

class _TinderCards2State extends State<TinderCards2> with SingleTickerProviderStateMixin {


  // Add a counter variable
  int counter = 0;
  late AnimationController _animationController = AnimationController(
    vsync:  this,  // pass the SingleTickerProviderStateMixin instance as the vsync parameter
    duration: Duration(milliseconds: 500),
  );
  List<AnimatedPositioned> cards = [
    AnimatedPositioned(left: 0, top: 0, duration: Duration(milliseconds: 500),
      child: Card(
        child: Container(
            width: 400.0,height: 400.0,decoration: BoxDecoration(
            image: DecorationImage(image: AssetImage("assets/1.png"),fit: BoxFit.cover)),
            padding: EdgeInsets.all(16.0)), ),
    ),
    AnimatedPositioned(left: 0, top: 0, duration: Duration(milliseconds: 500),
      child: Card(
        child: Container(
            width: 400.0,height: 400.0,decoration: BoxDecoration(
          image: DecorationImage(image: AssetImage("assets/2.png"),fit: BoxFit.cover,),),
            padding: EdgeInsets.all(16.0)), ),
    ),
    
  ];

  @override
  int currentCardIndex = 0;
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 500),
    );
  }
  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  Widget build(BuildContext context) {
    return Container(
      // Set the background color to pink
      color: Colors.pink,
      child: Center(
        // Wrap the Stack widget in a Container
        child: Container(
          // Set the height and width to a larger value
            height: 400.0,
            width: 400.0,
            // Use a Stack widget to stack the cards on top of each other

            child: Stack(
              children: cards.map((card) {
                return GestureDetector(
                  // Handle swipe gestures
                  onPanUpdate: (details) {
                    // Check the direction of the swipe
                    if (details.delta.dx > 0) { // Use last card's name
                      // Swiped right
                      setState(() {
                        int index = cards.indexOf(card);
                        currentCardIndex = index;
                        cards.removeAt(index);
                        cards.insert(index, AnimatedPositioned(left: 1000, top: 0, duration: Duration(milliseconds: 500), child: card.child));
                        right2.add(index);
                        ;});
                    } else if (details.delta.dx < 0) {

                      // Swiped left
                      setState(() {
                        int index = cards.indexOf(card);
                        currentCardIndex = index;
                        cards.removeAt(index);
                        cards.insert(index, AnimatedPositioned(left: -1000, top: 0, duration: Duration(milliseconds: 500), child: card.child));
                        left2.add(index);
                        ;});
                    }  /*else if (details.delta.dy > 0) {
                      setState(() {
                        int index = cards.indexOf(card);
                        currentCardIndex = index;
                        cards.removeAt(index);
                        cards.insert(index, AnimatedPositioned(left: 0, top: 2000, duration: Duration(milliseconds: 500), child: card.child));
                        down2.add(index);

                      });
                    }*/;

                  },
                  onPanEnd: (details) {
                    if (currentCardIndex == 0) {
                      // Navigate to a new screen
                      Navigator.pushNamed(context, '/GroupGame');
                    }
                  },
                  child: Stack(
                    children: [
                      card,
                    ],
                  ),
                );
              }).toList(),
              alignment: Alignment.center,
            )
        ),
      ),
    );
  }
}

Do you know what I could try?你知道我可以尝试什么吗? Any help is greatly appreciated.任何帮助是极大的赞赏。

I tried wrapping the Stack widget containing the swipeable cards in a Column widget and adding a TextButton widget as the last child but never got it to work.我尝试将包含可刷卡的 Stack 小部件包装在 Column 小部件中,并将 TextButton 小部件添加为最后一个子部件,但从未让它工作。

Can't help you at all with your specific question, but without any other context I'd suggest you to not try to reinvent the wheel.根本无法帮助您解决您的具体问题,但在没有任何其他背景的情况下,我建议您不要尝试重新发明轮子。 There are many packages already doing Tinderlike cards.已经有很多软件包在做类似 Tinder 的卡片。 I'd suggest this one specifically since it is the most similar and with many customizable options.我特别推荐这个,因为它最相似并且有许多可定制的选项。

https://pub.dev/packages/swipable_stack https://pub.dev/packages/swipable_stack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM