在Flutter中将鼠标悬停在Dragtarget上时,可拖动触发错误信息



我正在Flutter中处理draggable和draggargets。我目前在我的项目中有两个拖动目标,一旦我在它们上面放下一个拖动对象,它们都会正确响应。然而,一旦从draggable1到draggtarget1的第一次拖动完成,第二次拖动就会带来一个问题:当使用draggable2在draggtarget2上悬停(而不是放下(时,会触发onacept属性,并使用draggtarget1。我不明白为什么。如果有任何帮助/提示,我将不胜感激!

我制作了一个短视频,对这个问题进行了视觉呈现:https://youtu.be/IJa3oZ_7fw0

这是我的dragtarget代码:

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;
},
),
),
),
],
),

我似乎找到了一个解决方案,它不是很优雅,但它很有效。尽管我仍然不明白为什么我的可拖动对象在悬停(而不是掉落(在拖动目标上时会触发构建,但通过使拖动目标彼此更加不同,问题得到了解决。我没有给我的两个dragtargets分配相同的bool和int变量来触发构建小部件,而是给了它们唯一的变量:

Widget build(BuildContext context) {
bool isSuccessful = false;
bool isWorking = false;
int caughtData1;
int caughtData2;

布尔值isSuccessful和整数caughtData1被分配给第一个dragtarget,isWorking和caughtData2被分配给第二个dragttarget。我还删除了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;
},
),
),
),
],
),

相关内容

  • 没有找到相关文章

最新更新