flet-flashlight 0.1.0.dev1__py3-none-any.whl → 0.2.0.dev30__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of flet-flashlight might be problematic. Click here for more details.

@@ -1,62 +1,101 @@
1
- import 'dart:io' show Platform;
2
- import 'dart:io';
3
-
4
1
  import 'package:flet/flet.dart';
5
2
  import 'package:flutter/widgets.dart';
6
3
  import 'package:torch_light/torch_light.dart';
7
4
 
8
- class FlashlightControl extends StatefulWidget {
9
- final Control? parent;
10
- final Control control;
11
- final Widget? nextChild;
12
- final FletControlBackend backend;
13
-
14
- const FlashlightControl(
15
- {super.key,
16
- required this.parent,
17
- required this.control,
18
- required this.nextChild,
19
- required this.backend});
20
-
21
- @override
22
- State<FlashlightControl> createState() => _FlashlightControlState();
23
- }
5
+ class FlashlightControl extends FletService {
6
+ FlashlightControl({required super.control});
24
7
 
25
- class _FlashlightControlState extends State<FlashlightControl> {
26
8
  @override
27
- Widget build(BuildContext context) {
28
- debugPrint("FlashLightControl build: ${widget.control.id}");
9
+ void init() {
10
+ super.init();
11
+ debugPrint("Flashlight(${control.id}).init: ${control.properties}");
12
+ control.addInvokeMethodListener(_invokeMethod);
13
+ }
29
14
 
30
- if (Platform.isIOS || Platform.isAndroid) {
31
- () async {
32
- widget.backend.subscribeMethods(widget.control.id,
33
- (methodName, args) async {
34
- switch (methodName) {
35
- case "on":
36
- try {
37
- await TorchLight.enableTorch();
38
- return "1";
39
- } on Exception catch (e) {
40
- debugPrint("Couldn't enable Flash: $e");
41
- return "0";
42
- }
43
- case "off":
44
- try {
45
- await TorchLight.disableTorch();
46
- return "1";
47
- } on Exception catch (e) {
48
- debugPrint("Couldn't disable Flash: $e");
49
- return "0";
50
- }
15
+ Future<dynamic> _invokeMethod(String name, dynamic args) async {
16
+ debugPrint("Flashlight.$name($args)");
17
+ if (isMobilePlatform()) {
18
+ Map<String, String?>? errorInfo;
19
+ switch (name) {
20
+ case "on":
21
+ try {
22
+ await TorchLight.enableTorch();
23
+ return true;
24
+ } catch (e) {
25
+ if (e is EnableTorchExistentUserException) {
26
+ errorInfo = {
27
+ "error_type": "EnableTorchExistentUserException",
28
+ "error_msg": e.message
29
+ };
30
+ } else if (e is EnableTorchNotAvailableException) {
31
+ errorInfo = {
32
+ "error_type": "EnableTorchNotAvailableException",
33
+ "error_msg": e.message
34
+ };
35
+ } else {
36
+ errorInfo = {
37
+ "error_type": "EnableTorchException",
38
+ "error_msg": (e as EnableTorchException).message
39
+ };
40
+ }
41
+ control.triggerEvent("error", errorInfo);
42
+ debugPrint(
43
+ "Error enabling Flashlight: ${errorInfo["error_type"]}(${errorInfo["error_msg"]})");
44
+ return errorInfo;
51
45
  }
52
- return null;
53
- });
54
- }();
55
-
56
- return const SizedBox.shrink();
46
+ case "off":
47
+ try {
48
+ await TorchLight.disableTorch();
49
+ return true;
50
+ } catch (e) {
51
+ if (e is DisableTorchExistentUserException) {
52
+ errorInfo = {
53
+ "error_type": "DisableTorchExistentUserException",
54
+ "error_msg": e.message
55
+ };
56
+ } else if (e is DisableTorchNotAvailableException) {
57
+ errorInfo = {
58
+ "error_type": "DisableTorchNotAvailableException",
59
+ "error_msg": e.message
60
+ };
61
+ } else {
62
+ errorInfo = {
63
+ "error_type": "DisableTorchException",
64
+ "error_msg": (e as DisableTorchException).message
65
+ };
66
+ }
67
+ control.triggerEvent("error", errorInfo);
68
+ debugPrint(
69
+ "Error disabling Flashlight: ${errorInfo["error_type"]}(${errorInfo["error_msg"]})");
70
+ return errorInfo;
71
+ }
72
+ case "is_available":
73
+ try {
74
+ final available = await TorchLight.isTorchAvailable();
75
+ return available;
76
+ } on EnableTorchException catch (e) {
77
+ errorInfo = {
78
+ "error_type": "EnableTorchException",
79
+ "error_msg": e.message
80
+ };
81
+ control.triggerEvent("error", errorInfo);
82
+ debugPrint(
83
+ "Error checking Flashlight availability: EnableTorchException(${e.message})");
84
+ return errorInfo;
85
+ }
86
+ default:
87
+ throw Exception("Unknown Flashlight method: $name");
88
+ }
57
89
  } else {
58
- return const ErrorControl(
59
- "FlashLight control is not supported on this platform yet.");
90
+ throw Exception(
91
+ "Flashlight control is supported only on Android and iOS devices.");
60
92
  }
61
93
  }
94
+
95
+ @override
96
+ void dispose() {
97
+ debugPrint("Flashlight(${control.id}).dispose");
98
+ control.removeInvokeMethodListener(_invokeMethod);
99
+ super.dispose();
100
+ }
62
101
  }