DragTarget onWillAccept和onAccept不触发

Krz*_*ski 5 drag-and-drop dart flutter

我从Flutter开始,无法使用拖放功能。我遵循了文档,但不知道我在做什么错。此示例应用程序显示三个正方形,蓝色可拖动。其他的则设置了DragTarget,一个在正方形内,另一个在正方形外。当我拖动蓝色方块时,它将打印开始拖动的信息,但是在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],
                  ),
                ),
              ),
            ],
          )
        ),
      )
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Krz*_*ski 13

显然,如果要传递数据,则Draggable和DragTarget需要具有指定的通用类型,否则将不会触发onAccept和onWillAccept。

例如,如果您想将数据作为int传递,则使用Draggable<int>and DragTarget<int>?—这也适用于onAccept和onWillAccept,它们需要接受int作为参数。