flet-movable 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from .movable import Movable
2
+
3
+ __all__ = ["Movable"]
@@ -0,0 +1,45 @@
1
+ import math
2
+ from dataclasses import field
3
+ from typing import Optional
4
+
5
+ import flet as ft
6
+ from flet.controls.control_event import ControlEventHandler
7
+
8
+
9
+ @ft.control("movable")
10
+ class Movable(ft.LayoutControl):
11
+ """Bounded native dragging; x/y synchronize on release."""
12
+
13
+ content: Optional[ft.Control] = None
14
+ x: float = 16
15
+ y: float = 16
16
+ item_width: float = 232
17
+ item_height: float = 72
18
+ edge_padding: float = 8
19
+ haptic: bool = True
20
+ long_press_duration: float = 0.5
21
+ drag_border: Optional[ft.Border] = field(
22
+ default_factory=lambda: ft.Border.all(2, "#65D9C2")
23
+ )
24
+ drag_border_radius: ft.BorderRadiusValue = 22
25
+ on_move_start: Optional[ControlEventHandler["Movable"]] = None
26
+ on_move_cancel: Optional[ControlEventHandler["Movable"]] = None
27
+ on_move_end: Optional[ControlEventHandler["Movable"]] = None
28
+
29
+ def before_update(self):
30
+ super().before_update()
31
+ for name in (
32
+ "x",
33
+ "y",
34
+ "item_width",
35
+ "item_height",
36
+ "edge_padding",
37
+ "long_press_duration",
38
+ ):
39
+ value = getattr(self, name)
40
+ if not math.isfinite(value):
41
+ raise ValueError(f"{name} must be finite")
42
+ if self.long_press_duration < 0:
43
+ raise ValueError("long_press_duration must be nonnegative seconds")
44
+ if self.item_width <= 0 or self.item_height <= 0 or self.edge_padding < 0:
45
+ raise ValueError("Dimensions must be positive; padding nonnegative")
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.5
2
+ Name: flet-movable
3
+ Version: 0.1.0
4
+ Summary: Native bounded dragging for Flet controls
5
+ Project-URL: Homepage, https://github.com/alex-stoica/flet-movable
6
+ Project-URL: Repository, https://github.com/alex-stoica/flet-movable
7
+ Project-URL: Issues, https://github.com/alex-stoica/flet-movable/issues
8
+ Author-email: Alex Stoica <alexstoica@protonmail.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Programming Language :: Python :: 3
13
+ Requires-Python: >=3.11
14
+ Requires-Dist: flet<1.2,>=1.1.0
15
+ Description-Content-Type: text/plain
16
+
17
+ Native bounded dragging for Flet. See the repository for usage and compatibility.
@@ -0,0 +1,10 @@
1
+ flet_movable/__init__.py,sha256=bhcrVb7f9LDUTBsqJbKOpWo5-w43CXWioJxBGkcTcSo,55
2
+ flet_movable/movable.py,sha256=_6jT8xWjLxSQpUua00wZ0mBKUCwK1AC5fqd98IXows0,1570
3
+ flutter/flet_movable/pubspec.yaml,sha256=REvwS9BlHkt0rfxGQ22A3WRpHZTdZXJP9Zjd7VbujkU,329
4
+ flutter/flet_movable/lib/flet_movable.dart,sha256=nMaccZVPVg7iTUyAyxa6opcoEiHkX2wqWG6fKnmhPbA,45
5
+ flutter/flet_movable/lib/src/extension.dart,sha256=RimESBQgX604t93oqXTVSB-W5SUvt07uM_jD8eP3LXU,2155
6
+ flutter/flet_movable/lib/src/movable_surface.dart,sha256=UXcFWNOFqQvbpyWfopagBj9Yy73hgquV8WcCh61rRjs,7146
7
+ flet_movable-0.1.0.dist-info/METADATA,sha256=d-9Yi3OMIz5ciQGy_i3RfnK_0PJ2C8-dqaGBEvDdhYs,682
8
+ flet_movable-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
9
+ flet_movable-0.1.0.dist-info/licenses/LICENSE,sha256=b_gVOpdtm6yCJ1KyuVVdj2_aD1KvJEhAteGomJ73mlQ,1068
10
+ flet_movable-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alex Stoica
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ export 'src/extension.dart' show Extension;
@@ -0,0 +1,62 @@
1
+ import 'dart:convert';
2
+ import 'package:flet/flet.dart';
3
+ import 'package:material_ui/material_ui.dart';
4
+ import 'movable_surface.dart';
5
+
6
+ class Extension extends FletExtension {
7
+ @override
8
+ Widget? createWidget(Key? key, Control control) {
9
+ if (control.type != 'movable') return null;
10
+ return LayoutControl(
11
+ key: key,
12
+ control: control,
13
+ child: Builder(
14
+ builder: (context) => MovableSurface(
15
+ position: Offset(
16
+ control.getDouble('x', 16)!,
17
+ control.getDouble('y', 16)!,
18
+ ),
19
+ itemSize: Size(
20
+ control.getDouble('item_width', 232)!,
21
+ control.getDouble('item_height', 72)!,
22
+ ),
23
+ padding: control.getDouble('edge_padding', 8)!,
24
+ haptic: control.getBool('haptic', true)!,
25
+ enabled: !control.disabled,
26
+ longPressDuration: Duration(
27
+ microseconds:
28
+ (control.getDouble('long_press_duration', 0.5)! * 1000000)
29
+ .round(),
30
+ ),
31
+ dragBorder: control.getBorder('drag_border', Theme.of(context)),
32
+ dragBorderRadius:
33
+ control.getBorderRadius('drag_border_radius') ??
34
+ BorderRadius.zero,
35
+ onStart: (position) => control.triggerEvent(
36
+ 'move_start',
37
+ jsonEncode({'x': position.dx, 'y': position.dy}),
38
+ ),
39
+ onCancel: (position, reason) {
40
+ control.updateProperties({'x': position.dx, 'y': position.dy});
41
+ control.triggerEvent(
42
+ 'move_cancel',
43
+ jsonEncode({
44
+ 'x': position.dx,
45
+ 'y': position.dy,
46
+ 'reason': reason,
47
+ }),
48
+ );
49
+ },
50
+ onEnd: (position) {
51
+ control.updateProperties({'x': position.dx, 'y': position.dy});
52
+ control.triggerEvent(
53
+ 'move_end',
54
+ jsonEncode({'x': position.dx, 'y': position.dy}),
55
+ );
56
+ },
57
+ child: control.buildWidget('content') ?? const SizedBox.shrink(),
58
+ ),
59
+ ),
60
+ );
61
+ }
62
+ }
@@ -0,0 +1,216 @@
1
+ import 'dart:math' as math;
2
+ import 'package:flutter/widgets.dart';
3
+ import 'package:flutter/gestures.dart';
4
+ import 'package:flutter/services.dart';
5
+
6
+ class MovableSurface extends StatefulWidget {
7
+ const MovableSurface({
8
+ super.key,
9
+ required this.child,
10
+ required this.position,
11
+ required this.itemSize,
12
+ required this.onEnd,
13
+ this.padding = 8,
14
+ this.haptic = true,
15
+ this.enabled = true,
16
+ this.onStart,
17
+ this.onCancel,
18
+ this.longPressDuration = const Duration(milliseconds: 500),
19
+ this.dragBorder = const Border.fromBorderSide(
20
+ BorderSide(color: Color(0xFF65D9C2), width: 2),
21
+ ),
22
+ this.dragBorderRadius = const BorderRadius.all(Radius.circular(22)),
23
+ });
24
+
25
+ final Widget child;
26
+ final Offset position;
27
+ final Size itemSize;
28
+ final double padding;
29
+ final bool haptic;
30
+ final bool enabled;
31
+ final Duration longPressDuration;
32
+ final Border? dragBorder;
33
+ final BorderRadius dragBorderRadius;
34
+ final ValueChanged<Offset> onEnd;
35
+ final ValueChanged<Offset>? onStart;
36
+ final void Function(Offset position, String reason)? onCancel;
37
+
38
+ @override
39
+ State<MovableSurface> createState() => _MovableSurfaceState();
40
+ }
41
+
42
+ class _MovableSurfaceState extends State<MovableSurface> {
43
+ late final ValueNotifier<Offset> _position = ValueNotifier(widget.position);
44
+ int? _pointerId;
45
+ Offset? _pointerOrigin;
46
+ Offset? _itemOrigin;
47
+ Size _area = Size.zero;
48
+ Size _item = Size.zero;
49
+ Size? _viewport;
50
+ bool _hasLayout = false;
51
+ int _layoutEpoch = 0;
52
+
53
+ Offset _bounded(Offset value) {
54
+ final maxX = math.max(0.0, _area.width - _item.width);
55
+ final maxY = math.max(0.0, _area.height - _item.height);
56
+ final padX = math.min(widget.padding, maxX / 2);
57
+ final padY = math.min(widget.padding, maxY / 2);
58
+ return Offset(
59
+ value.dx.clamp(padX, maxX - padX),
60
+ value.dy.clamp(padY, maxY - padY),
61
+ );
62
+ }
63
+
64
+ @override
65
+ void didUpdateWidget(MovableSurface oldWidget) {
66
+ super.didUpdateWidget(oldWidget);
67
+ if (!widget.enabled) {
68
+ _cancel('disabled', rebuild: false);
69
+ } else if (oldWidget.longPressDuration != widget.longPressDuration) {
70
+ _cancel('configuration_changed', rebuild: false);
71
+ }
72
+ if (oldWidget.position != widget.position && _pointerOrigin == null) {
73
+ _position.value = widget.position;
74
+ }
75
+ }
76
+
77
+ @override
78
+ void dispose() {
79
+ _position.dispose();
80
+ super.dispose();
81
+ }
82
+
83
+ void _start(LongPressStartDetails details) {
84
+ _pointerOrigin = details.globalPosition;
85
+ _itemOrigin = _bounded(_position.value);
86
+ if (widget.haptic) HapticFeedback.mediumImpact();
87
+ setState(() {});
88
+ widget.onStart?.call(_itemOrigin!);
89
+ }
90
+
91
+ void _cancel(
92
+ String reason, {
93
+ bool rebuild = true,
94
+ bool deferNotification = false,
95
+ }) {
96
+ if (_pointerOrigin == null) return;
97
+ final position = _bounded(_itemOrigin!);
98
+ _pointerOrigin = null;
99
+ _itemOrigin = null;
100
+ _pointerId = null;
101
+ _position.value = position;
102
+ if (rebuild) setState(() {});
103
+ if (deferNotification) {
104
+ WidgetsBinding.instance.addPostFrameCallback((_) {
105
+ if (mounted) widget.onCancel?.call(position, reason);
106
+ });
107
+ } else {
108
+ widget.onCancel?.call(position, reason);
109
+ }
110
+ }
111
+
112
+ void _move(LongPressMoveUpdateDetails details) {
113
+ if (_pointerOrigin == null) return;
114
+ _position.value = _bounded(
115
+ _itemOrigin! + details.globalPosition - _pointerOrigin!,
116
+ );
117
+ }
118
+
119
+ void _end(LongPressEndDetails details) {
120
+ if (_pointerOrigin == null) return;
121
+ final position = _bounded(
122
+ _itemOrigin! + details.globalPosition - _pointerOrigin!,
123
+ );
124
+ _position.value = position;
125
+ _pointerOrigin = null;
126
+ _itemOrigin = null;
127
+ setState(() {});
128
+ widget.onEnd(position);
129
+ }
130
+
131
+ @override
132
+ Widget build(BuildContext context) => LayoutBuilder(
133
+ builder: (context, constraints) {
134
+ final area = constraints.biggest;
135
+ if (!area.isFinite) {
136
+ return const Text('Movable requires bounded width and height');
137
+ }
138
+ final item = Size(
139
+ math.min(widget.itemSize.width, area.width),
140
+ math.min(widget.itemSize.height, area.height),
141
+ );
142
+ final viewport = MediaQuery.maybeSizeOf(context);
143
+ final changed =
144
+ _hasLayout &&
145
+ (_area != area || _item != item || _viewport != viewport);
146
+ _area = area;
147
+ _item = item;
148
+ _viewport = viewport;
149
+ _hasLayout = true;
150
+ if (changed) {
151
+ _layoutEpoch++;
152
+ _cancel('layout_changed', rebuild: false, deferNotification: true);
153
+ _pointerId = null;
154
+ }
155
+ return Stack(
156
+ children: [
157
+ ValueListenableBuilder<Offset>(
158
+ valueListenable: _position,
159
+ child: Listener(
160
+ onPointerDown: (event) => _pointerId ??= event.pointer,
161
+ onPointerUp: (event) {
162
+ if (_pointerId == event.pointer) _pointerId = null;
163
+ },
164
+ onPointerCancel: (event) {
165
+ if (_pointerId == event.pointer) {
166
+ _cancel('pointer_cancelled');
167
+ _pointerId = null;
168
+ }
169
+ },
170
+ child: RawGestureDetector(
171
+ key: ValueKey((widget.longPressDuration, _layoutEpoch)),
172
+ behavior: HitTestBehavior.opaque,
173
+ gestures: widget.enabled
174
+ ? {
175
+ LongPressGestureRecognizer:
176
+ GestureRecognizerFactoryWithHandlers<
177
+ LongPressGestureRecognizer
178
+ >(
179
+ () => LongPressGestureRecognizer(
180
+ duration: widget.longPressDuration,
181
+ ),
182
+ (recognizer) => recognizer
183
+ ..onLongPressStart = _start
184
+ ..onLongPressMoveUpdate = _move
185
+ ..onLongPressEnd = _end
186
+ ..onLongPressCancel = () =>
187
+ _cancel('gesture_cancelled'),
188
+ ),
189
+ }
190
+ : {},
191
+ child: DecoratedBox(
192
+ position: DecorationPosition.foreground,
193
+ decoration: BoxDecoration(
194
+ borderRadius: widget.dragBorderRadius,
195
+ border: _pointerOrigin == null ? null : widget.dragBorder,
196
+ ),
197
+ child: RepaintBoundary(child: widget.child),
198
+ ),
199
+ ),
200
+ ),
201
+ builder: (context, position, child) {
202
+ final bounded = _bounded(position);
203
+ return Positioned(
204
+ left: bounded.dx,
205
+ top: bounded.dy,
206
+ width: _item.width,
207
+ height: _item.height,
208
+ child: child!,
209
+ );
210
+ },
211
+ ),
212
+ ],
213
+ );
214
+ },
215
+ );
216
+ }
@@ -0,0 +1,16 @@
1
+ name: flet_movable
2
+ description: Native bounded movement for Flet controls
3
+ version: 0.1.0
4
+ publish_to: none
5
+ environment:
6
+ sdk: '>=3.8.0 <4.0.0'
7
+ flutter: '>=3.47.0'
8
+ dependencies:
9
+ flutter:
10
+ sdk: flutter
11
+ flet: '>=1.1.0 <1.2.0'
12
+ material_ui: ^1.6.0
13
+ dev_dependencies:
14
+ flutter_test:
15
+ sdk: flutter
16
+ flutter_lints: ^6.0.0