Skip to content

Commit

Permalink
TextField.text_vertical_align property (#2496)
Browse files Browse the repository at this point in the history
* enum: VerticalAlignment

* prop: text_vertical_align

* enum: TextDirection

* prop: text_direction

* prop: text_vertical_align

* remove revealPassword

* reformat textVerticalAlign

* rename text_direction to rtl
  • Loading branch information
ndonkoHenri committed Jan 29, 2024
1 parent adb15f7 commit 829b085
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
10 changes: 8 additions & 2 deletions package/lib/src/controls/cupertino_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class CupertinoTextFieldControl extends StatefulWidget {
class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>
with FletControlStatefulMixin {
String _value = "";
final bool _revealPassword = false;
bool _focused = false;
late TextEditingController _controller;
late final FocusNode _focusNode;
Expand Down Expand Up @@ -172,6 +171,9 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>
orElse: () => TextAlign.start,
);

double? textVerticalAlign = widget.control.attrDouble("textVerticalAlign");

bool rtl = widget.control.attrBool("rtl", false)!;
bool autocorrect = widget.control.attrBool("autocorrect", true)!;
bool enableSuggestions =
widget.control.attrBool("enableSuggestions", true)!;
Expand All @@ -198,6 +200,9 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>

Widget textField = CupertinoTextField(
style: textStyle,
textAlignVertical: textVerticalAlign != null
? TextAlignVertical(y: textVerticalAlign)
: null,
placeholder: widget.control.attrString("placeholderText"),
placeholderStyle: parseTextStyle(
Theme.of(context), widget.control, "placeholderStyle"),
Expand Down Expand Up @@ -246,8 +251,9 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl>
? createControl(widget.control, suffixControls.first.id, disabled)
: null,
readOnly: readOnly,
textDirection: rtl ? TextDirection.rtl : null,
inputFormatters: inputFormatters.isNotEmpty ? inputFormatters : null,
obscureText: password && !_revealPassword,
obscureText: password,
controller: _controller,
focusNode: focusNode,
onChanged: (String value) {
Expand Down
8 changes: 8 additions & 0 deletions package/lib/src/controls/textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ class _TextFieldControlState extends State<TextFieldControl>
orElse: () => TextAlign.start,
);

double? textVerticalAlign =
widget.control.attrDouble("textVerticalAlign");

bool rtl = widget.control.attrBool("rtl", false)!;
bool autocorrect = widget.control.attrBool("autocorrect", true)!;
bool enableSuggestions =
widget.control.attrBool("enableSuggestions", true)!;
Expand Down Expand Up @@ -231,6 +235,10 @@ class _TextFieldControlState extends State<TextFieldControl>
revealPasswordIcon,
_focused),
showCursor: widget.control.attrBool("showCursor"),
textAlignVertical: textVerticalAlign != null
? TextAlignVertical(y: textVerticalAlign)
: null,
textDirection: rtl ? TextDirection.rtl : null,
cursorHeight: widget.control.attrDouble("cursorHeight"),
cursorWidth: widget.control.attrDouble("cursorWidth") ?? 2.0,
cursorRadius: parseRadius(widget.control, "cursorRadius"),
Expand Down
1 change: 1 addition & 0 deletions sdk/python/packages/flet-core/src/flet_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
TabAlignment,
TextAlign,
ThemeMode,
VerticalAlignment,
)
from flet_core.user_control import UserControl
from flet_core.vertical_divider import VerticalDivider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
#
value: Optional[str] = None,
keyboard_type: Optional[KeyboardType] = None,
rtl: Optional[bool] = None,
multiline: Optional[bool] = None,
min_lines: Optional[int] = None,
max_lines: Optional[int] = None,
Expand Down Expand Up @@ -211,6 +212,7 @@ def __init__(
#
value=value,
keyboard_type=keyboard_type,
rtl=rtl,
multiline=multiline,
min_lines=min_lines,
max_lines=max_lines,
Expand Down
15 changes: 15 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/form_field_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ResponsiveNumber,
RotateValue,
ScaleValue,
VerticalAlignment,
)

try:
Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(
#
text_size: OptionalNumber = None,
text_style: Optional[TextStyle] = None,
text_vertical_align: Union[VerticalAlignment, OptionalNumber] = None,
label: Optional[str] = None,
label_style: Optional[TextStyle] = None,
icon: Optional[str] = None,
Expand Down Expand Up @@ -128,6 +130,7 @@ def __init__(

self.text_size = text_size
self.text_style = text_style
self.text_vertical_align = text_vertical_align
self.label = label
self.label_style = label_style
self.icon = icon
Expand Down Expand Up @@ -290,6 +293,18 @@ def border_color(self):
def border_color(self, value):
self._set_attr("borderColor", value)

# text_vertical_align
@property
def text_vertical_align(self) -> Union[VerticalAlignment, OptionalNumber]:
return self._get_attr("textVerticalAlign")

@text_vertical_align.setter
def text_vertical_align(self, value: Union[VerticalAlignment, OptionalNumber]):
v = value.value if isinstance(value, VerticalAlignment) else value
if v is not None:
v = max(-1.0, min(v, 1.0)) # make sure 0.0 <= value <= 1.0
self._set_attr("textVerticalAlign", v)

# focused_color
@property
def focused_color(self):
Expand Down
24 changes: 16 additions & 8 deletions sdk/python/packages/flet-core/src/flet_core/textfield.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import dataclasses
import time
from dataclasses import field
from enum import Enum
from typing import Any, Optional, Union

import time

from flet_core.control import Control, OptionalNumber
from flet_core.form_field_control import FormFieldControl, InputBorder
from flet_core.ref import Ref
Expand All @@ -17,7 +18,7 @@
RotateValue,
ScaleValue,
TextAlign,
TextAlignString,
VerticalAlignment,
)

try:
Expand Down Expand Up @@ -141,6 +142,7 @@ def __init__(
#
text_size: OptionalNumber = None,
text_style: Optional[TextStyle] = None,
text_vertical_align: Union[VerticalAlignment, OptionalNumber] = None,
label: Optional[str] = None,
label_style: Optional[TextStyle] = None,
icon: Optional[str] = None,
Expand Down Expand Up @@ -179,6 +181,7 @@ def __init__(
adaptive: Optional[bool] = None,
value: Optional[str] = None,
keyboard_type: Optional[KeyboardType] = None,
rtl: Optional[bool] = None,
multiline: Optional[bool] = None,
min_lines: Optional[int] = None,
max_lines: Optional[int] = None,
Expand Down Expand Up @@ -235,6 +238,7 @@ def __init__(
#
text_size=text_size,
text_style=text_style,
text_vertical_align=text_vertical_align,
label=label,
label_style=label_style,
icon=icon,
Expand Down Expand Up @@ -272,6 +276,7 @@ def __init__(
self.text_style = text_style
self.keyboard_type = keyboard_type
self.text_align = text_align
self.rtl = rtl
self.multiline = multiline
self.min_lines = min_lines
self.max_lines = max_lines
Expand Down Expand Up @@ -349,13 +354,16 @@ def text_align(self) -> TextAlign:
@text_align.setter
def text_align(self, value: TextAlign):
self.__text_align = value
if isinstance(value, TextAlign):
self._set_attr("textAlign", value.value)
else:
self.__set_text_align(value)
self._set_attr("textAlign", value.value if isinstance(value, TextAlign) else value)

# rtl
@property
def rtl(self) -> Optional[bool]:
return self._get_attr("rtl", data_type="bool", def_value=False)

def __set_text_align(self, value: TextAlignString):
self._set_attr("textAlign", value)
@rtl.setter
def rtl(self, value: Optional[bool]):
self._set_attr("rtl", value)

# multiline
@property
Expand Down
7 changes: 7 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ class CrossAxisAlignment(Enum):
BASELINE = "baseline"


class VerticalAlignment(Enum):
NONE = None
START = -1.0
END = 1.0
CENTER = 0.0


class TabAlignment(Enum):
NONE = None
START = "start"
Expand Down

0 comments on commit 829b085

Please sign in to comment.