setProperty method

void setProperty(
  1. String propertyName,
  2. String? value, {
  3. bool? isImportant,
  4. String? baseHref,
  5. bool validate = true,
})

Modifies an existing CSS property or creates a new CSS property in the declaration block.

Implementation

void setProperty(
  String propertyName,
  String? value, {
  bool? isImportant,
  String? baseHref,
  bool validate = true,
}) {
  propertyName = propertyName.trim();

  // Null or empty value means should be removed.
  if (isNullOrEmptyValue(value)) {
    removeProperty(propertyName, isImportant);
    return;
  }

  final String rawValue = value.toString();
  final bool isCustomProperty = CSSVariable.isCSSSVariableProperty(propertyName);
  String normalizedValue = isCustomProperty ? rawValue : _toLowerCase(propertyName, rawValue.trim());

  if (validate && !_isValidValue(propertyName, normalizedValue)) return;

  if (_cssShorthandProperty[propertyName] != null) {
    return _expandShorthand(propertyName, normalizedValue, isImportant, baseHref: baseHref, validate: validate);
  }

  // From style sheet mark the property important as false.
  if (isImportant == false) {
    _sheetStyle[propertyName] = normalizedValue;
  }

  // If the important property is already set, we should ignore it.
  if (isImportant != true && _importants[propertyName] == true) {
    return;
  }

  if (isImportant == true) {
    _importants[propertyName] = true;
  }

  String? prevValue = getPropertyValue(propertyName);
  if (normalizedValue == prevValue && (!CSSVariable.isCSSVariableValue(normalizedValue))) return;

  _pendingProperties[propertyName] = CSSPropertyValue(normalizedValue, baseHref: baseHref);
}