touch_bubble.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import 'package:flutter/material.dart';
  2. import 'animated_touch_bubble_part.dart';
  3. class TouchBubble extends StatefulWidget {
  4. TouchBubble({
  5. required this.size,
  6. required this.onDrag,
  7. required this.onDragFinished,
  8. });
  9. final double size;
  10. final Function onDrag;
  11. final Function onDragFinished;
  12. @override
  13. _TouchBubbleState createState() => _TouchBubbleState();
  14. }
  15. class _TouchBubbleState extends State<TouchBubble> {
  16. bool dragging = false;
  17. @override
  18. Widget build(BuildContext context) {
  19. return GestureDetector(
  20. behavior: HitTestBehavior.opaque,
  21. onPanStart: _startDragging,
  22. onPanUpdate: _drag,
  23. onPanCancel: _cancelDragging,
  24. onPanEnd: (_) => _cancelDragging(),
  25. child: Container(
  26. width: widget.size,
  27. height: widget.size,
  28. decoration: BoxDecoration(
  29. color: Colors.transparent,
  30. borderRadius: BorderRadius.circular(widget.size / 2)),
  31. child: AnimatedTouchBubblePart(
  32. dragging: dragging,
  33. size: widget.size,
  34. )));
  35. }
  36. void _startDragging(DragStartDetails data) {
  37. setState(() {
  38. dragging = true;
  39. });
  40. widget
  41. .onDrag(data.localPosition - Offset(widget.size / 2, widget.size / 2));
  42. }
  43. void _cancelDragging() {
  44. setState(() {
  45. dragging = false;
  46. });
  47. widget.onDragFinished();
  48. }
  49. void _drag(DragUpdateDetails data) {
  50. if (!dragging) {
  51. return;
  52. }
  53. widget.onDrag(data.delta);
  54. }
  55. }