我正在使用 Flutter 中的可拖动对象和拖动目标。我目前在我的项目中有两个拖动目标,一旦我将一个可拖动对象放在它们上面,它们都会正确响应。但是,一旦从 draggable1 到 dragtarget1 的第一次拖动完成,第二次拖动就会出现一个问题:当使用 draggable2 将鼠标悬停(未放置)在 dragtarget2 上时,会触发 onaccept 属性并使用 dragtarget1 的内容构建 dragtarget2。我不明白为什么。我将不胜感激任何帮助/提示!
我制作了一个简短的视频,直观地展示了这个问题:https ://youtu.be/IJa3oZ_7fw0
这是我的拖动目标代码:
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;
},
),
),
),
],
),