Flutter 如何在我的颜色选择器上结束拖动后获取数据?
现在,每次我拖动选择器时,颜色都会改变。我不想每次都触发事件。结束后,我想得到颜色代码Flutter 如何在我的颜色选择器上结束拖动后获取数据?,flutter,dart,Flutter,Dart,现在,每次我拖动选择器时,颜色都会改变。我不想每次都触发事件。结束后,我想得到颜色代码 return GestureDetector( onPanDown: (details) => handleTouch(details.globalPosition, context), onPanStart: (details) => handleTouch(details.globalPosition, context), onPanUpdate: (details) =>
return GestureDetector(
onPanDown: (details) => handleTouch(details.globalPosition, context),
onPanStart: (details) => handleTouch(details.globalPosition, context),
onPanUpdate: (details) => handleTouch(details.globalPosition, context),
child: Stack(children: [frame, content, thumb]),
);
/// calculate colors picked from palette and update our states.
void handleTouch(Offset globalPosition, BuildContext context) {
RenderBox box = context.findRenderObject();
Offset localPosition = box.globalToLocal(globalPosition);
double percent;
if (widget.horizontal) {
percent = (localPosition.dx - widget.thumbRadius) / barWidth;
} else {
percent = (localPosition.dy - widget.thumbRadius) / barHeight;
}
percent = min(max(0.0, percent), 1.0);
setState(() {
this.percent = percent;
});
switch (widget.pickMode) {
case PickMode.Color:
Color color = HSVColor.fromAHSV(1.0, percent * 360, 1.0, 1.0).toColor();
widget.colorListener(color.value);
break;
case PickMode.Grey:
final int channel = (0xff * percent).toInt();
widget.colorListener(Color
.fromARGB(0xff, channel, channel, channel)
.value);
break;
}
}
试过了,结果不起作用
onPanEnd:(end)=>handleTouch(end.velocity.pixelsPerSecond,上下文)
完整代码出了什么问题
onPanDown:(details)=>handleTouch(details.globalPosition,context),
onPanStart:(details)=>handleTouch(details.globalPosition,context),
onPanUpdate:(details)=>handleTouch(details.globalPosition,context),
尝试删除以下一些不适合您所需行为的拖动回调
深入到回调中
onPanStart
///指针已通过主按钮与屏幕接触,并已开始移动
///移动。
///
///另见:
///
///*[kPrimaryButton],此回调响应的按钮。
最终手势DragStartCallback on PanStart;
onPanUpdate
///通过主按钮和
///搬家又搬家了。
///
///另见:
///
///*[kPrimaryButton],此回调响应的按钮。
最终手势DragUpdateCallback onPanUpdate;
现在您可以直接使用onPanDown
返回手势检测器(
//可能值得注意的是,在PanDown onPanEnd上查看您选择的行为
onPanDown:(details)=>handleTouch(details.globalPosition,context),
//取决于你是否还需要这个。
//onPanStart:(details)=>handleTouch(details.globalPosition,context),
子对象:堆栈(子对象:[框架、内容、拇指]),
);
onPanEnd
的详细信息不会记录任何职位的详细信息。
事实上,GestureDetector
类中没有任何属性可以帮助您记录最终位置
建议的解决方案:
最后一次调用onPanUpdate
details将显示最终位置
您可以将位置更新为变量(finalposition
),而不是每次都调用像handleTouch
这样的重载函数进行计算
当onPanEnd
处于活动状态时,使用可在GestureDetector小部件中访问的变量(finalPostion
)
onPanUpdate: (details) => updateFinalPosition(details.globalPosition),
onPanEnd: handleTouch( finalPosition, context)
onPanEnd不可用
详细信息。在这种情况下,globalPosition
Hi@BloodLoss。您不能只使用onPanDown
而不使用onPanUpdate
?