我有这样的Draggable。大多数教程视频都是在全屏模式下使用Draggable,但我只想在屏幕的一小部分使用它。
class DragBox extends StatefulWidget {
final Offset initPos;
final AssetImage image;
DragBox(this.initPos, this.image);
@override
_DragBoxState createState() => _DragBoxState();
}
class _DragBoxState extends State<DragBox> {
Offset position = Offset.zero;
double top = 0;
double left = 0;
@override
void initState() {
super.initState();
position = widget.initPos;
}
Widget _dragChild() {
return CircleAvatar(
backgroundImage: widget.image,
backgroundColor: colorPlayerBackground,
);
}
@override
Widget build(BuildContext context) {
return Positioned(
left: position.dx,
top: position.dy,
child: Draggable(
data: [widget.image],
child: _dragChild(),
onDraggableCanceled: (velocity, offset) {
setState(() {
position = offset;
});
},
childWhenDragging: Opacity(
opacity: 0.5,
child: _dragChild(),
),
feedback: _dragChild(),
),
);
}
}
在具有DragTarget的Card容器内部。
Container(
width: double.infinity,
child: Card(
child: Stack(
children: <Widget>[
DragBox(Offset(0.0, 0.0), AssetImage(playerAnthonyMartial)),
DragBox(Offset(150.0, 0.0), AssetImage(playerBrunoFernandes)),
Positioned(
left: 75.0,
bottom: 0.0,
child: Container(
width: 120,
height: 120,
color: Colors.black,
child: DragTarget(
onWillAccept: (data) {
return true;
},
onAccept: (data) {
accept = true;
},
builder: (
BuildContext context,
List<dynamic> accepted,
rejected,
) {
return accept
? Image.asset('assets/images/AnthonyMartial.png')
: Container();
},
),
),
)
],
),
),
),
但是发生这种情况when i drop it这是怎么发生的,如何解决?
我遵循youtube上的指南,但是当我将Draggable拖到目标上时,似乎什么都没有发生,数据如何传递。我不太明白它是如何工作的。请帮忙!!!
如果不使用,则需要添加小偏移校正并取出卡。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}
const playerAnthonyMartial = 'assets/AnthonyMartial.png';
const playerBrunoFernandes = 'assets/BrunoFernandes.png';
const colorPlayerBackground = Colors.white;
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
bool accept;
return Stack(
children: <Widget>[
DragBox(Offset(0.0, 0.0), AssetImage(playerAnthonyMartial)),
DragBox(Offset(150.0, 0.0), AssetImage(playerBrunoFernandes)),
Positioned(
left: 75.0,
bottom: 0.0,
child: Container(
width: 120,
height: 120,
color: Colors.black,
child: DragTarget(
onWillAccept: (data) {
return true;
},
onAccept: (data) {
accept = true;
},
builder: (
BuildContext context,
List<dynamic> accepted,
rejected,
) {
return Container(child: Text('hey'));
},
),
),
)
],
);
}
}
class DragBox extends StatefulWidget {
final Offset initPos;
final AssetImage image;
DragBox(this.initPos, this.image);
@override
_DragBoxState createState() => _DragBoxState();
}
class _DragBoxState extends State<DragBox> {
Offset position = Offset.zero;
double top = 0;
double left = 0;
@override
void initState() {
super.initState();
position = widget.initPos;
}
Widget _dragChild() {
return CircleAvatar(
radius: 20,
backgroundImage: widget.image,
backgroundColor: colorPlayerBackground,
);
}
@override
Widget build(BuildContext context) {
print(position);
return Positioned(
left: position.dx,
top: position.dy,
child: Draggable(
data: [widget.image],
child: _dragChild(),
onDraggableCanceled: (velocity, offset) {
setState(() {
position = offset - Offset(0, 20);
});
},
childWhenDragging: Opacity(
opacity: 0.5,
child: _dragChild(),
),
feedback: _dragChild(),
),
);
}
}