简体   繁体   English

悬停时颤振 DragTarget

[英]Flutter DragTarget on hover

When exploring the Flutter's DragTarget class I could not find any mechanism to hook to the 'on hover event' if you will.在探索 Flutter 的 DragTarget 类时,如果你愿意的话,我找不到任何机制来挂钩“悬停事件”。 Here is an example:这是一个例子:

 class DropZone extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 40,
      color: Colors.green,
      child: DragTarget(
          builder: (context, List<int> candidateData, rejectedData) {
            print("dragged");
            return new Container(color: Colors.red,);
          },
      onWillAccept: (data) {
            return true;
      },),
    );
  }
}

I would like to change the color of the container when a Draggable object is hovered but not yet dropped.当 Draggable 对象悬停但尚未放下时,我想更改容器的颜色。 The builder method, in this case, only executes during the initial rendering and when the Draggable leaves the target.在这种情况下,builder 方法仅在初始渲染期间和 Draggable 离开目标时执行。 I suspect I have to do something in the onWillAccept method but not sure what.我怀疑我必须在 onWillAccept 方法中做一些事情,但不确定是什么。 Does anybody have a solution?有人有解决方案吗?

here is the code for what I think you're looking for这是我认为您正在寻找的代码

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: App(),
      ),
    );
  }
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  Color caughtColor = Colors.grey;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        DragBox(Offset(0.0, 0.0), Colors.green),
        Positioned(
            left: 100,
            bottom: 0.0,
            child: DragTarget(onAccept: (Color color) {
              caughtColor = color;
            }, builder: (
              BuildContext context,
              List<dynamic> accepted,
              List<dynamic> rejected,
            ) {
              return Container(
                width: 200,
                height: 200,
                color: accepted.isEmpty ? caughtColor : Colors.grey.shade200,
              );
            }))
      ],
    );
  }
}

class DragBox extends StatefulWidget {
  final Offset initPos;
  final Color itemColor;

  DragBox(this.initPos, this.itemColor);
  @override
  _DragBoxState createState() => _DragBoxState();
}

class _DragBoxState extends State<DragBox> {
  Offset position = Offset(0.0, 0.0);

  @override
  void initState() {
    super.initState();
    position = widget.initPos;
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: position.dx,
      top: position.dy,
      child: Draggable(
        data: widget.itemColor,
        child: Container(
          width: 100,
          height: 100,
          color: widget.itemColor,
        ),
        onDraggableCanceled: (velocity, offset) {
          setState(() {
            position = offset;
          });
        },
        feedback: Container(
          width: 120,
          height: 120,
          color: widget.itemColor.withOpacity(0.5),
        ),
      ),
    );
  }
}

You can make combination of onAccept, onMove and onLeave functions in DragTarget to achieve the result.您可以在 DragTarget 中组合 onAccept、onMove 和 onLeave 函数来实现结果。

Here is how you do it:这是您的操作方法:

// ...
bool isTargetGettingDroppedForDelete = false;
//...

DragTarget<String>(
        builder: (
          BuildContext context,
          List<dynamic> accepted,
          List<dynamic> rejected,
        ) {
          return Container(
            height: 100,
            width: 100,
            color: isTargetGettingDroppedForDelete ? Colors.red : Colors.green,
          );
        },
        onWillAccept: (data) {
          return data == Constants.draggable;
        },
        onAccept: (data) {
          setState(() {
            isTargetGettingDroppedForDelete = false;
          });
        },
        onMove: (details) {
          setState(() {
            isTargetGettingDroppedForDelete = true;
          });
        },
        onLeave: (data) {
          setState(() {
            isTargetGettingDroppedForDelete = false;
          });
        },
      ),

You can use the candidateData parameter in the DragTarget.builder :您可以在candidateData中使用DragTarget.builder参数:

...
border: Border.all(
           color: candidateData.isNotEmpty
            ? Colors.red
            : Colors.black)),

The code above changes the border color to red upon hover.上面的代码在悬停时将边框颜色更改为red

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 未调用 DragTarget onAccept - Flutter DragTarget onAccept is not being called DragTarget 不使用自定义 Draggable (Flutter) 调用 onWillAccept - DragTarget doesnt call onWillAccept with custom Draggable (Flutter) Flutter 有多个独立的 DragTarget 小部件 - Flutter have multiple DragTarget widgets which are independent 在 Flutter 中为用户自动将可拖动对象移动到拖动目标 - Move a draggable to a dragtarget automatically for the user in Flutter Draggable 在 Flutter 中悬停在 Dragtarget 上时触发错误信息 - Draggable triggering wrong info while hovering over Dragtarget in Flutter Flutter Draggable 小部件,可以单击或拖动以移动到 DragTarget - Flutter Draggable widget that can be clicked or dragged to move to DragTarget Flutter:使用新的 Draggable 将鼠标悬停在 DragTarget 上时显示错误数据 - Flutter: DragTarget shows wrong data when hovering over it with a new Draggable Flutter:等待 DragTarget 的 onWillAccept function 内的警报对话框的响应 - Flutter : Waiting for response of an alert dialog inside onWillAccept function of DragTarget 在Flutter中输入DragTarget时如何更新Draggable子级? - How to update Draggable child when entering DragTarget in Flutter? 可拖动数据类型与 Flutter 中的 DragTarget 动态候选数据不匹配 - Draggable data type does not match DragTarget dynamic candidateData in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM