Skip to content

Commit 43f6cf4

Browse files
authored
TextField: suffix_icon, prefix_icon and icon can be Control or str (#4173)
* suffix_icon is a Control * suffix_icon Control or str in python * suffixIcon works as str and Control * Update form_field_control.py prefix_icon of IconValue type in python * prefix_icon can be str or Control * icon field Control or str in Python * icon of type Control or str in dart * cleanup * Dropdown: prefix_icon, suffix_icon and icon can be a string or a Control * renamed IconValue to IconValueOrControl and made it not optional
1 parent a217d5e commit 43f6cf4

File tree

6 files changed

+79
-30
lines changed

6 files changed

+79
-30
lines changed

packages/flet/lib/src/controls/dropdown.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,14 @@ class _DropdownControlState extends State<DropdownControl> with FletStoreMixin {
147147

148148
var prefixControls = itemsView.controlViews
149149
.where((c) => c.control.name == "prefix" && c.control.isVisible);
150+
var prefixIconControls =
151+
widget.children.where((c) => c.name == "prefixIcon" && c.isVisible);
150152
var suffixControls = itemsView.controlViews
151153
.where((c) => c.control.name == "suffix" && c.control.isVisible);
154+
var suffixIconControls =
155+
widget.children.where((c) => c.name == "suffixIcon" && c.isVisible);
156+
var iconControls =
157+
widget.children.where((c) => c.name == "icon" && c.isVisible);
152158
var counterControls = itemsView.controlViews
153159
.where((c) => c.control.name == "counter" && c.control.isVisible);
154160

@@ -189,8 +195,11 @@ class _DropdownControlState extends State<DropdownControl> with FletStoreMixin {
189195
decoration: buildInputDecoration(context, widget.control,
190196
prefix:
191197
prefixControls.isNotEmpty ? prefixControls.first.control : null,
198+
prefixIcon: prefixIconControls.isNotEmpty ? prefixIconControls.first : null,
192199
suffix:
193200
suffixControls.isNotEmpty ? suffixControls.first.control : null,
201+
suffixIcon: suffixIconControls.isNotEmpty ? suffixIconControls.first : null,
202+
icon: iconControls.isNotEmpty ? iconControls.first : null,
194203
counter: counterControls.isNotEmpty
195204
? counterControls.first.control
196205
: null,

packages/flet/lib/src/controls/textfield.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,14 @@ class _TextFieldControlState extends State<TextFieldControl>
123123

124124
var prefixControls =
125125
widget.children.where((c) => c.name == "prefix" && c.isVisible);
126+
var prefixIconControls =
127+
widget.children.where((c) => c.name == "prefixIcon" && c.isVisible);
126128
var suffixControls =
127129
widget.children.where((c) => c.name == "suffix" && c.isVisible);
130+
var suffixIconControls =
131+
widget.children.where((c) => c.name == "suffixIcon" && c.isVisible);
132+
var iconControls =
133+
widget.children.where((c) => c.name == "icon" && c.isVisible);
128134
var counterControls =
129135
widget.children.where((c) => c.name == "counter" && c.isVisible);
130136

@@ -224,7 +230,10 @@ class _TextFieldControlState extends State<TextFieldControl>
224230
: null,
225231
decoration: buildInputDecoration(context, widget.control,
226232
prefix: prefixControls.isNotEmpty ? prefixControls.first : null,
233+
prefixIcon: prefixIconControls.isNotEmpty ? prefixIconControls.first : null,
227234
suffix: suffixControls.isNotEmpty ? suffixControls.first : null,
235+
suffixIcon: suffixIconControls.isNotEmpty ? suffixIconControls.first : null,
236+
icon: iconControls.isNotEmpty ? iconControls.first : null,
228237
counter:
229238
counterControls.isNotEmpty ? counterControls.first : null,
230239
customSuffix: revealPasswordIcon,

packages/flet/lib/src/utils/form_field.dart

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ InputDecoration buildInputDecoration(
5454
BuildContext context,
5555
Control control,
5656
{Control? prefix,
57+
Control? prefixIcon,
5758
Control? suffix,
59+
Control? suffixIcon,
60+
Control? icon,
5861
Control? counter,
5962
Widget? customSuffix,
6063
bool focused = false,
@@ -65,11 +68,11 @@ InputDecoration buildInputDecoration(
6568
control.attrString("border"),
6669
FormFieldInputBorder.outline,
6770
)!;
68-
var icon = parseIcon(control.attrString("icon"));
71+
var iconStr = parseIcon(control.attrString("icon"));
6972

70-
var prefixIcon = parseIcon(control.attrString("prefixIcon"));
73+
var prefixIconStr = parseIcon(control.attrString("prefixIcon"));
7174
var prefixText = control.attrString("prefixText");
72-
var suffixIcon = parseIcon(control.attrString("suffixIcon"));
75+
var suffixIconStr = parseIcon(control.attrString("suffixIcon"));
7376
var suffixText = control.attrString("suffixText");
7477

7578
var bgcolor = control.attrColor("bgcolor", context);
@@ -133,7 +136,10 @@ InputDecoration buildInputDecoration(
133136
enabledBorder: border,
134137
focusedBorder: focusedBorder,
135138
hoverColor: hoverColor,
136-
icon: icon != null ? Icon(icon) : null,
139+
icon: icon != null
140+
? createControl(control, icon.id, control.isDisabled,
141+
parentAdaptive: adaptive)
142+
: iconStr !=null? Icon(iconStr): null,
137143
filled: control.attrBool("filled", false)!,
138144
fillColor: fillColor ?? (focused ? focusedBgcolor ?? bgcolor : bgcolor),
139145
hintText: control.attrString("hintText"),
@@ -150,7 +156,10 @@ InputDecoration buildInputDecoration(
150156
? control.attrString("errorText")
151157
: null,
152158
errorStyle: parseTextStyle(Theme.of(context), control, "errorStyle"),
153-
prefixIcon: prefixIcon != null ? Icon(prefixIcon) : null,
159+
prefixIcon: prefixIcon != null
160+
? createControl(control, prefixIcon.id, control.isDisabled,
161+
parentAdaptive: adaptive)
162+
: prefixIconStr !=null? Icon(prefixIconStr): null,
154163
prefixText: prefixText,
155164
prefixStyle: parseTextStyle(Theme.of(context), control, "prefixStyle"),
156165
prefix: prefix != null
@@ -161,9 +170,13 @@ InputDecoration buildInputDecoration(
161170
? createControl(control, suffix.id, control.isDisabled,
162171
parentAdaptive: adaptive)
163172
: null,
164-
suffixIcon: suffixIcon != null ? Icon(suffixIcon) : customSuffix,
173+
suffixIcon: suffixIcon != null
174+
? createControl(control, suffixIcon.id, control.isDisabled,
175+
parentAdaptive: adaptive)
176+
: suffixIconStr !=null? Icon(suffixIconStr): customSuffix,
165177
suffixText: suffixText,
166-
suffixStyle: parseTextStyle(Theme.of(context), control, "suffixStyle"));
178+
suffixStyle: parseTextStyle(Theme.of(context), control, "suffixStyle"),
179+
);
167180
}
168181

169182
OverlayVisibilityMode parseVisibilityMode(String type) {

sdk/python/packages/flet-core/src/flet_core/dropdown.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
OptionalControlEventCallable,
1919
)
2020
from flet_core.utils import deprecated
21-
21+
from flet_core.form_field_control import IconValueOrControl
2222

2323
class Option(Control):
2424
def __init__(
@@ -181,7 +181,7 @@ def __init__(
181181
text_style: Optional[TextStyle] = None,
182182
label: Optional[str] = None,
183183
label_style: Optional[TextStyle] = None,
184-
icon: Optional[str] = None,
184+
icon: Optional[IconValueOrControl] = None,
185185
border: Optional[InputBorder] = None,
186186
color: Optional[str] = None,
187187
bgcolor: Optional[str] = None,
@@ -206,11 +206,11 @@ def __init__(
206206
error_text: Optional[str] = None,
207207
error_style: Optional[TextStyle] = None,
208208
prefix: Optional[Control] = None,
209-
prefix_icon: Optional[str] = None,
209+
prefix_icon: Optional[IconValueOrControl] = None,
210210
prefix_text: Optional[str] = None,
211211
prefix_style: Optional[TextStyle] = None,
212212
suffix: Optional[Control] = None,
213-
suffix_icon: Optional[str] = None,
213+
suffix_icon: Optional[IconValueOrControl] = None,
214214
suffix_text: Optional[str] = None,
215215
suffix_style: Optional[TextStyle] = None,
216216
#

sdk/python/packages/flet-core/src/flet_core/form_field_control.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from flet_core.constrained_control import ConstrainedControl
55
from flet_core.control import Control, OptionalNumber
6+
from flet_core.icon import Icon
67
from flet_core.ref import Ref
78
from flet_core.text_style import TextStyle
89
from flet_core.tooltip import TooltipValue
@@ -23,6 +24,7 @@
2324
except ImportError:
2425
from typing_extensions import Literal
2526

27+
IconValueOrControl = Union[str, Control]
2628

2729
class InputBorder(Enum):
2830
NONE = "none"
@@ -38,7 +40,7 @@ def __init__(
3840
text_vertical_align: Union[VerticalAlignment, OptionalNumber] = None,
3941
label: Optional[str] = None,
4042
label_style: Optional[TextStyle] = None,
41-
icon: Optional[str] = None,
43+
icon: Optional[IconValueOrControl] = None,
4244
border: Optional[InputBorder] = None,
4345
color: Optional[str] = None,
4446
bgcolor: Optional[str] = None,
@@ -64,11 +66,11 @@ def __init__(
6466
error_text: Optional[str] = None,
6567
error_style: Optional[TextStyle] = None,
6668
prefix: Optional[Control] = None,
67-
prefix_icon: Optional[str] = None,
69+
prefix_icon: Optional[IconValueOrControl] = None,
6870
prefix_text: Optional[str] = None,
6971
prefix_style: Optional[TextStyle] = None,
7072
suffix: Optional[Control] = None,
71-
suffix_icon: Optional[str] = None,
73+
suffix_icon: Optional[IconValueOrControl] = None,
7274
suffix_text: Optional[str] = None,
7375
suffix_style: Optional[TextStyle] = None,
7476
rtl: Optional[bool] = None,
@@ -186,6 +188,12 @@ def before_update(self):
186188
self._set_attr_json("errorStyle", self.__error_style)
187189
self._set_attr_json("prefixStyle", self.__prefix_style)
188190
self._set_attr_json("suffixStyle", self.__suffix_style)
191+
if isinstance(self.__suffix_icon, str):
192+
self._set_attr("suffixIcon", self.__suffix_icon)
193+
if isinstance(self.__prefix_icon, str):
194+
self._set_attr("prefixIcon", self.__prefix_icon)
195+
if isinstance(self.__icon, str):
196+
self._set_attr("icon", self.__icon)
189197

190198
def _get_children(self):
191199
children = []
@@ -195,6 +203,15 @@ def _get_children(self):
195203
if isinstance(self.__suffix, Control):
196204
self.__suffix._set_attr_internal("n", "suffix")
197205
children.append(self.__suffix)
206+
if isinstance(self.__suffix_icon, Control):
207+
self.__suffix_icon._set_attr_internal("n", "suffixIcon")
208+
children.append(self.__suffix_icon)
209+
if isinstance(self.__prefix_icon, Control):
210+
self.__prefix_icon._set_attr_internal("n", "prefixIcon")
211+
children.append(self.__prefix_icon)
212+
if isinstance(self.__icon, Control):
213+
self.__icon._set_attr_internal("n", "icon")
214+
children.append(self.__icon)
198215
if isinstance(self.__counter, Control):
199216
self.__counter._set_attr_internal("n", "counter")
200217
children.append(self.__counter)
@@ -238,12 +255,12 @@ def label_style(self, value: Optional[TextStyle]):
238255

239256
# icon
240257
@property
241-
def icon(self) -> Optional[str]:
242-
return self._get_attr("icon")
258+
def icon(self) -> Optional[IconValueOrControl]:
259+
return self.__icon
243260

244261
@icon.setter
245-
def icon(self, value: Optional[str]):
246-
self._set_attr("icon", value)
262+
def icon(self, value: Optional[IconValueOrControl]):
263+
self.__icon = value
247264

248265
# border
249266
@property
@@ -467,12 +484,12 @@ def counter(self, value: Optional[Control]):
467484

468485
# prefix_icon
469486
@property
470-
def prefix_icon(self) -> Optional[str]:
471-
return self._get_attr("prefixIcon")
487+
def prefix_icon(self) -> Optional[IconValueOrControl]:
488+
return self.__prefix_icon
472489

473490
@prefix_icon.setter
474-
def prefix_icon(self, value: Optional[str]):
475-
self._set_attr("prefixIcon", value)
491+
def prefix_icon(self, value: Optional[IconValueOrControl]):
492+
self.__prefix_icon = value
476493

477494
# prefix_text
478495
@property
@@ -500,15 +517,15 @@ def suffix(self) -> Optional[Control]:
500517
@suffix.setter
501518
def suffix(self, value: Optional[Control]):
502519
self.__suffix = value
503-
520+
504521
# suffix_icon
505522
@property
506-
def suffix_icon(self) -> Optional[str]:
507-
return self._get_attr("suffixIcon")
523+
def suffix_icon(self) -> Optional[IconValueOrControl]:
524+
return self.__suffix_icon
508525

509526
@suffix_icon.setter
510-
def suffix_icon(self, value: Optional[str]):
511-
self._set_attr("suffixIcon", value)
527+
def suffix_icon(self, value: Optional[IconValueOrControl]):
528+
self.__suffix_icon = value
512529

513530
# suffix_text
514531
@property

sdk/python/packages/flet-core/src/flet_core/textfield.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
OptionalControlEventCallable,
2424
)
2525
from flet_core.utils import deprecated
26+
from flet_core.form_field_control import IconValueOrControl
2627

2728
try:
2829
from typing import Literal
@@ -140,7 +141,7 @@ def __init__(
140141
text_vertical_align: Union[VerticalAlignment, OptionalNumber] = None,
141142
label: Optional[str] = None,
142143
label_style: Optional[TextStyle] = None,
143-
icon: Optional[str] = None,
144+
icon: Optional[IconValueOrControl] = None,
144145
border: Optional[InputBorder] = None,
145146
color: Optional[str] = None,
146147
bgcolor: Optional[str] = None,
@@ -166,11 +167,11 @@ def __init__(
166167
error_text: Optional[str] = None,
167168
error_style: Optional[TextStyle] = None,
168169
prefix: Optional[Control] = None,
169-
prefix_icon: Optional[str] = None,
170+
prefix_icon: Optional[IconValueOrControl] = None,
170171
prefix_text: Optional[str] = None,
171172
prefix_style: Optional[TextStyle] = None,
172173
suffix: Optional[Control] = None,
173-
suffix_icon: Optional[str] = None,
174+
suffix_icon: Optional[IconValueOrControl] = None,
174175
suffix_text: Optional[str] = None,
175176
suffix_style: Optional[TextStyle] = None,
176177
#

0 commit comments

Comments
 (0)