22
33import flet as ft
44
5- __all__ = ["Flashlight" ]
6-
75from .exceptions import *
86
7+ __all__ = ["Flashlight" ]
8+
99
1010@ft .control ("Flashlight" )
1111class Flashlight (ft .Service ):
1212 """
1313 A control to use FlashLight. Works on iOS and Android. Based on torch_light Flutter widget (https://pub.dev/packages/torch_light).
1414
15- Flashlight control is non-visual and should be added to `page.overlay` list.
16-
17- Example:
18- ```
19- import flet as ft
20-
21- import flet_flashlight as ffl
22-
23- def main(page: ft.Page):
24- flashLight = ffl.Flashlight()
25- page.overlay.append(flashLight)
26- page.add(
27- ft.TextButton("toggle", on_click: lambda _: flashlight.toggle())
28- )
29-
30- ft.app(target=main)
31- ```
15+ Note:
16+ This control is a non-visual and should be added to `page.services` list before it can be used.
17+ """
3218
19+ on = False
20+ """
21+ Whether the flashlight is currently turned on.
3322 """
3423
35- turned_on = False
3624 on_error : ft .OptionalControlEventCallable = None
25+ """
26+ Fires when an error occurs.
27+
28+ The `data` property of the event handler argument contains information on the error.
29+ """
3730
3831 async def turn_on_async (self ):
32+ """
33+ Turns the flashlight on.
34+ """
3935 r = await self ._invoke_method_async ("on" )
4036 if r is True :
41- self .turned_on = True
37+ self .on = True
4238 else : # error occured
4339 error_type = r .get ("error_type" )
4440 error_msg = r .get ("error_msg" )
@@ -50,12 +46,18 @@ async def turn_on_async(self):
5046 raise FlashlightEnableException (error_msg )
5147
5248 def turn_on (self ):
49+ """
50+ Turns the flashlight on.
51+ """
5352 asyncio .create_task (self .turn_on_async ())
5453
5554 async def turn_off_async (self ):
55+ """
56+ Turns the flashlight off.
57+ """
5658 r = await self ._invoke_method_async ("off" )
5759 if r is True :
58- self .turned_on = False
60+ self .on = False
5961 else : # error occured
6062 error_type = r .get ("error_type" )
6163 error_msg = r .get ("error_msg" )
@@ -67,17 +69,29 @@ async def turn_off_async(self):
6769 raise FlashlightDisableException (error_msg )
6870
6971 def turn_off (self ):
72+ """
73+ Turns the flashlight off.
74+ """
7075 asyncio .create_task (self .turn_off_async ())
7176
7277 async def toggle_async (self ):
73- if self .turned_on :
78+ """
79+ Toggles the flashlight on and off.
80+ """
81+ if self .on :
7482 await self .turn_off_async ()
7583 await self .turn_on_async ()
7684
7785 def toggle (self ):
86+ """
87+ Toggles the flashlight on and off.
88+ """
7889 asyncio .create_task (self .toggle_async ())
7990
8091 async def is_available_async (self ):
92+ """
93+ Checks if the flashlight is available on the device.
94+ """
8195 r = await self ._invoke_method_async ("is_available" )
8296 if r is bool :
8397 return r
0 commit comments