Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TextStyle for the Text control #2270

Merged
merged 7 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions package/lib/src/controls/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class TextControl extends StatelessWidget {
final Control control;
final bool parentDisabled;

const TextControl(
{super.key,
required this.parent,
required this.control,
required this.parentDisabled});
const TextControl({
super.key,
required this.parent,
required this.control,
required this.parentDisabled,
});

@override
Widget build(BuildContext context) {
Expand All @@ -34,16 +35,39 @@ class TextControl extends StatelessWidget {
bool disabled = control.isDisabled || parentDisabled;

String text = control.attrString("value", "")!;
List<InlineSpan>? spans = parseTextSpans(Theme.of(context), viewModel,
disabled, FletAppServices.of(context).server);
List<InlineSpan>? spans = parseTextSpans(
Theme.of(context),
viewModel,
disabled,
FletAppServices.of(context).server,
);
String? semanticsLabel = control.attrString("semanticsLabel");
bool noWrap = control.attrBool("noWrap", false)!;
int? maxLines = control.attrInt("maxLines");

TextStyle? style;
var styleName = control.attrString("style", null);
var styleNameOrData = control.attrString("style", null);
if (styleNameOrData != null) {
style = getTextStyle(context, styleNameOrData);
}
if (style == null && styleNameOrData != null) {
try {
style = parseTextStyle(Theme.of(context), control, "style");
} on FormatException catch (_) {
style = null;
}
}

TextStyle? themeStyle;
var styleName = control.attrString("theme_style", null);
if (styleName != null) {
style = getTextStyle(context, styleName);
themeStyle = getTextStyle(context, styleName);
}

if (style == null && themeStyle != null) {
style = themeStyle;
} else if (style != null && themeStyle != null) {
style = themeStyle.merge(style);
}

var fontWeight = control.attrString("weight", "")!;
Expand All @@ -57,8 +81,12 @@ class TextControl extends StatelessWidget {
style = (style ?? const TextStyle()).copyWith(
fontSize: control.attrDouble("size", null),
fontWeight: getFontWeight(fontWeight),
fontStyle:
control.attrBool("italic", false)! ? FontStyle.italic : null,
fontStyle: control.attrBool(
"italic",
false,
)!
? FontStyle.italic
: null,
fontFamily: control.attrString("fontFamily"),
fontVariations: variations,
color: HexColor.fromString(
Expand Down
6 changes: 6 additions & 0 deletions package/lib/src/utils/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ TextStyle textStyleFromJson(ThemeData theme, Map<String, dynamic> json) {
decorations.add(TextDecoration.lineThrough);
}

var height = json["height"];
if (height != null && !(height is double)) {
height = double.tryParse(height.toString());
}

FeodorFitsner marked this conversation as resolved.
Show resolved Hide resolved
return TextStyle(
fontSize: json["size"] != null ? parseDouble(json["size"]) : null,
fontWeight: fontWeight != null ? getFontWeight(fontWeight) : null,
Expand All @@ -181,6 +186,7 @@ TextStyle textStyleFromJson(ThemeData theme, Map<String, dynamic> json) {
: null,
fontFamily: json["font_family"],
fontVariations: variations,
height: height,
decoration:
decorations.isNotEmpty ? TextDecoration.combine(decorations) : null,
decorationStyle: json["decoration_style"] != null
Expand Down
33 changes: 29 additions & 4 deletions sdk/python/packages/flet-core/src/flet_core/text.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import dataclasses
from enum import Enum
from typing import Any, List, Optional, Union

from flet_core.constrained_control import ConstrainedControl
from flet_core.control import OptionalNumber
from flet_core.ref import Ref
from flet_core.text_span import TextSpan
from flet_core.text_style import TextStyle
from flet_core.types import (
AnimationValue,
FontWeight,
Expand Down Expand Up @@ -137,7 +139,8 @@ def __init__(
size: OptionalNumber = None,
weight: Optional[FontWeight] = None,
italic: Optional[bool] = None,
style: Optional[TextThemeStyle] = None,
style: Optional[Union[TextThemeStyle, TextStyle]] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea! I'm not a big fan of mixing types though, but let's users decide! :)

Copy link
Contributor

@ndonkoHenri ndonkoHenri Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should separate props. Perhaps create text_style and theme_style? - such that one can use both. Or it should not be permitted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since @ndonkoHenri noticed this 🫣 I'm not going to try convincing him that "it's OK like that" - let's have two separate properties 😏

Now, I'd love to have style property be of TextStyle to look the same as TextSpan.style, and then theme_style for named styles.

However, we don't want to break a lot of existing applications. I'm thinking what if we "temporary" make style property accepting both TextThemeStyle and TextStyle (like in this PR) and also add theme_style which accepts only TextThemeStyle. Will update docs for .style property to allow only TextStyle. What do you guys think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@50Bytes-dev would you implement that (along with previous note) please?

theme_style: Optional[TextThemeStyle] = None,
max_lines: Optional[int] = None,
overflow: TextOverflow = TextOverflow.NONE,
selectable: Optional[bool] = None,
Expand Down Expand Up @@ -185,6 +188,7 @@ def __init__(
self.italic = italic
self.no_wrap = no_wrap
self.style = style
self.theme_style = theme_style
self.max_lines = max_lines
self.overflow = overflow
self.selectable = selectable
Expand All @@ -199,6 +203,11 @@ def _get_children(self):
children = []
children.extend(self.__spans)
return children

def _before_build_command(self):
super()._before_build_command()
if dataclasses.is_dataclass(self.__style):
self._set_attr_json("style", self.__style)

# value
@property
Expand Down Expand Up @@ -270,20 +279,36 @@ def __set_weight(self, value: FontWeightString):

# style
@property
def style(self) -> Optional[TextThemeStyle]:
def style(self) -> Optional[Union[TextThemeStyle, TextStyle, TextThemeStyleString]]:
return self.__style

@style.setter
def style(self, value: Optional[TextThemeStyle]):
def style(self, value: Optional[Union[TextThemeStyle, TextStyle, TextThemeStyleString]]):
self.__style = value
if isinstance(value, TextThemeStyle):
self._set_attr("style", value.value)
else:
self.__set_style(value)

def __set_style(self, value: Optional[TextThemeStyleString]):
def __set_style(self, value: Optional[Union[TextStyle, TextThemeStyleString]]):
self._set_attr("style", value)

# theme_style
@property
def theme_style(self) -> Optional[Union[TextThemeStyle, TextThemeStyleString]]:
return self.__theme_style

@theme_style.setter
def theme_style(self, value: Optional[Union[TextThemeStyle, TextThemeStyleString]]):
self.__theme_style = value
if isinstance(value, TextThemeStyle):
self._set_attr("theme_style", value.value)
else:
self.__set_theme_style(value)

def __set_theme_style(self, value: Optional[TextThemeStyleString]):
self._set_attr("theme_style", value)

# italic
@property
def italic(self) -> Optional[bool]:
Expand Down
1 change: 1 addition & 0 deletions sdk/python/packages/flet-core/src/flet_core/text_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TextDecorationStyle(Enum):
@dataclasses.dataclass
class TextStyle:
size: Union[None, int, float] = field(default=None)
height: Union[None, int, float] = field(default=None)
weight: Optional[FontWeight] = field(default=None)
italic: Optional[bool] = field(default=None)
decoration: Optional[TextDecoration] = field(default=None)
Expand Down