我正在Flutter中处理可拖动对象和拖动目标。我目前在项目中有两个dragtarget,一旦将可拖动对象放到它们上,它们都可以正确响应。但是,一旦完成了从draggable1到dragtarget1的第一次拖动,第二个拖动就带来了一个问题:在将鼠标悬停在dragtagable2上并拖动draggable2的同时,onaccept属性被触发,并且dragtarget2使用dragtarget1的内容构建。我不明白为什么。如果有任何帮助/提示,我将不胜感激!
我制作了一段简短的视频,以直观的方式展示了该问题: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;
},
),
),
),
],
),
答案 0 :(得分:0)
似乎我找到了一个解决方案,虽然它不是很优雅,但是可以工作。尽管我仍然不明白为什么将我的可拖动对象悬停(而不是放下)在拖动目标上时会触发构建,但是通过使拖动目标彼此之间更加不同来解决了该问题。与其给我的两个拖动目标分配相同的bool和int变量来触发构建小部件,我不给它们唯一的变量:
Widget build(BuildContext context) {
bool isSuccessful = false;
bool isWorking = false;
int caughtData1;
int caughtData2;
布尔值isSuccessful,并且将整数catchData1分配给第一个拖动目标,将isWorking和catchData2分配给第二个拖动目标。我还删除了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;
},
),
),
),
],
),