Skip to content

Commit b9a7351

Browse files
authored
fix #4119: Refactor ElevatedButton.before_update method to simplify style initialization and assignment
1 parent e58b8dd commit b9a7351

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

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

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time
2-
from typing import Any, Optional, Union
2+
from typing import Any, Dict, Optional, Union
33

44
from flet_core.adaptive_control import AdaptiveControl
55
from flet_core.buttons import ButtonStyle
@@ -9,13 +9,14 @@
99
from flet_core.tooltip import TooltipValue
1010
from flet_core.types import (
1111
AnimationValue,
12+
ClipBehavior,
13+
ControlState,
1214
OffsetValue,
15+
OptionalControlEventCallable,
1316
ResponsiveNumber,
1417
RotateValue,
1518
ScaleValue,
16-
ClipBehavior,
1719
UrlTarget,
18-
OptionalControlEventCallable,
1920
)
2021
from flet_core.utils import deprecated
2122

@@ -48,10 +49,10 @@ def __init__(
4849
text: Optional[str] = None,
4950
icon: Optional[str] = None,
5051
icon_color: Optional[str] = None,
51-
color: Optional[str] = None,
52-
bgcolor: Optional[str] = None,
52+
color: Union[None, str, Dict[ControlState, str]] = None,
53+
bgcolor: Union[None, str, Dict[ControlState, str]] = None,
5354
content: Optional[Control] = None,
54-
elevation: OptionalNumber = None,
55+
elevation: Union[OptionalNumber, Dict[ControlState, OptionalNumber]] = None,
5556
style: Optional[ButtonStyle] = None,
5657
autofocus: Optional[bool] = None,
5758
clip_behavior: Optional[ClipBehavior] = None,
@@ -127,10 +128,6 @@ def __init__(
127128

128129
AdaptiveControl.__init__(self, adaptive=adaptive)
129130

130-
self.__color = None
131-
self.__bgcolor = None
132-
self.__elevation = None
133-
134131
self.text = text
135132
self.color = color
136133
self.bgcolor = bgcolor
@@ -157,27 +154,18 @@ def before_update(self):
157154
assert (
158155
self.text or self.icon or (self.__content and self.__content.visible)
159156
), "at minimum, text, icon or a visible content must be provided"
157+
style = ButtonStyle()
160158
if any([self.__color, self.__bgcolor, self.__elevation]):
161-
self.__style = self.__style or ButtonStyle()
162-
if self.__style:
163-
self.__style.color = (
164-
self.__style.color if self.__style.color is not None else self.color
165-
)
166-
self.__style.bgcolor = (
167-
self.__style.bgcolor
168-
if self.__style.bgcolor is not None
169-
else self.bgcolor
170-
)
171-
self.__style.elevation = (
172-
self.__style.elevation
173-
if self.__style.elevation is not None
174-
else self.elevation
175-
)
176-
self.__style.side = self._wrap_attr_dict(self.__style.side)
177-
self.__style.shape = self._wrap_attr_dict(self.__style.shape)
178-
self.__style.padding = self._wrap_attr_dict(self.__style.padding)
179-
self.__style.text_style = self._wrap_attr_dict(self.__style.text_style)
180-
self._set_attr_json("style", self.__style)
159+
style = self.__style or style
160+
if style:
161+
style.color = style.color or self.__color
162+
style.bgcolor = style.bgcolor or self.__bgcolor
163+
style.elevation = style.elevation or self.__elevation
164+
style.side = self._wrap_attr_dict(style.side)
165+
style.shape = self._wrap_attr_dict(style.shape)
166+
style.padding = self._wrap_attr_dict(style.padding)
167+
style.text_style = self._wrap_attr_dict(style.text_style)
168+
self._set_attr_json("style", style)
181169

182170
def _get_children(self):
183171
if self.__content is None:
@@ -208,30 +196,31 @@ def text(self, value: Optional[str]):
208196

209197
# color
210198
@property
211-
def color(self) -> Optional[str]:
199+
def color(self) -> Union[None, str, Dict[ControlState, str]]:
212200
return self.__color
213201

214202
@color.setter
215-
def color(self, value: Optional[str]):
203+
def color(self, value: Union[None, str, Dict[ControlState, str]]):
216204
self.__color = value
217205

218206
# bgcolor
219207
@property
220-
def bgcolor(self) -> Optional[str]:
208+
def bgcolor(self) -> Union[None, str, Dict[ControlState, str]]:
221209
return self.__bgcolor
222210

223211
@bgcolor.setter
224-
def bgcolor(self, value: Optional[str]):
212+
def bgcolor(self, value: Union[None, str, Dict[ControlState, str]]):
225213
self.__bgcolor = value
226-
self._set_attr("bgColor", value)
227214

228215
# elevation
229216
@property
230-
def elevation(self) -> OptionalNumber:
217+
def elevation(self) -> Union[OptionalNumber, Dict[ControlState, OptionalNumber]]:
231218
return self.__elevation
232219

233220
@elevation.setter
234-
def elevation(self, value: OptionalNumber):
221+
def elevation(
222+
self, value: Union[OptionalNumber, Dict[ControlState, OptionalNumber]]
223+
):
235224
self.__elevation = value
236225

237226
# style

0 commit comments

Comments
 (0)