toggle method

Future<bool?> toggle({
  1. double? intensity,
})

Execute the action to toggle torch on/off. Returns the current torch state.

double intensity: Define the desired intensity and set as default. This parameter is ignored on Android.

Example:

final controller = TorchController();

controller.toggle(); // Activates the torch and returns true;
controller.toggle(); // Deactivates the torch and returns false;

Implementation

Future<bool?> toggle({double? intensity}) async {
  if (Platform.isAndroid && intensity != null) {
    debugPrint("Warning: Torch intensity is not available on Android");
  }

  if (intensity != null) {
    assert(intensity > 0 && intensity <= 1,
        "You can only use values between 0 and 1");
  }
  assert(_ensureInitialized, '''
    You must initialize your `TorchControl` instance on your runApp().
    You can perform this by adding the following line to your main.dart:

    `TorchControl.initialize();`
  ''');

  try {
    if (intensity != null) {
      _torchIntensity = intensity;
    }

    return await _channel.invokeMethod('toggleTorch', _torchIntensity);
  } catch (error) {
    if (_debug) {
      debugPrint(error.toString());
    }

    if (!_suppressTorchErrors) {
      rethrow;
    }

    return false;
  }
}