diff --git a/sdk/python/packages/flet/src/flet/core/flashlight.py b/sdk/python/packages/flet/src/flet/core/flashlight.py index c31b9e41d..46feed866 100644 --- a/sdk/python/packages/flet/src/flet/core/flashlight.py +++ b/sdk/python/packages/flet/src/flet/core/flashlight.py @@ -42,41 +42,40 @@ def __init__( def _get_control_name(self): return "flashlight" + def _toggle_state(self, sr: str, state: bool=True) -> bool: + if ("1" == sr): + self.turned_on = state + return self.turned_on + def turn_on(self, wait_timeout: Optional[int] = 5) -> bool: sr = self.invoke_method("on", wait_for_result=True, wait_timeout=wait_timeout) + return self._toggle_state(sr) - if int(sr) == 1: - self.turned_on = True - return self.turned_on async def turn_on_async(self, wait_timeout: Optional[int] = 5) -> bool: sr = await self.invoke_method_async( "on", wait_for_result=True, wait_timeout=wait_timeout ) - if int(sr) == 1: - self.turned_on = True - return self.turned_on + return self._toggle_state(sr) + def turn_off(self, wait_timeout: Optional[int] = 5) -> bool: sr = self.invoke_method("off", wait_for_result=True, wait_timeout=wait_timeout) - - if int(sr) == 1: - self.turned_on = False - return self.turned_on + return self._toggle_state(sr, False) + async def turn_off_async(self, wait_timeout: Optional[int] = 5) -> bool: sr = await self.invoke_method_async( "off", wait_for_result=True, wait_timeout=wait_timeout ) - if int(sr) == 1: - self.turned_on = False - return self.turned_on + return self._toggle_state(sr, False) + def toggle(self, wait_timeout: Optional[int] = 5) -> bool: - if self.turned_on: - return self.turn_off(wait_timeout) - return self.turn_on(wait_timeout) + _func = self.turn_off if self.turned_on else self.turn_on + return _func(wait_timeout) + async def toggle_async(self, wait_timeout: Optional[int] = 5) -> bool: if self.turned_on: return await self.turn_off_async(wait_timeout)