I can't avoid the "Incorrect use of ParentDataWidget" error. The code below works - I want to be able to drop a draggable widget onto a dragTarget in the bottomSheet, but also want the widget to be draggable in the body.
The code works fine in the emulator, but when the draggable is accepted on the dragTarget, the error is flagged up in the console.
Is there something obvious that I'm doing wrong?
void main() => runApp(MyApp());
bool isAccepted = false;
DragMe niceBox = new DragMe('Hello', 50.0, 100.0, Colors.green);
DragMe nastyBox = new DragMe('Yucky', 150.0, 100.0, Colors.brown[300]);
TargetBox targetBox = new TargetBox();
MyDragTarget myDragTarget = new MyDragTarget();
const kTopSpace = 96; // Height of Appbar plus the icon bar above
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData( fontFamily: 'PressStart'),
home: MyHomeScreen(),
);
}
}
class MyHomeScreen extends StatefulWidget {
MyHomeScreen({Key key}) : super(key: key);
createState() => MyHomeScreenState();
}
class MyHomeScreenState extends State<MyHomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red[100],
appBar: AppBar(
toolbarHeight: 70.0,
title: Center(child: Text('My brain hurts')),
backgroundColor: Colors.pink,
),
body: Stack(children: [niceBox, nastyBox]),
bottomSheet: Container(
color: Colors.lightBlue,
height: 100.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [myDragTarget],
),
),
);
} // End build()
} // End class MyHomeScreenState
class DragMe extends StatefulWidget {
DragMe(this.myText, this.xPos, this.yPos, this.myColor);
final String myText;
double xPos;
double yPos;
final Color myColor;
@override
_DragMeState createState() => _DragMeState();
}
class _DragMeState extends State<DragMe> {
@override
Widget build(BuildContext context) {
return Positioned(
left: widget.xPos,
top: widget.yPos,
// left: widget.xPos,
// top: widget.yPos,
child: Draggable(
data: widget.myText,
feedback: Container(height: 50, width: 70, color: Colors.grey),
childWhenDragging: Container(),
onDragStarted: () {},
onDragEnd: (details) {
setState(() {
widget.xPos = details.offset.dx;
widget.yPos = details.offset.dy - kTopSpace;
});
},
onDragCompleted: () {},
onDraggableCanceled: (velocity, offset) {},
child: Container(
child: Center(child: Text(widget.myText)),
color: widget.myColor,
height: 50,
width: 70,
),
),
);
}
}
class MyDragTarget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DragTarget<String>(
builder: (context, List<String> acceptedData, rejectedData) {
if (isAccepted) {
return niceBox;
} else {
return targetBox;
}
},
onWillAccept: (data) {
if (data == 'Hello') {
isAccepted = true;
return true;
} else {
isAccepted = false;
return false;
}
},
onAccept: (strData) {},
onLeave: (strData) {
return true;
},
onMove: (moveData) {
return true;
},
onAcceptWithDetails: (data) {},
);
}
}
class TargetBox extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(child: Text('Drop Here')),
color: Colors.yellow,
height: 50.0,
width: 100.0);
}
}
question from:
https://stackoverflow.com/questions/65886838/how-do-i-get-rid-of-incorrect-use-of-parentdatawidget-error