|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | + |
| 3 | +/// Controller for managing the state of a [LoadSwitch] widget. |
| 4 | +/// |
| 5 | +/// This controller allows for programmatic control of a [LoadSwitch] widget, |
| 6 | +/// including changing the value, loading state, and active state. |
| 7 | +class LoadSwitchController extends ChangeNotifier { |
| 8 | + /// Creates a new [LoadSwitchController] with the specified initial values. |
| 9 | + LoadSwitchController({ |
| 10 | + bool initialValue = false, |
| 11 | + bool isLoading = false, |
| 12 | + bool isActive = true, |
| 13 | + }) : _value = initialValue, |
| 14 | + _isLoading = isLoading, |
| 15 | + _isActive = isActive; |
| 16 | + |
| 17 | + bool _value; |
| 18 | + bool _isLoading; |
| 19 | + bool _isActive; |
| 20 | + |
| 21 | + /// The current value of the switch (on/off). |
| 22 | + bool get value => _value; |
| 23 | + |
| 24 | + /// Whether the switch is currently in a loading state. |
| 25 | + bool get isLoading => _isLoading; |
| 26 | + |
| 27 | + /// Whether the switch is currently active and can be interacted with. |
| 28 | + bool get isActive => _isActive; |
| 29 | + |
| 30 | + /// Sets the value of the switch and notifies listeners. |
| 31 | + set value(bool newValue) { |
| 32 | + if (_value != newValue) { |
| 33 | + _value = newValue; |
| 34 | + notifyListeners(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Sets the loading state of the switch and notifies listeners. |
| 39 | + set isLoading(bool loading) { |
| 40 | + if (_isLoading != loading) { |
| 41 | + _isLoading = loading; |
| 42 | + notifyListeners(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Sets the active state of the switch and notifies listeners. |
| 47 | + set isActive(bool active) { |
| 48 | + if (_isActive != active) { |
| 49 | + _isActive = active; |
| 50 | + notifyListeners(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Toggles the value of the switch if it's not loading and is active. |
| 55 | + void toggle() { |
| 56 | + if (!_isLoading && _isActive) { |
| 57 | + value = !value; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Executes a future with loading state management. |
| 62 | + /// |
| 63 | + /// Sets [isLoading] to true before executing the future, and to false after |
| 64 | + /// the future completes or throws an error. Updates the value of the switch |
| 65 | + /// based on the result of the future. |
| 66 | + /// |
| 67 | + /// If [onChange] is provided, it will be called with the new value. |
| 68 | + /// If [onError] is provided, it will be called with any error that occurs. |
| 69 | + Future<void> executeWithLoading( |
| 70 | + Future<bool> Function() future, { |
| 71 | + Function(bool)? onChange, |
| 72 | + Function(Object)? onError, |
| 73 | + }) async { |
| 74 | + if (_isLoading || !_isActive) return; |
| 75 | + |
| 76 | + isLoading = true; |
| 77 | + |
| 78 | + try { |
| 79 | + value = await future.call(); |
| 80 | + onChange?.call(value); |
| 81 | + } catch (error) { |
| 82 | + onError?.call(error); |
| 83 | + } finally { |
| 84 | + isLoading = false; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments