Skip to content

Commit 829b085

Browse files
authored
TextField.text_vertical_align property (#2496)
* enum: VerticalAlignment * prop: text_vertical_align * enum: TextDirection * prop: text_direction * prop: text_vertical_align * remove revealPassword * reformat textVerticalAlign * rename text_direction to rtl
1 parent adb15f7 commit 829b085

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

package/lib/src/controls/cupertino_textfield.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class CupertinoTextFieldControl extends StatefulWidget {
3636
class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>
3737
with FletControlStatefulMixin {
3838
String _value = "";
39-
final bool _revealPassword = false;
4039
bool _focused = false;
4140
late TextEditingController _controller;
4241
late final FocusNode _focusNode;
@@ -172,6 +171,9 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>
172171
orElse: () => TextAlign.start,
173172
);
174173

174+
double? textVerticalAlign = widget.control.attrDouble("textVerticalAlign");
175+
176+
bool rtl = widget.control.attrBool("rtl", false)!;
175177
bool autocorrect = widget.control.attrBool("autocorrect", true)!;
176178
bool enableSuggestions =
177179
widget.control.attrBool("enableSuggestions", true)!;
@@ -198,6 +200,9 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>
198200

199201
Widget textField = CupertinoTextField(
200202
style: textStyle,
203+
textAlignVertical: textVerticalAlign != null
204+
? TextAlignVertical(y: textVerticalAlign)
205+
: null,
201206
placeholder: widget.control.attrString("placeholderText"),
202207
placeholderStyle: parseTextStyle(
203208
Theme.of(context), widget.control, "placeholderStyle"),
@@ -246,8 +251,9 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>
246251
? createControl(widget.control, suffixControls.first.id, disabled)
247252
: null,
248253
readOnly: readOnly,
254+
textDirection: rtl ? TextDirection.rtl : null,
249255
inputFormatters: inputFormatters.isNotEmpty ? inputFormatters : null,
250-
obscureText: password && !_revealPassword,
256+
obscureText: password,
251257
controller: _controller,
252258
focusNode: focusNode,
253259
onChanged: (String value) {

package/lib/src/controls/textfield.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ class _TextFieldControlState extends State<TextFieldControl>
200200
orElse: () => TextAlign.start,
201201
);
202202

203+
double? textVerticalAlign =
204+
widget.control.attrDouble("textVerticalAlign");
205+
206+
bool rtl = widget.control.attrBool("rtl", false)!;
203207
bool autocorrect = widget.control.attrBool("autocorrect", true)!;
204208
bool enableSuggestions =
205209
widget.control.attrBool("enableSuggestions", true)!;
@@ -231,6 +235,10 @@ class _TextFieldControlState extends State<TextFieldControl>
231235
revealPasswordIcon,
232236
_focused),
233237
showCursor: widget.control.attrBool("showCursor"),
238+
textAlignVertical: textVerticalAlign != null
239+
? TextAlignVertical(y: textVerticalAlign)
240+
: null,
241+
textDirection: rtl ? TextDirection.rtl : null,
234242
cursorHeight: widget.control.attrDouble("cursorHeight"),
235243
cursorWidth: widget.control.attrDouble("cursorWidth") ?? 2.0,
236244
cursorRadius: parseRadius(widget.control, "cursorRadius"),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
TabAlignment,
242242
TextAlign,
243243
ThemeMode,
244+
VerticalAlignment,
244245
)
245246
from flet_core.user_control import UserControl
246247
from flet_core.vertical_divider import VerticalDivider

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(
6464
#
6565
value: Optional[str] = None,
6666
keyboard_type: Optional[KeyboardType] = None,
67+
rtl: Optional[bool] = None,
6768
multiline: Optional[bool] = None,
6869
min_lines: Optional[int] = None,
6970
max_lines: Optional[int] = None,
@@ -211,6 +212,7 @@ def __init__(
211212
#
212213
value=value,
213214
keyboard_type=keyboard_type,
215+
rtl=rtl,
214216
multiline=multiline,
215217
min_lines=min_lines,
216218
max_lines=max_lines,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ResponsiveNumber,
1414
RotateValue,
1515
ScaleValue,
16+
VerticalAlignment,
1617
)
1718

1819
try:
@@ -63,6 +64,7 @@ def __init__(
6364
#
6465
text_size: OptionalNumber = None,
6566
text_style: Optional[TextStyle] = None,
67+
text_vertical_align: Union[VerticalAlignment, OptionalNumber] = None,
6668
label: Optional[str] = None,
6769
label_style: Optional[TextStyle] = None,
6870
icon: Optional[str] = None,
@@ -128,6 +130,7 @@ def __init__(
128130

129131
self.text_size = text_size
130132
self.text_style = text_style
133+
self.text_vertical_align = text_vertical_align
131134
self.label = label
132135
self.label_style = label_style
133136
self.icon = icon
@@ -290,6 +293,18 @@ def border_color(self):
290293
def border_color(self, value):
291294
self._set_attr("borderColor", value)
292295

296+
# text_vertical_align
297+
@property
298+
def text_vertical_align(self) -> Union[VerticalAlignment, OptionalNumber]:
299+
return self._get_attr("textVerticalAlign")
300+
301+
@text_vertical_align.setter
302+
def text_vertical_align(self, value: Union[VerticalAlignment, OptionalNumber]):
303+
v = value.value if isinstance(value, VerticalAlignment) else value
304+
if v is not None:
305+
v = max(-1.0, min(v, 1.0)) # make sure 0.0 <= value <= 1.0
306+
self._set_attr("textVerticalAlign", v)
307+
293308
# focused_color
294309
@property
295310
def focused_color(self):

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import dataclasses
2-
import time
32
from dataclasses import field
43
from enum import Enum
54
from typing import Any, Optional, Union
65

6+
import time
7+
78
from flet_core.control import Control, OptionalNumber
89
from flet_core.form_field_control import FormFieldControl, InputBorder
910
from flet_core.ref import Ref
@@ -17,7 +18,7 @@
1718
RotateValue,
1819
ScaleValue,
1920
TextAlign,
20-
TextAlignString,
21+
VerticalAlignment,
2122
)
2223

2324
try:
@@ -141,6 +142,7 @@ def __init__(
141142
#
142143
text_size: OptionalNumber = None,
143144
text_style: Optional[TextStyle] = None,
145+
text_vertical_align: Union[VerticalAlignment, OptionalNumber] = None,
144146
label: Optional[str] = None,
145147
label_style: Optional[TextStyle] = None,
146148
icon: Optional[str] = None,
@@ -179,6 +181,7 @@ def __init__(
179181
adaptive: Optional[bool] = None,
180182
value: Optional[str] = None,
181183
keyboard_type: Optional[KeyboardType] = None,
184+
rtl: Optional[bool] = None,
182185
multiline: Optional[bool] = None,
183186
min_lines: Optional[int] = None,
184187
max_lines: Optional[int] = None,
@@ -235,6 +238,7 @@ def __init__(
235238
#
236239
text_size=text_size,
237240
text_style=text_style,
241+
text_vertical_align=text_vertical_align,
238242
label=label,
239243
label_style=label_style,
240244
icon=icon,
@@ -272,6 +276,7 @@ def __init__(
272276
self.text_style = text_style
273277
self.keyboard_type = keyboard_type
274278
self.text_align = text_align
279+
self.rtl = rtl
275280
self.multiline = multiline
276281
self.min_lines = min_lines
277282
self.max_lines = max_lines
@@ -349,13 +354,16 @@ def text_align(self) -> TextAlign:
349354
@text_align.setter
350355
def text_align(self, value: TextAlign):
351356
self.__text_align = value
352-
if isinstance(value, TextAlign):
353-
self._set_attr("textAlign", value.value)
354-
else:
355-
self.__set_text_align(value)
357+
self._set_attr("textAlign", value.value if isinstance(value, TextAlign) else value)
358+
359+
# rtl
360+
@property
361+
def rtl(self) -> Optional[bool]:
362+
return self._get_attr("rtl", data_type="bool", def_value=False)
356363

357-
def __set_text_align(self, value: TextAlignString):
358-
self._set_attr("textAlign", value)
364+
@rtl.setter
365+
def rtl(self, value: Optional[bool]):
366+
self._set_attr("rtl", value)
359367

360368
# multiline
361369
@property

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ class CrossAxisAlignment(Enum):
139139
BASELINE = "baseline"
140140

141141

142+
class VerticalAlignment(Enum):
143+
NONE = None
144+
START = -1.0
145+
END = 1.0
146+
CENTER = 0.0
147+
148+
142149
class TabAlignment(Enum):
143150
NONE = None
144151
START = "start"

0 commit comments

Comments
 (0)