Flutter 在颤振状态下悬停在Dragtarget上时可拖动触发错误信息
我在《颤振》中使用拖拽物和拖拽目标。我目前在我的项目中有两个DragTarget,当我在上面放置Dragable时,它们都能正确响应。但是,从DragTable1到dragtarget1的第一次拖动完成后,第二次拖动会出现一个问题:当使用DragTable2悬停(未放下)在dragtarget2上时,会触发onaccept属性,并使用dragtarget1的内容构建dragtarget2。我不明白为什么。如有任何帮助/提示,我将不胜感激 我制作了一个视频短片,以直观的方式展示了这一问题: 以下是我的dragtarget代码:Flutter 在颤振状态下悬停在Dragtarget上时可拖动触发错误信息,flutter,draggable,dragtarget,Flutter,Draggable,Dragtarget,我在《颤振》中使用拖拽物和拖拽目标。我目前在我的项目中有两个DragTarget,当我在上面放置Dragable时,它们都能正确响应。但是,从DragTable1到dragtarget1的第一次拖动完成后,第二次拖动会出现一个问题:当使用DragTable2悬停(未放下)在dragtarget2上时,会触发onaccept属性,并使用dragtarget1的内容构建dragtarget2。我不明白为什么。如有任何帮助/提示,我将不胜感激 我制作了一个视频短片,以直观的方式展示了这一问题: 以下是
Widget build(BuildContext context) {
bool isSuccessful = false;
int caughtData;
return SafeArea(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
height: 70,
width: 200,
color: Colors.grey.shade600,
child: DragTarget<int>(
builder: (context, List<int> candidateData, rejectedData) {
return isSuccessful
? FlatButton(
color:
chordBrain.chordBank[caughtData].buttoncolor,
child:
Text(chordBrain.chordBank[caughtData].chord),
onPressed: () {
playSound(noteBrain.noteBank[caughtData].note1);
playSound(noteBrain.noteBank[caughtData].note2);
playSound(noteBrain.noteBank[caughtData].note3);
playSound(noteBrain.noteBank[caughtData].note4);
},
)
: Container();
},
onWillAccept: (int data) {
print('$data');
return true;
},
onAccept: (int data) {
print('$data');
isSuccessful = true;
caughtData = data;
},
),
),
),
SizedBox(
width: 8,
),
Expanded(
child: Container(
height: 70,
width: 200,
color: Colors.grey.shade600,
child: DragTarget<int>(
builder: (context, List<int> candidateData, rejectedData) {
return isSuccessful
? FlatButton(
color:
chordBrain.chordBank[caughtData].buttoncolor,
child:
Text(chordBrain.chordBank[caughtData].chord),
onPressed: () {
playSound(noteBrain.noteBank[caughtData].note1);
playSound(noteBrain.noteBank[caughtData].note2);
playSound(noteBrain.noteBank[caughtData].note3);
playSound(noteBrain.noteBank[caughtData].note4);
},
)
: Container();
},
onWillAccept: (int data) {
print('$data');
return true;
},
onAccept: (int data) {
print('$data');
isSuccessful = true;
caughtData = data;
},
),
),
),
],
),
小部件构建(构建上下文){
bool ISSUCCESS=错误;
int caughtData;
返回安全区(
子:列(
儿童:[
划船(
儿童:[
扩大(
子:容器(
身高:70,
宽度:200,
颜色:Colors.grey.shade600,
儿童:DragTarget(
生成器:(上下文、列表候选数据、拒绝数据){
返回成功
?扁平按钮(
颜色:
chordBrain.chordBank[caughtData].按钮颜色,
儿童:
文本(chordBrain.chordBank[caughtData].chord),
已按下:(){
playSound(noteBrain.noteBank[caughtData].note1);
playSound(noteBrain.noteBank[caughtData].note2);
playSound(noteBrain.noteBank[caughtData].note3);
playSound(noteBrain.noteBank[caughtData].note4);
},
)
:容器();
},
onWillAccept:(int数据){
打印(“$data”);
返回true;
},
onAccept:(int数据){
打印(“$data”);
isSuccessful=真;
caughtData=数据;
},
),
),
),
大小盒子(
宽度:8,
),
扩大(
子:容器(
身高:70,
宽度:200,
颜色:Colors.grey.shade600,
儿童:DragTarget(
生成器:(上下文、列表候选数据、拒绝数据){
返回成功
?扁平按钮(
颜色:
chordBrain.chordBank[caughtData].按钮颜色,
儿童:
文本(chordBrain.chordBank[caughtData].chord),
已按下:(){
playSound(noteBrain.noteBank[caughtData].note1);
playSound(noteBrain.noteBank[caughtData].note2);
playSound(noteBrain.noteBank[caughtData].note3);
playSound(noteBrain.noteBank[caughtData].note4);
},
)
:容器();
},
onWillAccept:(int数据){
打印(“$data”);
返回true;
},
onAccept:(int数据){
打印(“$data”);
isSuccessful=真;
caughtData=数据;
},
),
),
),
],
),
看来我已经找到了一个解决方案,它不是很优雅,但很有效。尽管我仍然不明白为什么我的拖拽功能会在悬停(而不是放下)时触发构建在dragtargets上,通过使dragtargets彼此更加不同,问题得到了解决。我没有给我的dragtargets分配相同的bool和int变量来触发构建小部件,而是给了它们唯一的变量:
Widget build(BuildContext context) {
bool isSuccessful = false;
bool isWorking = false;
int caughtData1;
int caughtData2;
布尔值isSuccessful和整数caughtData1分配给第一个dragtarget,isWorking和caughtData2分配给第二个dragtarget。我还删除了onWillAccept代码-但不确定这是否必要。完整代码如下:
Widget build(BuildContext context) {
bool isSuccessful = false;
bool isWorking = false;
int caughtData1;
int caughtData2;
return SafeArea(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
height: 70,
color: Colors.grey.shade600,
child: DragTarget<int>(
builder: (context, List<int> candidateData, rejectedData) {
return isWorking
? FlatButton(
color:
chordBrain.chordBank[caughtData1].buttoncolor,
child:
Text(chordBrain.chordBank[caughtData1].chord),
onPressed: () {
playSound(
noteBrain.noteBank[caughtData1].note1);
playSound(
noteBrain.noteBank[caughtData1].note2);
playSound(
noteBrain.noteBank[caughtData1].note3);
playSound(
noteBrain.noteBank[caughtData1].note4);
},
)
: Container();
},
onAccept: (int data) {
print('$data');
caughtData1 = data;
isWorking = true;
},
),
),
),
SizedBox(
width: 8,
),
Expanded(
child: Container(
height: 70,
color: Colors.grey.shade600,
child: DragTarget<int>(
builder: (context, List<int> candidateData, rejectedData) {
return isSuccessful
? FlatButton(
color:
chordBrain.chordBank[caughtData2].buttoncolor,
child:
Text(chordBrain.chordBank[caughtData2].chord),
onPressed: () {
playSound(
noteBrain.noteBank[caughtData2].note1);
playSound(
noteBrain.noteBank[caughtData2].note2);
playSound(
noteBrain.noteBank[caughtData2].note3);
playSound(
noteBrain.noteBank[caughtData2].note4);
},
)
: Container();
},
onAccept: (int data) {
print('$data');
caughtData2 = data;
isSuccessful = true;
},
),
),
),
],
),
小部件构建(构建上下文){
bool ISSUCCESS=错误;
bool isWorking=false;
int caughtData1;
int caughtData2;
返回安全区(
子:列(
儿童:[
划船(
儿童:[
扩大(
子:容器(
身高:70,
颜色:Colors.grey.shade600,
儿童:DragTarget(
生成器:(上下文、列表候选数据、拒绝数据){
回程有效
?扁平按钮(
颜色:
chordBrain.chordBank[caughtData1]。按钮颜色,
儿童:
文本(chordBrain.chordBank[caughtData1].chord),
已按下:(){
播放声音(
noteBrain.noteBank[caughtData1].note1);
播放声音(
noteBrain.noteBank[caughtData1].note2);
播放声音(
noteBrain.noteBank[caughtData1].note3);
播放声音(
noteBrain.noteBank[caughtData1].note4);
},
)
:容器();
},
onAccept:(int数据){
打印(“$data”);
caughtData1=数据;
isWorking=true;
},
),
),
),
大小盒子(
宽度:8,
),
扩大(
子:容器(
身高:70,
颜色:Colors.grey.shade600,