gpt4 book ai didi

dart - DragTarget onWillAccept 和 onAccept 不触发

转载 作者:IT老高 更新时间:2023-10-28 12:42:37 25 4
gpt4 key购买 nike

我从 Flutter 开始,我无法使拖放功能正常工作。我遵循了文档,但不知道我做错了什么。此示例应用程序显示三个正方形,蓝色是可拖动的。其他的设置了 DragTarget,一个在正方形内,一个在正方形外。当我拖动蓝色方 block 时,它会打印出拖动开始的信息,但在拖放到 DragTargets 时没有打印信息。代码如下:

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.red,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
constraints: BoxConstraints.expand(),
color: Colors.grey[900],
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
child: DragTarget(
onWillAccept: (d) => true,
onAccept: (d) => print("ACCEPT 1!"),
onLeave: (d) => print("LEAVE 1!"),
builder: (a,data,c) {
print(data);
return Center();
},
),
),
DragTarget(
onWillAccept: (d){return true;},
onAccept:(d) => print("ACCEPT 2!"),
onLeave: (d) => print("LEAVE 2!"),
builder: (context, candidateData, rejectedData){
return Container(
width: 150,
height: 150,
color: Colors.purple
);
}
),
Draggable(
data: ["SOME DATA"],
onDragStarted: () => print("DRAG START!"),
onDragCompleted: () => print("DRAG COMPLETED!"),
onDragEnd: (details) => print("DRAG ENDED!"),
onDraggableCanceled: (data, data2) => print("DRAG CANCELLED!"),
feedback: SizedBox(
width: 100,
height: 100,
child: Container(
margin: EdgeInsets.all(10),
color: Colors.green[800],
),
),
child: SizedBox(
width: 100,
height: 100,
child: Container(
margin: EdgeInsets.all(10),
color: Colors.blue[800],
),
),
),
],
)
),
)
);
}
}

最佳答案

如果要传递数据,显然 Draggable 和 DragTarget 需要指定泛型,否则不会触发 onAccept 和 onWillAccept。

例如,如果您想以 int 形式传递数据,则使用 Draggable<int>DragTarget<int> — 这同样适用于onAccept 和onWillAccept,它们需要接受int 作为参数。

关于dart - DragTarget onWillAccept 和 onAccept 不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53585711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com