toggleAttr method
Toggles a boolean attribute on the element.
If force is provided:
- true: adds the attribute
- false: removes the attribute
If force is not provided, toggles the current state.
Returns the new state (true if attribute exists, false if removed).
Example
toggleAttr('disabled'); // Toggle current state
toggleAttr('disabled', true); // Force add
toggleAttr('disabled', false); // Force remove
Implementation
bool toggleAttr(String name, [bool? force]) {
if (_element == null) return false;
final shouldAdd = force ?? !hasAttr(name);
if (shouldAdd) {
_element!.setAttribute(name, '');
} else {
_element!.removeAttribute(name);
}
return shouldAdd;
}