Flutter:在同一小部件​​中使用列表中项目的索引

问题描述

希望您平安无事,并做得很好。 好的,所以我的问题是我想使用cardBuilder的索引

cardBuilder: (context,index) => Card(

swipeUpdateCallback 中,我想在向右或向左滑动时使用它,该怎么办? 如何在swipeUpdateCallback中调用索引?

 swipeUpdateCallback:
          (DragUpdateDetails details,Alignment align) {
       
        if (align.x < 0) {
          _dataService.seenPost(
              welcomeImages[_index].id,///want to use the index here///
              welcomeImages[_index].seen,///want to use the index here///
              welcomeImages[_index].imgPath); ///want to use the index here///
        }

这是小部件的完整代码

child: new TinderSwapCard(
      swipeUp: true,swipeDown: true,orientation: AmassOrientation.BottOM,totalNum: welcomeImages.length,stackNum: 3,swipeEdge: 4.0,maxWidth: MediaQuery.of(context).size.width * 1.5,maxHeight: MediaQuery.of(context).size.width * 1.5,minWidth: MediaQuery.of(context).size.width * 1.4,minHeight: MediaQuery.of(context).size.width * 1.4,cardBuilder: (context,index) => Card( ///Here's the index i want to use///
        child: SingleChildScrollView(
          child: Column(
            children: [
              Text('s'),new Image.network(
                '${welcomeImages[index].imgPath}',fit: BoxFit.cover,),],cardController: controller = CardController(),swipeUpdateCallback:
          (DragUpdateDetails details,Alignment align) {
        if (align.x < 0) {
          _dataService.seenPost(
              welcomeImages[index].id,///want to use the index here///
              welcomeImages[index].seen,welcomeImages[index].imgPath);
        } else if (align.x > 0) {
          //Card is RIGHT swiping
        }
      },

解决方法

您可以使用<?php // Alternatives using array_sum // Ex. Ternary operator $numerators = [ $bean->year_1_noi_c,$bean->year_2_noi_c,$bean->year_3_noi_c ]; $denominators = [ $bean->year_1_c === null ? 0 : 1,$bean->year_2_c === null ? 0 : 1,$bean->year_3_c === null ? 0 : 1,]; $output = array_sum($numerators) / array_sum($denominators); /***********************************************************/ // Ex. Check is_null,inverse,cast to int $numerators = [ $bean->year_1_noi_c,$bean->year_3_noi_c ]; $denominators = [ (int)!is_null($bean->year_1_c),(int)!is_null($bean->year_2_c),(int)!is_null($bean->year_3_c) ]; $output = array_sum($numerators) / array_sum($denominators); 保存当前的swipeCompleteCallback并在index中使用
但是,如果您使用swipeUpdateCallback,则您的swipeUpdateCallback会在滑动时被多次调用
我不知道您的用例,但您可以将_dataService代码放在_dataService
代码段

swipeCompleteCallback

完整的示例代码

    swipeUpdateCallback:
          (DragUpdateDetails details,Alignment align) {
        if (align.x < 0) {
          _dataService.seenPost(
              welcomeImages[currentIndex].id,///want to use the Index here///
              welcomeImages[currentIndex].seen,welcomeImages[currentIndex].imgPath);
        } else if (align.x > 0) {
          //Card is RIGHT swiping
        }
      },swipeCompleteCallback:
                (CardSwipeOrientation orientation,int index) {
              currentIndex = index;
              print("$currentIndex ${orientation.toString()}");

              /// Get orientation & index of swiped card!
            },

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...