当我开始在 Flutter 中拖动 Widget 时如何显示 Icon

发布于 2025-01-10 10:53:25 字数 1387 浏览 2 评论 0原文

我想知道当我开始在 LongPressDraggable 小部件中拖动容器时,如何在屏幕底部显示删除图标

Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () => _onTap(context),
        child: LongPressDraggable(
          data: index,
          maxSimultaneousDrags: 1,
          onDragUpdate: (details) => print('update'),
          onDragStarted: () => _buildDragTarget(),
          onDragEnd: (_) => print('end'),
          feedback: Material(
            child: Container(
              height: Sizes.height / 4.5,
              width: Sizes.height / 4.5,
              child: _DraggableContent(
                index: index,
                place: place,
              ),
            ),
          ),
          childWhenDragging: Container(color: Colors.transparent),
          child: _DraggableContent(
            index: index,
            place: place,
          ),
        ));
  }

 Widget _buildDragTarget() {
    return DragTarget<int>(
      builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
        return Icon(Icons.delete);
      },
      onAcceptWithDetails: (DragTargetDetails<int> dragTargetDetails) {
        print('onAcceptWithDetails');
        print('Data: ${dragTargetDetails.data}');
        print('Offset: ${dragTargetDetails.offset}');
      },
    );
  }

目前,当我开始拖动项目时,会发生任何事情,我不知道如何继续

I want to know how can I show at the bottom of the screen a delete icon when I start dragging a Container in LongPressDraggable widget

Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () => _onTap(context),
        child: LongPressDraggable(
          data: index,
          maxSimultaneousDrags: 1,
          onDragUpdate: (details) => print('update'),
          onDragStarted: () => _buildDragTarget(),
          onDragEnd: (_) => print('end'),
          feedback: Material(
            child: Container(
              height: Sizes.height / 4.5,
              width: Sizes.height / 4.5,
              child: _DraggableContent(
                index: index,
                place: place,
              ),
            ),
          ),
          childWhenDragging: Container(color: Colors.transparent),
          child: _DraggableContent(
            index: index,
            place: place,
          ),
        ));
  }

 Widget _buildDragTarget() {
    return DragTarget<int>(
      builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
        return Icon(Icons.delete);
      },
      onAcceptWithDetails: (DragTargetDetails<int> dragTargetDetails) {
        print('onAcceptWithDetails');
        print('Data: ${dragTargetDetails.data}');
        print('Offset: ${dragTargetDetails.offset}');
      },
    );
  }

At the moment, when I start dragging the item, anything happens and I don't know how to continue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢欲 2025-01-17 10:53:26

您在这里有一个可拖动小部件的示例: https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/

删除项目部分https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=the%20tomato%20image.-,删除%20an%20item,-At%20this%20point

您需要在 DragTarget 小部件上使用 onAccept 事件:

onAccept: (data) {
setState(() {
  showSnackBarGlobal(context, 'Dropped successfully!');
  _isDropped = true;
});
},

在 Drag 开始时,您可以显示您的使用此事件删除图标 (https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=Listening%20to%20drag%20events) :

onDragStarted: () {
showSnackBarGlobal(context, 'Drag started');
},

希望对你有帮助

You have an example with Dragable widget here : https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/

On the DROPPING AN ITEM part https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=the%20tomato%20image.-,Dropping%20an%20item,-At%20this%20point

You need to use the onAccept event on your DragTarget widget :

onAccept: (data) {
setState(() {
  showSnackBarGlobal(context, 'Dropped successfully!');
  _isDropped = true;
});
},

And on Drag starting, you can show up your delete icon by using this event (https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=Listening%20to%20drag%20events) :

onDragStarted: () {
showSnackBarGlobal(context, 'Drag started');
},

I hope it will help you

能否归途做我良人 2025-01-17 10:53:26

据我了解,当您拖动对象时,您需要在底部显示删除图标。
您可以使用 Visibility 小部件包裹删除图标,并根据拖动活动设置可见参数 truefalse

它会是这样的:

Widget _buildDragTarget() {
  return DragTarget<int>(
    builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
      return Visibility(
          visible: isDragEnable,
          child: Icon(Icons.delete));
    },
    onAcceptWithDetails: (DragTargetDetails<int> dragTargetDetails) {
      print('onAcceptWithDetails');
      print('Data: ${dragTargetDetails.data}');
      print('Offset: ${dragTargetDetails.offset}');
    },
  );
}

As far as I understood you need to show the delete icon on bottom when you are dragging an object.
You can wrap the delete icon with Visibility widget and set the visible parameter true or false based on the drag activity.

It will go something like this:

Widget _buildDragTarget() {
  return DragTarget<int>(
    builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
      return Visibility(
          visible: isDragEnable,
          child: Icon(Icons.delete));
    },
    onAcceptWithDetails: (DragTargetDetails<int> dragTargetDetails) {
      print('onAcceptWithDetails');
      print('Data: ${dragTargetDetails.data}');
      print('Offset: ${dragTargetDetails.offset}');
    },
  );
}
三岁铭 2025-01-17 10:53:26

我找到了解决方案。它可以创建一个 BlocProvider 并与状态一起工作。

class _Trash extends StatefulWidget {
  const _Trash({
    Key key,
  }) : super(key: key);

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

class __TrashState extends State<_Trash> with SingleTickerProviderStateMixin {
  Animation animation;
  AnimationController controller;

  _listenerManager(bool isDragging) {
    if (isDragging) {
      setState(() => animation =
          CurvedAnimation(parent: controller, curve: Curves.elasticOut));
      controller.forward();
    } else {
      setState(() => animation = CurvedAnimation(
          parent:
              CurvedAnimation(parent: controller, curve: Interval(0.75, 1.0)),
          curve: Curves.ease));
      controller.reverse();
    }
  }

  _deleteWidget(data) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.delete(data));
  }

  @override
  void initState() {
    controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    animation = CurvedAnimation(parent: controller, curve: Curves.elasticOut);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        bottom: 0,
        left: Sizes.width / 2 - Sizes.height / 15 / 2,
        child: ScaleTransition(
            scale: animation,
            child: BlocListener<BoardBloc, BoardState>(
              listenWhen: (previous, current) =>
                  previous.isDragging != current.isDragging,
              listener: (context, state) => _listenerManager(state.isDragging),
              child: Container(
                height: Sizes.height / 15,
                width: Sizes.height / 15,
                color: Colors.amber,
                child: DragTarget<int>(
                  builder: (BuildContext context, List<int> data,
                      List<dynamic> rejects) {
                    return Icon(Icons.delete);
                  },
                  onAcceptWithDetails:
                      (DragTargetDetails<int> dragTargetDetails) {
                    _deleteWidget(dragTargetDetails.data);
                    print('onAcceptWithDetails');
                    print('Data: ${dragTargetDetails.data}');
                    print('Offset: ${dragTargetDetails.offset}');
                  },
                ),
              ),
            )));
  }
}

class _PlaceTile extends StatelessWidget {
  final int index;
  final Place place;
  const _PlaceTile({@required this.place, @required this.index});

  _onTap(BuildContext context) => Navigator.of(context).push(PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) =>
          PlaceViewer(index: index, animation: animation, place: place)));

  _startDragging(context) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.startedDragging());
  }

  _stopDragging(context) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.stopDragging());
  }

  _dragging(context) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.dragging());
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () => _onTap(context),
        child: LongPressDraggable(
          data: index,
          maxSimultaneousDrags: 1,
          onDragUpdate: (details) => _dragging(context),
          onDragStarted: () => _startDragging(context),
          onDragEnd: (_) => _stopDragging(context),
          feedback: Material(
            child: Container(
              height: Sizes.height / 4.5,
              width: Sizes.height / 4.5,
              child: _DraggableContent(
                index: index,
                place: place,
              ),
            ),
          ),
          childWhenDragging: Container(color: Colors.transparent),
          child: _DraggableContent(
            index: index,
            place: place,
          ),
        ));
  }
}

I found the solution. It works creating a BlocProvider and working with the state.

class _Trash extends StatefulWidget {
  const _Trash({
    Key key,
  }) : super(key: key);

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

class __TrashState extends State<_Trash> with SingleTickerProviderStateMixin {
  Animation animation;
  AnimationController controller;

  _listenerManager(bool isDragging) {
    if (isDragging) {
      setState(() => animation =
          CurvedAnimation(parent: controller, curve: Curves.elasticOut));
      controller.forward();
    } else {
      setState(() => animation = CurvedAnimation(
          parent:
              CurvedAnimation(parent: controller, curve: Interval(0.75, 1.0)),
          curve: Curves.ease));
      controller.reverse();
    }
  }

  _deleteWidget(data) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.delete(data));
  }

  @override
  void initState() {
    controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    animation = CurvedAnimation(parent: controller, curve: Curves.elasticOut);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        bottom: 0,
        left: Sizes.width / 2 - Sizes.height / 15 / 2,
        child: ScaleTransition(
            scale: animation,
            child: BlocListener<BoardBloc, BoardState>(
              listenWhen: (previous, current) =>
                  previous.isDragging != current.isDragging,
              listener: (context, state) => _listenerManager(state.isDragging),
              child: Container(
                height: Sizes.height / 15,
                width: Sizes.height / 15,
                color: Colors.amber,
                child: DragTarget<int>(
                  builder: (BuildContext context, List<int> data,
                      List<dynamic> rejects) {
                    return Icon(Icons.delete);
                  },
                  onAcceptWithDetails:
                      (DragTargetDetails<int> dragTargetDetails) {
                    _deleteWidget(dragTargetDetails.data);
                    print('onAcceptWithDetails');
                    print('Data: ${dragTargetDetails.data}');
                    print('Offset: ${dragTargetDetails.offset}');
                  },
                ),
              ),
            )));
  }
}

class _PlaceTile extends StatelessWidget {
  final int index;
  final Place place;
  const _PlaceTile({@required this.place, @required this.index});

  _onTap(BuildContext context) => Navigator.of(context).push(PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) =>
          PlaceViewer(index: index, animation: animation, place: place)));

  _startDragging(context) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.startedDragging());
  }

  _stopDragging(context) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.stopDragging());
  }

  _dragging(context) {
    BlocProvider.of<BoardBloc>(context).add(BoardEvent.dragging());
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () => _onTap(context),
        child: LongPressDraggable(
          data: index,
          maxSimultaneousDrags: 1,
          onDragUpdate: (details) => _dragging(context),
          onDragStarted: () => _startDragging(context),
          onDragEnd: (_) => _stopDragging(context),
          feedback: Material(
            child: Container(
              height: Sizes.height / 4.5,
              width: Sizes.height / 4.5,
              child: _DraggableContent(
                index: index,
                place: place,
              ),
            ),
          ),
          childWhenDragging: Container(color: Colors.transparent),
          child: _DraggableContent(
            index: index,
            place: place,
          ),
        ));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文