问题描述
希望您平安无事,并做得很好。 好的,所以我的问题是我想使用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!
},