我有一个可拖式包含数据类型int。当我将拖放到DragTarget上时,List candidateData不接受int数据。
当我更改为List candidateData时,它就被接受了。
我以为dynamic接受所有类型的数据。这是窃听器还是我误解了什么。
class _DragTargetDataState extends State<DragTargetData> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: Stack(
children: <Widget>[
Positioned(
top: 100,
left: 50,
child: Draggable(
feedback: buildBox('Draggable', Colors.cyanAccent),
child: buildBox('Draggable', Colors.cyanAccent),
data: 1, // -> data Draggable bring along
onDragEnd: (details) {
print('wasAccepted: ${details.wasAccepted.toString()}');
},
),
),
Positioned(
top: 300,
left: 120,
child: DragTarget(
builder: (BuildContext context,
List<dynamic> candidateData, // List dynamic type of candidate data
List<dynamic> rejectedData) {
return buildBox('Drag Target', Colors.deepOrange);
},
onWillAccept: (data) {
print('onWillAccept: $data');
return true;
},
),
),
],
),
),
),
);
}
Container buildBox(String title, Color color) {
return Container(
width: 100,
height: 100,
color: color,
child: Center(
child: Text(
title,
style: TextStyle(
fontSize: 15,
color: Colors.black,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
decoration: TextDecoration.none),
),
),
);
}
}发布于 2020-06-04 06:04:59
您可以复制粘贴,运行下面的完整代码
要与dynamic一起使用,可以转换数据类型data: 1 as dynamic
转换后,系统知道T是动态的。
typedef DragTargetBuilder<T> = Widget Function(BuildContext context, List<T> candidateData, List<dynamic> rejectedData);代码段
Draggable(
feedback: buildBox('Draggable', Colors.cyanAccent),
child: buildBox('Draggable', Colors.cyanAccent),
data: 1 as dynamic,输出
I/flutter (14993): onWillAccept: 1
I/flutter (14993): wasAccepted: true 工作演示

全码
import 'package:flutter/material.dart';
class DragTargetData extends StatefulWidget {
@override
_DragTargetDataState createState() => _DragTargetDataState();
}
class _DragTargetDataState extends State<DragTargetData> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: Stack(
children: <Widget>[
Positioned(
top: 100,
left: 50,
child: Draggable(
feedback: buildBox('Draggable', Colors.cyanAccent),
child: buildBox('Draggable', Colors.cyanAccent),
data: 1 as dynamic, // -> data Draggable bring along
onDragEnd: (details) {
print('wasAccepted: ${details.wasAccepted.toString()}');
},
),
),
Positioned(
top: 300,
left: 120,
child: DragTarget<dynamic>(
builder: (BuildContext context,
List<dynamic>
candidateData, // List dynamic type of candidate data
List<dynamic> rejectedData) {
return buildBox('Drag Target', Colors.deepOrange);
},
onWillAccept: (data) {
print('onWillAccept: $data');
return true;
},
),
),
],
),
),
),
);
}
Container buildBox(String title, Color color) {
return Container(
width: 100,
height: 100,
color: color,
child: Center(
child: Text(
title,
style: TextStyle(
fontSize: 15,
color: Colors.black,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
decoration: TextDecoration.none),
),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: DragTargetData(),
);
}
}https://stackoverflow.com/questions/62186462
复制相似问题