I have recently started learning Flutter and I am stuck at a point while dealing with Drag and Drop using Droppable and DragTarget. When I Drag my draggable element over DropTarget element, I am doing few validations in onWillAccept method. One of the conditions here requires me to confirm with user if they are willing to continue with their action before returning true and heading to onAccept method. For some reason, code execution does not wait for user's action to return.
This is how my DragTarget looks
DragTarget<Map>(
builder: (context, listOne, listTwo) {
return Container();
},
onWillAccept: (value) {
if(condition1) {
return true;
} else if(condition2) {
return true;
} else {
if(!condition3) {
return true;
} else {
await _showConfirmation();
return false;
}
}
},
onAccept: (value) {
print(value);
},
)
and _showConfirmation method looks something like this
Future<void> _showConfirmation() async {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Attention'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Some message")
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Accept'),
onPressed: () {
Navigator.of(context).pop();
return true;
},
),
FlatButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
return false;
},
)
],
);
},
);
}
Adding await does not help as onWillAccept is not async. Making it async did not help either.
_showConfirmation().then((result) {
return result
})
Above code did not help either. In many cases, the dragged item is left hanging over DragTarget box.
Any help regarding this will appreciated, thank you.