setMetaViewportScale function

bool setMetaViewportScale({
  1. String? minimumScale,
  2. String? maximumScale,
})

Sets the meta viewport with minimumScale and maximumScale.

Implementation

bool setMetaViewportScale({String? minimumScale, String? maximumScale}) {
  if (minimumScale == null && maximumScale == null) return false;

  var metaViewportList = getMetaTagsWithName('viewport');
  if (metaViewportList.isEmpty) return false;

  var metaViewport = metaViewportList[0];

  var content = metaViewport.getAttribute('content')!;
  var params = parseMetaContent(content);

  var defaultScale = params['initial-scale'] ?? '1.0';

  var changed = false;

  if (maximumScale != null) {
    minimumScale = minimumScale!.trim();
    if (minimumScale.isEmpty || minimumScale == '*') {
      minimumScale = defaultScale;
    }

    params['minimum-scale'] = minimumScale;
    changed = true;
  }

  if (maximumScale != null) {
    maximumScale = maximumScale.trim();
    if (maximumScale.isEmpty || maximumScale == '*') {
      maximumScale = defaultScale;
    }

    params['maximum-scale'] = maximumScale;
    changed = true;
  }

  if (changed) {
    var content2 = buildMetaContent(params);
    metaViewport.setAttribute('content', content2);
    return true;
  }

  return false;
}