ANTIPALSU Label template editor using flutter

resizeable_element.dart 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:defer_pointer/defer_pointer.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:provider/provider.dart';
  4. class EditorTest with ChangeNotifier {
  5. double _selectedWidth = 150.0;
  6. double get selectedWidth => _selectedWidth;
  7. void updateElmWidth(double newWidth) {
  8. _selectedWidth = newWidth;
  9. notifyListeners();
  10. }
  11. }
  12. class ResizableElement extends StatefulWidget {
  13. @override
  14. _ResizableElementState createState() => _ResizableElementState();
  15. }
  16. class _ResizableElementState extends State<ResizableElement> {
  17. double _initialWidth = 150.0; // Initial width of the element
  18. Offset? _dragStartPosition; // Starting position of the drag
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. appBar: AppBar(title: Text("Resizable Element ")),
  23. body: DeferredPointerHandler(
  24. child: Stack(
  25. clipBehavior: Clip.none,
  26. children: [
  27. // Resizable container
  28. Container(
  29. width: _initialWidth,
  30. // height: 100,
  31. color: Colors.blue[100],
  32. // alignment: Alignment.center,
  33. child: Text(
  34. "Resizable Element $_initialWidth",
  35. textAlign: TextAlign.center,
  36. ),
  37. ),
  38. // Drag handle
  39. Positioned(
  40. right: -30,
  41. top: 0,
  42. bottom: 0,
  43. child: DeferPointer(
  44. child: Listener(
  45. onPointerDown: (event) {
  46. _dragStartPosition = event.position;
  47. },
  48. onPointerMove: (event) {
  49. setState(() {
  50. if (_dragStartPosition != null) {
  51. // Calculate drag delta
  52. final delta = event.position.dx - _dragStartPosition!.dx;
  53. _initialWidth = (_initialWidth + delta).clamp(75.0, MediaQuery.of(context).size.width);
  54. _dragStartPosition = event.position; // Update drag position
  55. }
  56. });
  57. },
  58. onPointerUp: (event) {
  59. _dragStartPosition = null; // Reset drag start position
  60. },
  61. child: Container(
  62. width: 30,
  63. height: 100,
  64. color: Colors.blue,
  65. child: Icon(
  66. Icons.drag_handle,
  67. size: 16,
  68. color: Colors.white,
  69. ),
  70. ),
  71. ),
  72. ),
  73. ),
  74. ],
  75. ),
  76. ),
  77. );
  78. }
  79. }