responds-forms 0.4__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,1418 @@
1
+ # widgets.py
2
+ from __future__ import annotations
3
+ from enum import Enum
4
+ import tkinter as tk
5
+ from tkinter import ttk
6
+ from typing import Optional, Callable, cast, Any, Literal
7
+
8
+ from pyflutter_ui.ui.core import (
9
+ BuildContext, Element, FlutterWidget, StatelessWidget, BaseMaterialApp, RenderObject,
10
+ inflate_widget, widget_exists
11
+ )
12
+ from pyflutter_ui.ui.container import Container, SizedBox
13
+ from pyflutter_ui.navigator import NavigatorApp, Navigator
14
+ from pyflutter_ui.scheduler import MainThreadScheduler
15
+
16
+
17
+ class TkMaterialApp(BaseMaterialApp):
18
+
19
+ def __init__(
20
+ self,
21
+ home: FlutterWidget, *,
22
+ title: str = "MyApp",
23
+ theme: Any = None,
24
+ height: int = 350,
25
+ width: int = 480,
26
+ ) -> None:
27
+ super().__init__(home, title, theme)
28
+ self._navigator = NavigatorApp(self)
29
+ self._width: int = width
30
+ self._height: int = height
31
+ self.root: tk.Tk = tk.Tk()
32
+
33
+ @property
34
+ def geometry(self) -> str:
35
+ return f'{self._width}x{self._height}'
36
+
37
+ def set_height(self, new: int):
38
+ self._height = new
39
+ self.update_geometry()
40
+
41
+ def set_width(self, new: int):
42
+ self._width = new
43
+ self.update_geometry()
44
+
45
+ def update_geometry(self):
46
+ self.root.geometry(self.geometry)
47
+
48
+ def get_navigator(self) -> Navigator:
49
+ return self._navigator
50
+
51
+ def run(self) -> None:
52
+ if self.root is None:
53
+ self.root = tk.Tk()
54
+
55
+ self.update_geometry()
56
+ self.root.title(self.title)
57
+
58
+ ##
59
+ MainThreadScheduler.attach(self.root)
60
+ ##
61
+ self.get_navigator().push(self.home)
62
+
63
+ try:
64
+ self.root.mainloop()
65
+ finally:
66
+ MainThreadScheduler.detach()
67
+
68
+
69
+ # ============================================================
70
+ # ALIGNMENTS
71
+ # ============================================================
72
+ class MainAxisAlignment(Enum):
73
+ START = "start"
74
+ CENTER = "center"
75
+ END = "end"
76
+ SPACE_BETWEEN = "space_between"
77
+ SPACE_AROUND = "space_around"
78
+ SPACE_EVENLY = "space_evenly"
79
+
80
+
81
+ class CrossAxisAlignment(Enum):
82
+ START = "start"
83
+ CENTER = "center"
84
+ END = "end"
85
+ STRETCH = "stretch"
86
+
87
+
88
+ class Alignment(Enum):
89
+ TOP_LEFT = "top_left"
90
+ TOP_CENTER = "top_center"
91
+ TOP_RIGHT = "top_right"
92
+
93
+ CENTER_LEFT = "center_left"
94
+ CENTER = "center"
95
+ CENTER_RIGHT = "center_right"
96
+
97
+ BOTTOM_LEFT = "bottom_left"
98
+ BOTTOM_CENTER = "bottom_center"
99
+ BOTTOM_RIGHT = "bottom_right"
100
+
101
+
102
+ class FlexRenderObject(RenderObject):
103
+
104
+ def __init__(
105
+ self,
106
+ direction: str, *,
107
+ main_axis_alignment: MainAxisAlignment = MainAxisAlignment.CENTER,
108
+ cross_axis_alignment: CrossAxisAlignment = CrossAxisAlignment.CENTER
109
+ ) -> None:
110
+ super().__init__()
111
+ self.direction = direction
112
+ self.main_axis_alignment: MainAxisAlignment = main_axis_alignment
113
+ self.cross_axis_alignment: CrossAxisAlignment = cross_axis_alignment
114
+
115
+ def apply_layout(self) -> None:
116
+ if self.tk_widget is None:
117
+ return
118
+
119
+ if self.direction == "row":
120
+ self.tk_widget.grid_rowconfigure(0, weight=1)
121
+ else:
122
+ self.tk_widget.grid_columnconfigure(0, weight=1)
123
+
124
+ # Calcula o anchor/sticky base para o cross axis
125
+ if self.direction == "row":
126
+ # ancora o filho à esquerda da célula quando main_axis é START
127
+ main_sticky = {
128
+ MainAxisAlignment.START: "w",
129
+ MainAxisAlignment.END: "e",
130
+ MainAxisAlignment.CENTER: "",
131
+ }.get(self.main_axis_alignment, "w")
132
+
133
+ cross_sticky = {
134
+ CrossAxisAlignment.START: "n",
135
+ CrossAxisAlignment.END: "s",
136
+ CrossAxisAlignment.CENTER: "",
137
+ CrossAxisAlignment.STRETCH: "ns",
138
+ }.get(self.cross_axis_alignment, "")
139
+ else:
140
+ main_sticky = {
141
+ MainAxisAlignment.START: "n",
142
+ MainAxisAlignment.END: "s",
143
+ MainAxisAlignment.CENTER: "",
144
+ }.get(self.main_axis_alignment, "n")
145
+
146
+ cross_sticky = {
147
+ CrossAxisAlignment.START: "w",
148
+ CrossAxisAlignment.END: "e",
149
+ CrossAxisAlignment.CENTER: "",
150
+ CrossAxisAlignment.STRETCH: "ew",
151
+ }.get(self.cross_axis_alignment, "")
152
+
153
+ for index, child in enumerate(self.children):
154
+ widget = child.tk_widget
155
+ if widget is None:
156
+ continue
157
+
158
+ flex = child.layout_data.flex
159
+ expand = child.layout_data.expand
160
+ fill_x = child.layout_data.fill_x
161
+ fill_y = child.layout_data.fill_y
162
+
163
+ if expand:
164
+ sticky = "nsew"
165
+ else:
166
+ # combina main + cross axis
167
+ sticky = main_sticky + cross_sticky
168
+ if fill_x:
169
+ sticky += "ew"
170
+ if fill_y:
171
+ sticky += "ns"
172
+
173
+ if self.direction == "row":
174
+ col_weight = flex if flex > 0 else 0
175
+ self.tk_widget.grid_columnconfigure(index, weight=col_weight)
176
+ widget.grid(row=0, column=index, sticky=sticky, padx=(0, 2))
177
+ else:
178
+ row_weight = flex if flex > 0 else 0
179
+ self.tk_widget.grid_rowconfigure(index, weight=row_weight)
180
+ widget.grid(row=index, column=0, sticky=sticky, pady=(0, 2))
181
+
182
+ def apply_layout_old(self) -> None:
183
+
184
+ if self.tk_widget is None:
185
+ return
186
+
187
+ # Row()
188
+ if self.direction == "row":
189
+ self.tk_widget.grid_rowconfigure(0, weight=1)
190
+ else:
191
+ # Column()
192
+ self.tk_widget.grid_columnconfigure(0, weight=1)
193
+
194
+ for index, child in enumerate(self.children):
195
+
196
+ widget = child.tk_widget
197
+ if widget is None:
198
+ continue
199
+
200
+ flex = child.layout_data.flex
201
+
202
+ expand = child.layout_data.expand
203
+
204
+ fill_x = child.layout_data.fill_x
205
+
206
+ fill_y = child.layout_data.fill_y
207
+
208
+ sticky = ""
209
+
210
+ if expand:
211
+ sticky = "nsew"
212
+ else:
213
+
214
+ if fill_x:
215
+ sticky += "ew"
216
+
217
+ if fill_y:
218
+ sticky += "ns"
219
+
220
+ weight = max(1, flex)
221
+
222
+ if self.direction == "row":
223
+ self.tk_widget.grid_columnconfigure(index, weight=weight)
224
+ widget.grid(row=0, column=index, sticky=sticky)
225
+ else:
226
+ self.tk_widget.grid_rowconfigure(index, weight=weight)
227
+ widget.grid(row=index, column=0, sticky=sticky)
228
+
229
+
230
+ # ============================================================
231
+ # TEXT
232
+ # ============================================================
233
+
234
+ class Text(StatelessWidget):
235
+
236
+ def __init__(self, text: str, key: str | None = None) -> None:
237
+ super().__init__(key)
238
+ self.text: str = text
239
+
240
+ def build(self, context: BuildContext) -> FlutterWidget:
241
+ return self
242
+
243
+ def create_element(self) -> Element:
244
+ return TextElement(self)
245
+
246
+
247
+ class TextElement(Element[ttk.Label]):
248
+
249
+ def __init__(self, widget: Text) -> None:
250
+ super().__init__(widget)
251
+
252
+ def get_widget_flutter(self) -> Text:
253
+ return self.widget_flutter
254
+
255
+ def mount(self, context: BuildContext) -> ttk.Label:
256
+ super().mount(context)
257
+
258
+ label = ttk.Label(context.parent_tk_widget, text=self.get_widget_flutter().text)
259
+ label.pack()
260
+ self.widget_tk = label
261
+ return label
262
+
263
+ def update(self, new_widget: FlutterWidget) -> None:
264
+ self.widget_flutter = cast(Text, new_widget)
265
+
266
+ if self.widget_tk is not None:
267
+ self.widget_tk.configure(text=self.widget_flutter.text)
268
+
269
+
270
+ # ============================================================
271
+ # PADDING
272
+ # ============================================================
273
+
274
+ class Padding(StatelessWidget):
275
+
276
+ def __init__(self, key: str | None = None, *, padding: int, child: FlutterWidget) -> None:
277
+ super().__init__(key)
278
+ self.padding = padding
279
+ self.child = child
280
+
281
+ def build(self, context: BuildContext) -> Padding:
282
+ return self
283
+
284
+ def create_element(self) -> PaddingElement:
285
+ return PaddingElement(self)
286
+
287
+ class PaddingElement(Element[ttk.Frame]):
288
+
289
+ def __init__(self, widget: Padding) -> None:
290
+ super().__init__(widget)
291
+
292
+ def update(self, new_widget: FlutterWidget) -> None:
293
+ context = self.get_context() # salva ANTES de trocar o widget
294
+ self.widget_flutter = cast(Padding, new_widget)
295
+ self.widget_flutter.context = context
296
+ if context:
297
+ self.unmount()
298
+ self.mount(context)
299
+
300
+ def get_widget_flutter(self) -> Padding:
301
+ return self.widget_flutter
302
+
303
+ def mount(self, context: BuildContext) -> ttk.Frame:
304
+ super().mount(context)
305
+ frame = ttk.Frame(context.parent_tk_widget)
306
+ frame.pack(padx=self.get_widget_flutter().padding, pady=self.get_widget_flutter().padding)
307
+ child_element = self.get_widget_flutter().child.create_element()
308
+
309
+ child_context = (
310
+ BuildContext(
311
+ app=context.app,
312
+ parent_widget=frame,
313
+ parent_context=context
314
+ )
315
+ )
316
+
317
+ child_element.mount(child_context)
318
+ self.widget_tk = frame
319
+ return frame
320
+
321
+
322
+ # ============================================================
323
+ # CENTER
324
+ # ============================================================
325
+
326
+ class Center(StatelessWidget):
327
+
328
+ def __init__(self, child: FlutterWidget) -> None:
329
+ super().__init__()
330
+ self.child = child
331
+
332
+ def build(self, context: BuildContext) -> Center:
333
+ return self
334
+
335
+ def create_element(self) -> CenterElement:
336
+ return CenterElement(self)
337
+
338
+
339
+ class CenterElement(Element[ttk.Frame]):
340
+
341
+ def __init__(self, widget: Center) -> None:
342
+ super().__init__(widget)
343
+
344
+ def get_widget_flutter(self) -> Center:
345
+ return self.widget_flutter
346
+
347
+ def mount(self, context: BuildContext) -> ttk.Frame:
348
+ super().mount(context)
349
+ frame = ttk.Frame(context.parent_tk_widget)
350
+ frame.pack(expand=True)
351
+ child_element = self.get_widget_flutter().child.create_element()
352
+
353
+ child_context = (
354
+ BuildContext(
355
+ app=context.app,
356
+ parent_widget=frame,
357
+ parent_context=context
358
+ )
359
+ )
360
+
361
+ child_element.mount(child_context)
362
+ self.widget_tk = frame
363
+ return frame
364
+
365
+
366
+ # ============================================================
367
+ # ALIGN
368
+ # ============================================================
369
+
370
+ class Align(StatelessWidget):
371
+
372
+ def __init__(
373
+ self,
374
+ child: FlutterWidget,
375
+ alignment: Alignment = (
376
+ Alignment.CENTER
377
+ )
378
+ ) -> None:
379
+ super().__init__()
380
+
381
+ self.child = child
382
+ self.alignment = alignment
383
+
384
+ def build(
385
+ self,
386
+ context: BuildContext
387
+ ) -> FlutterWidget:
388
+ return self
389
+
390
+ def create_element(
391
+ self
392
+ ) -> Element:
393
+ return AlignElement(self)
394
+
395
+
396
+ class AlignElement(Element[ttk.Frame]):
397
+
398
+ def __init__(self, widget: Align) -> None:
399
+ super().__init__(widget)
400
+
401
+ def get_widget_flutter(self) -> Align:
402
+ return self.widget_flutter
403
+
404
+ def get_widget_tk(self) -> ttk.Frame:
405
+ return super().get_widget_tk()
406
+
407
+ def mount(self, context: BuildContext) -> ttk.Frame:
408
+ super().mount(context)
409
+ frame = ttk.Frame(context.parent_tk_widget)
410
+
411
+ anchor = {
412
+ Alignment.TOP_LEFT: "nw",
413
+ Alignment.TOP_CENTER: "n",
414
+ Alignment.TOP_RIGHT: "ne",
415
+ Alignment.CENTER_LEFT: "w",
416
+ Alignment.CENTER: "center",
417
+ Alignment.CENTER_RIGHT: "e",
418
+ Alignment.BOTTOM_LEFT: "sw",
419
+ Alignment.BOTTOM_CENTER: "s",
420
+ Alignment.BOTTOM_RIGHT: "se",
421
+ }.get(self.get_widget_flutter().alignment, "center")
422
+
423
+ frame.pack(fill="both", expand=True)
424
+
425
+ child_element = self.get_widget_flutter().child.create_element()
426
+ child_context = context.create_child_context(frame)
427
+ child_element.mount(child_context)
428
+
429
+ # ancora o filho dentro do frame
430
+ if child_element.widget_tk is not None:
431
+ child_element.widget_tk.pack_configure(anchor=anchor, expand=False)
432
+
433
+ self.widget_tk = frame
434
+ return frame
435
+
436
+
437
+ # ============================================================
438
+ # DIVIDER
439
+ # ============================================================
440
+
441
+ class Divider(StatelessWidget):
442
+
443
+ def __init__(
444
+ self, key: str | None = None, *,
445
+ orientation: Literal["horizontal", "vertical"] = "horizontal"
446
+ ) -> None:
447
+ super().__init__(key)
448
+ self.orientation: Literal["horizontal", "vertical"] = orientation
449
+
450
+ def create_element(self) -> Element:
451
+ return DividerElement(self)
452
+
453
+ def build(self, context: BuildContext) -> FlutterWidget:
454
+ return self
455
+
456
+
457
+ class DividerElement(Element):
458
+
459
+ def __init__(self, widget: Divider) -> None:
460
+ super().__init__(widget)
461
+
462
+ def get_widget_flutter(self) -> Divider:
463
+ return self.widget_flutter
464
+
465
+ def mount(self, context: BuildContext) -> ttk.Separator:
466
+ super().mount(context)
467
+ separator = ttk.Separator(
468
+ context.parent_tk_widget,
469
+ orient=self.get_widget_flutter().orientation
470
+ )
471
+
472
+ if self.get_widget_flutter().orientation == "horizontal":
473
+ separator.pack(fill="x", padx=4)
474
+ elif self.get_widget_flutter().orientation == "vertical":
475
+ separator.pack(fill='y', pady=4)
476
+
477
+ self.widget_tk = separator
478
+ return separator
479
+
480
+
481
+ # ============================================================
482
+ # CARD
483
+ # ============================================================
484
+
485
+ class Card(Container):
486
+
487
+ def __init__(
488
+ self,
489
+ child: Optional[FlutterWidget] = None,
490
+ width: int | None = None,
491
+ height: int | None = None,
492
+ padding: int = 0
493
+ ) -> None:
494
+ super().__init__(child, width, height, padding)
495
+
496
+
497
+ # ============================================================
498
+ # LIST ORIENTATION WIDGETS (super classe de Row e Column)
499
+ # ============================================================
500
+
501
+ class ListOrientationWidgets(StatelessWidget):
502
+ """
503
+ Base comum para widgets de lista com orientação fixa (Row/Column).
504
+ Centraliza toda a lógica de montagem, layout via FlexRenderObject e
505
+ atualização (update) em um único lugar — bugs corrigidos aqui valem
506
+ automaticamente para Row e Column.
507
+ """
508
+
509
+ def __init__(
510
+ self,
511
+ children: list[FlutterWidget],
512
+ direction: str,
513
+ main_axis_alignment: MainAxisAlignment = MainAxisAlignment.START,
514
+ cross_axis_alignment: CrossAxisAlignment = CrossAxisAlignment.CENTER
515
+ ) -> None:
516
+ super().__init__()
517
+ self.children: list[FlutterWidget] = children
518
+ self.direction: str = direction
519
+ self.main_axis_alignment = main_axis_alignment
520
+ self.cross_axis_alignment = cross_axis_alignment
521
+
522
+ def build(self, context: BuildContext) -> "ListOrientationWidgets":
523
+ return self
524
+
525
+ def create_element(self) -> ListOrientationWidgetsElement:
526
+ return ListOrientationWidgetsElement(self)
527
+
528
+
529
+ class ListOrientationWidgetsElement(Element[ttk.Frame]):
530
+
531
+ def __init__(self, widget: ListOrientationWidgets) -> None:
532
+ super().__init__(widget)
533
+ self._child_elements: list[Element] = []
534
+
535
+ self.render_object = FlexRenderObject(
536
+ direction=widget.direction,
537
+ main_axis_alignment=widget.main_axis_alignment,
538
+ cross_axis_alignment=widget.cross_axis_alignment,
539
+ )
540
+
541
+ def get_widget_flutter(self) -> ListOrientationWidgets:
542
+ return self.widget_flutter
543
+
544
+ def get_widget_tk(self) -> ttk.Frame:
545
+ return super().get_widget_tk()
546
+
547
+ def _pack_frame(self, frame: ttk.Frame) -> None:
548
+ """
549
+ Empacotamento do frame principal. Comportamento padrão = Column
550
+ (ocupa todo o espaço disponível). Subclasses (ex: RowElement)
551
+ sobrescrevem quando precisam de outro comportamento de packing.
552
+ """
553
+ frame.pack(fill="both", expand=True)
554
+
555
+ def update(self, new_widget: FlutterWidget) -> None:
556
+ # IMPORTANTE: usa get_context() (== widget_flutter.context), nunca
557
+ # um atributo tipo "_parent_context" guardado manualmente.
558
+ # Element.mount() sempre seta widget_flutter.context em TODA
559
+ # subclasse (é feito na base), então esta fonte nunca fica
560
+ # desatualizada — foi exatamente a falta disso que causava o bug
561
+ # de "monta certo na primeira vez, mas nunca atualiza depois".
562
+
563
+ context: BuildContext | None = self.get_context()
564
+
565
+ if context:
566
+ new_widget.context = context
567
+ self.widget_flutter = new_widget
568
+
569
+ self.render_object = FlexRenderObject(
570
+ direction=self.get_widget_flutter().direction,
571
+ main_axis_alignment=self.get_widget_flutter().main_axis_alignment,
572
+ cross_axis_alignment=self.get_widget_flutter().cross_axis_alignment,
573
+ )
574
+ self.unmount()
575
+ self.mount(context)
576
+
577
+ def mount(self, context: BuildContext) -> ttk.Frame:
578
+ super().mount(context)
579
+
580
+ if not context.parent_tk_widget.winfo_exists():
581
+ return None # pai já destruído, aborta silenciosamente em vez de TclError
582
+
583
+ frame = ttk.Frame(context.parent_tk_widget)
584
+ self._pack_frame(frame)
585
+ self.render_object.attach_widget(frame)
586
+
587
+ self._child_elements: list[Element] = []
588
+
589
+ for child in self.get_widget_flutter().children:
590
+ # wrapper entra no frame via grid (apply_layout faz isso)
591
+ # NÃO chamar pack/grid aqui no wrapper
592
+ wrapper = ttk.Frame(frame)
593
+
594
+ child_element: Element = inflate_widget(child)
595
+ child_context: BuildContext = context.create_child_context(wrapper)
596
+ child_element.mount(child_context)
597
+ self._child_elements.append(child_element)
598
+
599
+ render_object = child_element.render_object
600
+ render_object.attach_widget(wrapper)
601
+ self.render_object.add_child(render_object)
602
+
603
+ self.render_object.apply_layout()
604
+ self.widget_tk = frame
605
+ return frame
606
+
607
+ def unmount(self) -> None:
608
+ for child_element in self._child_elements:
609
+ child_element.unmount()
610
+ self._child_elements = []
611
+ super().unmount()
612
+
613
+
614
+ # ============================================================
615
+ # ROW
616
+ # ============================================================
617
+ class Row(ListOrientationWidgets):
618
+
619
+ def __init__(
620
+ self,
621
+ children: list[FlutterWidget],
622
+ main_axis_alignment: MainAxisAlignment = MainAxisAlignment.START,
623
+ cross_axis_alignment: CrossAxisAlignment = CrossAxisAlignment.CENTER
624
+ ) -> None:
625
+ super().__init__(children, "row", main_axis_alignment, cross_axis_alignment)
626
+
627
+ def create_element(self) -> RowElement:
628
+ return RowElement(self)
629
+
630
+
631
+ class RowElement(ListOrientationWidgetsElement):
632
+
633
+ def __init__(self, widget: ListOrientationWidgets) -> None:
634
+ super().__init__(widget)
635
+
636
+ def _pack_frame(self, frame: ttk.Frame) -> None:
637
+ frame.pack(fill="x", expand=False, padx=1, pady=1)
638
+
639
+
640
+ # ============================================================
641
+ # COLUMN
642
+ # ============================================================
643
+ class Column(ListOrientationWidgets):
644
+
645
+ def __init__(
646
+ self,
647
+ children: list[FlutterWidget],
648
+ main_axis_alignment: MainAxisAlignment = MainAxisAlignment.START,
649
+ cross_axis_alignment: CrossAxisAlignment = CrossAxisAlignment.CENTER
650
+ ) -> None:
651
+ super().__init__(children, "column", main_axis_alignment, cross_axis_alignment)
652
+
653
+ def create_element(self) -> "ColumnElement":
654
+ return ColumnElement(self)
655
+
656
+
657
+ class ColumnElement(ListOrientationWidgetsElement):
658
+
659
+ def __init__(self, widget: ListOrientationWidgets) -> None:
660
+ super().__init__(widget)
661
+
662
+ # ============================================================
663
+ # ROW
664
+ # ============================================================
665
+
666
+ class OldRow(StatelessWidget):
667
+
668
+ def __init__(
669
+ self,
670
+ children: list[FlutterWidget],
671
+ main_axis_alignment: MainAxisAlignment = MainAxisAlignment.START,
672
+ cross_axis_alignment: CrossAxisAlignment = CrossAxisAlignment.CENTER
673
+ ) -> None:
674
+ super().__init__()
675
+ self.children = children
676
+ self.main_axis_alignment = main_axis_alignment
677
+ self.cross_axis_alignment = cross_axis_alignment
678
+
679
+ def build(self, context: BuildContext) -> Row:
680
+ return self
681
+
682
+ def create_element(self) -> RowElement:
683
+ return RowElement(self)
684
+
685
+
686
+ class OldRowElement(Element[ttk.Frame]):
687
+
688
+ def __init__(self, widget: Row) -> None:
689
+ super().__init__(widget)
690
+ self._child_elements: list[Element] = []
691
+
692
+ self.flex_render_obj = FlexRenderObject(
693
+ direction="row",
694
+ main_axis_alignment=widget.main_axis_alignment,
695
+ cross_axis_alignment=widget.cross_axis_alignment
696
+ )
697
+
698
+ def update(self, new_widget: FlutterWidget) -> None:
699
+ super().update(new_widget)
700
+
701
+ if self.get_context():
702
+ self.flex_render_obj = FlexRenderObject(
703
+ direction="row",
704
+ main_axis_alignment=self.get_widget_flutter().main_axis_alignment,
705
+ cross_axis_alignment=self.get_widget_flutter().cross_axis_alignment,
706
+ )
707
+ self.unmount()
708
+ self.mount(self.get_context())
709
+
710
+ def get_widget_flutter(self) -> Row:
711
+ return self.widget_flutter
712
+
713
+ def mount(self, context: BuildContext) -> ttk.Frame:
714
+ super().mount(context)
715
+ frame = ttk.Frame(context.parent_tk_widget)
716
+ frame.pack(fill="x", expand=False, padx=1, pady=1)
717
+ self.flex_render_obj.attach_widget(frame)
718
+
719
+ self._child_elements: list[Element] = []
720
+ for index, child in enumerate(self.get_widget_flutter().children):
721
+ wrapper = ttk.Frame(frame)
722
+ # NÃO chama wrapper.pack() — o apply_layout fará wrapper.grid()
723
+ child_element = inflate_widget(child)
724
+ child_context = context.create_child_context(wrapper)
725
+ child_element.mount(child_context)
726
+ self._child_elements.append(child_element)
727
+
728
+ render_object = child_element.render_object
729
+ render_object.attach_widget(wrapper)
730
+ self.flex_render_obj.add_child(render_object)
731
+
732
+ self.flex_render_obj.apply_layout()
733
+ self.widget_tk = frame
734
+ return frame
735
+
736
+ def unmount(self) -> None:
737
+ for child_element in self._child_elements:
738
+ child_element.unmount()
739
+ self._child_elements = []
740
+ super().unmount()
741
+
742
+ # ============================================================
743
+ # COLUMN
744
+ # ============================================================
745
+ class OldColumn(StatelessWidget):
746
+
747
+ def __init__(
748
+ self,
749
+ children: list[FlutterWidget],
750
+ main_axis_alignment: MainAxisAlignment = MainAxisAlignment.START,
751
+ cross_axis_alignment: CrossAxisAlignment = CrossAxisAlignment.CENTER
752
+ ) -> None:
753
+ super().__init__()
754
+
755
+ self.children = children
756
+ self.main_axis_alignment = main_axis_alignment
757
+ self.cross_axis_alignment = cross_axis_alignment
758
+
759
+ def build(self, context: BuildContext) -> Column:
760
+ return self
761
+
762
+ def create_element(self) -> ColumnElement:
763
+ return ColumnElement(self)
764
+
765
+
766
+ class OldColumnElement(Element[ttk.Frame]):
767
+
768
+ def __init__(self, widget: Column) -> None:
769
+ super().__init__(widget)
770
+
771
+ self.render_object = FlexRenderObject(
772
+ direction="column",
773
+ main_axis_alignment=widget.main_axis_alignment,
774
+ cross_axis_alignment=widget.cross_axis_alignment,
775
+ )
776
+ self._child_elements: list[Element] = []
777
+
778
+ def get_widget_flutter(self) -> Column:
779
+ return self.widget_flutter
780
+
781
+ def get_widget_tk(self) -> ttk.Frame:
782
+ return super().get_widget_tk()
783
+
784
+ def _column_element_update_fixed(self, new_widget: FlutterWidget) -> None:
785
+ context = self.get_context() # em vez de self._parent_context
786
+
787
+ if context:
788
+ new_widget.context = context
789
+ self.widget_flutter = new_widget
790
+ self.render_object = FlexRenderObject(
791
+ direction="column",
792
+ main_axis_alignment=self.get_widget_flutter().main_axis_alignment,
793
+ cross_axis_alignment=self.get_widget_flutter().cross_axis_alignment,
794
+ )
795
+ self.unmount()
796
+ self.mount(context)
797
+
798
+ def update(self, new_widget: FlutterWidget) -> None:
799
+ #super().update(new_widget)
800
+ self._column_element_update_fixed(new_widget)
801
+
802
+ def mount(self, context: BuildContext) -> ttk.Frame:
803
+ super().mount(context)
804
+
805
+ if not context.parent_tk_widget.winfo_exists():
806
+ return None # pai já destruído, aborta silenciosamente em vez de TclError
807
+
808
+ frame = ttk.Frame(context.parent_tk_widget)
809
+ frame.pack(fill="both", expand=True)
810
+ self.render_object.attach_widget(frame)
811
+
812
+ self._child_elements: list[Element] = []
813
+ for child in self.get_widget_flutter().children:
814
+ # wrapper entra no frame via grid (apply_layout faz isso)
815
+ # NÃO chamar pack/grid aqui no wrapper
816
+ wrapper = ttk.Frame(frame)
817
+
818
+ child_element = inflate_widget(child)
819
+ child_context = context.create_child_context(wrapper)
820
+ child_element.mount(child_context)
821
+ self._child_elements.append(child_element)
822
+
823
+ render_object = child_element.render_object
824
+ render_object.attach_widget(wrapper)
825
+ self.render_object.add_child(render_object)
826
+
827
+ self.render_object.apply_layout()
828
+ self.widget_tk = frame
829
+ return frame
830
+
831
+ def unmount(self) -> None:
832
+ for child_element in self._child_elements:
833
+ child_element.unmount()
834
+ self._child_elements = []
835
+ super().unmount()
836
+
837
+
838
+ # ============================================================
839
+ # TEXT FIELD
840
+ # ============================================================
841
+
842
+ class TextField(StatelessWidget):
843
+
844
+ def __init__(
845
+ self,
846
+ text: str = "",
847
+ hint: str = "",
848
+ on_changed: Callable[[str], None] | None = None,
849
+ width: int = 30,
850
+ ) -> None:
851
+ super().__init__()
852
+ self.text = text
853
+ self.hint = hint
854
+ self.on_changed = on_changed
855
+ self.width = width
856
+
857
+ def build(self, context: BuildContext) -> FlutterWidget:
858
+ return self
859
+
860
+ def create_element(self) -> Element:
861
+ return TextFieldElement(self)
862
+
863
+
864
+ class TextFieldElement(Element[ttk.Entry]):
865
+
866
+ def __init__(self, widget: TextField) -> None:
867
+ super().__init__(widget)
868
+
869
+ def get_widget_flutter(self) -> TextField:
870
+ return self.widget_flutter
871
+
872
+ def mount(self, context: BuildContext) -> tk.Widget:
873
+ super().mount(context)
874
+ var = tk.StringVar(value=self.get_widget_flutter().text)
875
+ entry = ttk.Entry(context.parent_tk_widget, textvariable=var, width=self.widget_flutter.width)
876
+
877
+ if self.get_widget_flutter().on_changed:
878
+ var.trace_add(
879
+ "write", lambda *_: self.get_widget_flutter().on_changed(var.get())
880
+ )
881
+
882
+ entry.pack(fill="x")
883
+ self.widget_tk = entry
884
+ return entry
885
+
886
+
887
+ # ============================================================
888
+ # ICON BUTTON
889
+ # ============================================================
890
+
891
+ class IconButton(StatelessWidget):
892
+
893
+ def __init__(self, text: str, on_pressed: Callable[[], None]) -> None:
894
+ super().__init__()
895
+ self.text = text
896
+ self.on_pressed = on_pressed
897
+
898
+ def build(self, context: BuildContext) -> FlutterWidget:
899
+ return self
900
+
901
+ def create_element(self) -> Element:
902
+ return IconButtonElement(self)
903
+
904
+
905
+ class IconButtonElement(Element[ttk.Button]):
906
+
907
+ def __init__(self, widget: IconButton) -> None:
908
+ super().__init__(widget)
909
+
910
+ def get_widget_flutter(self) -> IconButton:
911
+ return self.widget_flutter
912
+
913
+ def mount(self, context: BuildContext) -> tk.Widget:
914
+ super().mount(context)
915
+ button = ttk.Button(
916
+ context.parent_tk_widget, text=self.widget_flutter.text, command=self.widget_flutter.on_pressed, width=3
917
+ )
918
+
919
+ button.pack()
920
+ self.widget_tk = button
921
+ return button
922
+
923
+
924
+ # ============================================================
925
+ # CHECKBOX
926
+ # ============================================================
927
+
928
+ class Checkbox(StatelessWidget):
929
+
930
+ def __init__(
931
+ self,
932
+ value: bool = False,
933
+ text: str = "",
934
+ on_changed: Callable[[bool], None] | None = None
935
+ ) -> None:
936
+ super().__init__()
937
+ self.value = value
938
+ self.text = text
939
+ self.on_changed = on_changed
940
+
941
+ def build(self, context: BuildContext) -> FlutterWidget:
942
+ return self
943
+
944
+ def create_element(self) -> Element:
945
+ return CheckboxElement(self)
946
+
947
+
948
+ class CheckboxElement(Element[ttk.Checkbutton]):
949
+
950
+ def __init__(self, widget: Checkbox) -> None:
951
+ super().__init__(widget)
952
+
953
+ def get_widget_flutter(self) -> Checkbox:
954
+ return self.widget_flutter
955
+
956
+ def mount(self, context: BuildContext) -> ttk.Checkbutton:
957
+ super().mount(context)
958
+ var = tk.BooleanVar(value=self.get_widget_flutter().value)
959
+ checkbox = ttk.Checkbutton(
960
+ context.parent_tk_widget, text=self.get_widget_flutter().text, variable=var
961
+ )
962
+
963
+ if self.get_widget_flutter().on_changed:
964
+ checkbox.configure(
965
+ command=lambda: self.get_widget_flutter().on_changed(var.get())
966
+ )
967
+
968
+ checkbox.pack(anchor="w")
969
+ self.widget_tk = checkbox
970
+ return checkbox
971
+
972
+
973
+ # ============================================================
974
+ # SWITCH
975
+ # ============================================================
976
+
977
+ class Switch(Checkbox):
978
+
979
+ def __init__(
980
+ self,
981
+ value: bool = False,
982
+ text: str = "",
983
+ on_changed: Callable[[bool], None] | None = None
984
+ ) -> None:
985
+ super().__init__(value, text, on_changed)
986
+
987
+
988
+
989
+ # ============================================================
990
+ # IMAGE
991
+ # ============================================================
992
+
993
+ class Image(StatelessWidget):
994
+
995
+ def __init__(self, path: str) -> None:
996
+ super().__init__()
997
+ self.path = path
998
+
999
+ def build(self, context: BuildContext) -> FlutterWidget:
1000
+ return self
1001
+
1002
+ def create_element(self) -> Element:
1003
+ return ImageElement(self)
1004
+
1005
+
1006
+ class ImageElement(Element[ttk.Label]):
1007
+
1008
+ def __init__(self, widget: Image) -> None:
1009
+ super().__init__(widget)
1010
+
1011
+ def get_widget_flutter(self) -> Image:
1012
+ return self.widget_flutter
1013
+
1014
+ def mount(self, context: BuildContext) -> ttk.Label:
1015
+ super().mount(context)
1016
+ image = tk.PhotoImage(file=self.get_widget_flutter().path)
1017
+ label = ttk.Label(context.parent_tk_widget, image=image)
1018
+ #label.image = image
1019
+ label.pack()
1020
+ self.widget_tk = label
1021
+ return label
1022
+
1023
+
1024
+ # ============================================================
1025
+ # ICON
1026
+ # ============================================================
1027
+ class Icon(Text):
1028
+
1029
+ def __init__(self, text: str) -> None:
1030
+ super().__init__(text)
1031
+
1032
+
1033
+ # ============================================================
1034
+ # APP BAR
1035
+ # ============================================================
1036
+ class AppBar(StatelessWidget):
1037
+
1038
+ def __init__(self, title: str, actions: list[FlutterWidget] | None = None, height: int = 48) -> None:
1039
+ super().__init__()
1040
+ self.title = title
1041
+ self.actions = actions or []
1042
+ self.height = height
1043
+
1044
+ def build(self, context: BuildContext) -> FlutterWidget:
1045
+ return self
1046
+
1047
+ def create_element(self) -> Element:
1048
+ return AppBarElement(self)
1049
+
1050
+
1051
+ class AppBarElement(Element[ttk.Frame]):
1052
+
1053
+ def __init__(self, widget: AppBar) -> None:
1054
+ super().__init__(widget)
1055
+ self._action_elements: list[Element] = []
1056
+
1057
+ def get_widget_flutter(self) -> AppBar:
1058
+ return self.widget_flutter
1059
+
1060
+ def mount(self, context: BuildContext) -> ttk.Frame:
1061
+ super().mount(context)
1062
+ frame = ttk.Frame(context.parent_tk_widget)
1063
+ frame.pack(fill="x")
1064
+ title = ttk.Label(frame, text=self.get_widget_flutter().title)
1065
+ title.pack(side="left", padx=8, pady=8)
1066
+
1067
+ self._action_elements = []
1068
+ for action in self.get_widget_flutter().actions:
1069
+ wrapper = ttk.Frame(frame)
1070
+ wrapper.pack(side="right")
1071
+ child = action.create_element()
1072
+ child.mount(context.create_child_context(wrapper))
1073
+ self._action_elements.append(child)
1074
+
1075
+ self.widget_tk = frame
1076
+ return frame
1077
+
1078
+ def unmount(self) -> None:
1079
+ for child in self._action_elements:
1080
+ child.unmount()
1081
+ self._action_elements = []
1082
+ super().unmount()
1083
+
1084
+ # ============================================================
1085
+ # SCAFFOLD
1086
+ # ============================================================
1087
+
1088
+ class Scaffold(StatelessWidget):
1089
+
1090
+ def __init__(
1091
+ self,
1092
+ body: FlutterWidget, *,
1093
+ app_bar: AppBar | None = None,
1094
+ floating_action_button: FlutterWidget | None = None,
1095
+ drawer: FlutterWidget | None = None,
1096
+ bottom_navigation_bar: FlutterWidget | None = None
1097
+ ) -> None:
1098
+ super().__init__()
1099
+
1100
+ self.body = body
1101
+ self.app_bar = app_bar
1102
+ self.drawer = drawer
1103
+ self.bottom_navigation_bar = bottom_navigation_bar
1104
+ self.floating_action_button = floating_action_button
1105
+
1106
+ def build(self, context: BuildContext) -> FlutterWidget:
1107
+ return self
1108
+
1109
+ def create_element(self) -> Element:
1110
+ return ScaffoldElement(self)
1111
+
1112
+
1113
+ class ScaffoldElement(Element):
1114
+
1115
+ def __init__(self, widget: Scaffold) -> None:
1116
+ super().__init__(widget)
1117
+ self._app_bar_element = None
1118
+ self._body_element = None
1119
+ self._bottom_nav_element = None
1120
+
1121
+ def update_widget(self, widget: FlutterWidget) -> None:
1122
+ super().update_widget(widget)
1123
+
1124
+ def update(self, new_widget: FlutterWidget) -> None:
1125
+ super().update(new_widget)
1126
+
1127
+ #old_widget = self.get_widget()
1128
+ self.widget_flutter = cast(Scaffold, new_widget)
1129
+
1130
+ # Se você quiser simplificar para testar se funciona,
1131
+ # pode desmontar e remontar tudo, ou implementar a atualização dos filhos:
1132
+ if self.widget_tk is not None:
1133
+ # Força a reconstrução do conteúdo destruindo o frame antigo e criando um novo
1134
+ context = self.get_context()
1135
+
1136
+ if context:
1137
+ self.unmount()
1138
+ self.mount(context)
1139
+
1140
+
1141
+ def get_widget_flutter(self) -> Scaffold:
1142
+ return self.widget_flutter
1143
+
1144
+ def mount(self, context: BuildContext) -> tk.Widget:
1145
+ super().mount(context)
1146
+ root_frame = ttk.Frame(context.parent_tk_widget)
1147
+ root_frame.pack(fill="both", expand=True)
1148
+
1149
+ self._app_bar_element = None
1150
+ if self.get_widget_flutter().app_bar:
1151
+ self._app_bar_element = self.get_widget_flutter().app_bar.create_element()
1152
+ self._app_bar_element.mount(context.create_child_context(root_frame))
1153
+
1154
+ body_container = ttk.Frame(root_frame)
1155
+ body_container.pack(fill="both", expand=True)
1156
+ self._body_element = self.get_widget_flutter().body.create_element()
1157
+ self._body_element.mount(context.create_child_context(body_container))
1158
+
1159
+ self._bottom_nav_element = None
1160
+ if self.get_widget_flutter().bottom_navigation_bar:
1161
+ self._bottom_nav_element = self.get_widget_flutter().bottom_navigation_bar.create_element()
1162
+ self._bottom_nav_element.mount(context.create_child_context(root_frame))
1163
+
1164
+ self.widget_tk = root_frame
1165
+ return root_frame
1166
+
1167
+ def unmount(self) -> None:
1168
+ for element in (self._app_bar_element, self._body_element, self._bottom_nav_element):
1169
+ if element is not None:
1170
+ element.unmount()
1171
+ self._app_bar_element = self._body_element = self._bottom_nav_element = None
1172
+ super().unmount()
1173
+
1174
+ def mount_old(self, context: BuildContext) -> tk.Widget:
1175
+ #self.widget.context = context
1176
+ super().mount(context)
1177
+
1178
+ root = ttk.Frame(context.parent_tk_widget)
1179
+ root.pack(fill="both", expand=True)
1180
+
1181
+ if self.get_widget_flutter().app_bar:
1182
+ self.get_widget_flutter().app_bar.create_element().mount(
1183
+ context.create_child_context(root)
1184
+ )
1185
+
1186
+ body_container = ttk.Frame(root)
1187
+ body_container.pack(
1188
+ fill="both",
1189
+ expand=True
1190
+ )
1191
+
1192
+ self.get_widget_flutter().body.create_element().mount(
1193
+ context.create_child_context(body_container)
1194
+ )
1195
+
1196
+ if self.get_widget_flutter().bottom_navigation_bar:
1197
+ self.get_widget_flutter().bottom_navigation_bar.create_element().mount(
1198
+ context.create_child_context(root)
1199
+ )
1200
+
1201
+ if self.get_widget_flutter().floating_action_button:
1202
+ fab_frame = ttk.Frame(root)
1203
+
1204
+ fab_frame.place(
1205
+ relx=0.95,
1206
+ rely=0.95,
1207
+ anchor="se"
1208
+ )
1209
+
1210
+ self.get_widget_flutter().floating_action_button.create_element().mount(
1211
+ context.create_child_context(fab_frame)
1212
+ )
1213
+
1214
+ self.widget_tk = root
1215
+ return root
1216
+
1217
+
1218
+ # ============================================================
1219
+ # SAFE AREA
1220
+ # ============================================================
1221
+
1222
+ class SafeArea(Padding):
1223
+
1224
+ def __init__(self, child: FlutterWidget) -> None:
1225
+ super().__init__(
1226
+ padding=8,
1227
+ child=child
1228
+ )
1229
+
1230
+
1231
+ # ============================================================
1232
+ # DRAWER
1233
+ # ============================================================
1234
+
1235
+ class Drawer(Container):
1236
+
1237
+ def __init__(
1238
+ self,
1239
+ child: Optional[FlutterWidget] = None,
1240
+ width: int | None = None,
1241
+ height: int | None = None,
1242
+ padding: int = 0
1243
+ ) -> None:
1244
+ super().__init__(child, width, height, padding)
1245
+
1246
+
1247
+
1248
+ # ============================================================
1249
+ # BOTTOM NAVIGATION BAR
1250
+ # ============================================================
1251
+
1252
+ class BottomNavigationBar(StatelessWidget):
1253
+
1254
+ def __init__(
1255
+ self,
1256
+ items: list[str],
1257
+ on_selected: Callable[[int], None]
1258
+ ) -> None:
1259
+ super().__init__()
1260
+ self.items = items
1261
+ self.on_selected = on_selected
1262
+
1263
+ def build(self, context: BuildContext) -> FlutterWidget:
1264
+ return self
1265
+
1266
+ def create_element(self) -> Element:
1267
+ return BottomNavigationBarElement(self)
1268
+
1269
+
1270
+ class BottomNavigationBarElement(Element[ttk.Frame]):
1271
+
1272
+ def __init__(self, widget: BottomNavigationBar) -> None:
1273
+ super().__init__(widget)
1274
+
1275
+ def get_widget_flutter(self) -> BottomNavigationBar:
1276
+ return self.widget_flutter
1277
+
1278
+ def mount(self, context: BuildContext) -> ttk.Frame:
1279
+ super().mount(context)
1280
+ frame = ttk.Frame(context.parent_tk_widget)
1281
+ frame.pack(fill="x")
1282
+
1283
+ for index, item in enumerate(self.get_widget_flutter().items):
1284
+ button = ttk.Button(
1285
+ frame,
1286
+ text=item,
1287
+ command=lambda i=index:
1288
+ self.get_widget_flutter().on_selected(i)
1289
+ )
1290
+ button.pack(side="left", expand=True, fill="x")
1291
+
1292
+ self.widget_tk = frame
1293
+ return frame
1294
+
1295
+
1296
+ # ============================================================
1297
+ # WRAP
1298
+ # ============================================================
1299
+
1300
+ class Wrap(Column):
1301
+
1302
+ def __init__(
1303
+ self, children: list[FlutterWidget],
1304
+ main_axis_alignment: MainAxisAlignment = MainAxisAlignment.START,
1305
+ cross_axis_alignment: CrossAxisAlignment = CrossAxisAlignment.CENTER
1306
+ ) -> None:
1307
+ super().__init__(children, main_axis_alignment, cross_axis_alignment)
1308
+
1309
+
1310
+ # ============================================================
1311
+ # STACK
1312
+ # ============================================================
1313
+
1314
+ class Stack(StatelessWidget):
1315
+
1316
+ def __init__(self, children: list[FlutterWidget]) -> None:
1317
+ super().__init__()
1318
+ self.children = children
1319
+
1320
+ def build(self, context: BuildContext) -> FlutterWidget:
1321
+ return self
1322
+
1323
+ def create_element(self) -> Element:
1324
+ return StackElement(self)
1325
+
1326
+
1327
+ class StackElement(Element[ttk.Frame]):
1328
+
1329
+ def __init__(self, widget: Stack) -> None:
1330
+ super().__init__(widget)
1331
+
1332
+ def get_widget_flutter(self) -> Stack:
1333
+ return self.widget_flutter
1334
+
1335
+ def mount(self, context: BuildContext) -> ttk.Frame:
1336
+ super().mount(context)
1337
+ frame = ttk.Frame(context.parent_tk_widget)
1338
+ frame.pack(fill="both", expand=True)
1339
+
1340
+ for child in self.get_widget_flutter().children:
1341
+ wrapper = ttk.Frame(frame)
1342
+ wrapper.place(
1343
+ relx=0,
1344
+ rely=0,
1345
+ relwidth=1,
1346
+ relheight=1
1347
+ )
1348
+
1349
+ inflate_widget(child, context).mount(
1350
+ context.create_child_context(wrapper)
1351
+ )
1352
+
1353
+ self.widget_tk = frame
1354
+ return frame
1355
+
1356
+
1357
+ # ============================================================
1358
+ # POSITIONED
1359
+ # ============================================================
1360
+ class Positioned(StatelessWidget):
1361
+
1362
+ def __init__(self, child: FlutterWidget, x: int = 0, y: int = 0) -> None:
1363
+ super().__init__()
1364
+
1365
+ self.child = child
1366
+ self.x = x
1367
+ self.y = y
1368
+
1369
+ def build(self, context: BuildContext) -> FlutterWidget:
1370
+ return self.child
1371
+
1372
+ def create_element(self) -> Element:
1373
+ return inflate_widget(self.child, self.context)
1374
+
1375
+
1376
+ # ============================================================
1377
+ # VISIBILITY
1378
+ # ============================================================
1379
+ class Visibility(StatelessWidget):
1380
+
1381
+ def __init__(self, visible: bool, child: FlutterWidget) -> None:
1382
+ super().__init__()
1383
+ self.visible = visible
1384
+ self.child = child
1385
+
1386
+ def build(self, context: BuildContext) -> FlutterWidget:
1387
+ if self.visible:
1388
+ return self.child
1389
+ return SizedBox()
1390
+
1391
+ def create_element(self) -> Element:
1392
+ return self.build(None).create_element()
1393
+
1394
+
1395
+ # ============================================================
1396
+ # FUTURE BUILDER
1397
+ # ============================================================
1398
+
1399
+ class FutureBuilder(StatelessWidget):
1400
+
1401
+ def __init__(
1402
+ self,
1403
+ future: Callable[[], object],
1404
+ builder: Callable[[BuildContext, object], FlutterWidget]
1405
+ ) -> None:
1406
+ super().__init__()
1407
+
1408
+ self.future = future
1409
+ self.builder = builder
1410
+
1411
+ def build(self, context: BuildContext) -> FlutterWidget:
1412
+ result = self.future()
1413
+ return self.builder(context, result)
1414
+
1415
+ def create_element(self) -> Element:
1416
+ return self.build(None).create_element()
1417
+
1418
+