updateIndex function

void updateIndex({
  1. required String imageMode,
  2. required bool showImages,
})

Implementation

void updateIndex({required String imageMode, required bool showImages}) {
  print('[Web] Updating index.html');
  final webIndex = File(_webIndex);
  var lines = webIndex.readAsLinesSync();

  var foundExistingStyleSheet = false;
  var headCloseTagLine = 0;
  var dartScriptTagLine = 0;
  var existingPictureLine = 0;

  final styleSheetLink =
      '<link rel="stylesheet" type="text/css" href="splash/style.css">';
  for (var x = 0; x < lines.length; x++) {
    var line = lines[x];

    if (line.contains(styleSheetLink)) {
      foundExistingStyleSheet = true;
    } else if (line.contains('</head>')) {
      headCloseTagLine = x;
    } else if (line.contains('src="main.dart.js"')) {
      dartScriptTagLine = x;
    } else if (line.contains('<picture id="splash">')) {
      existingPictureLine = x;
    }
  }

  if (!foundExistingStyleSheet) {
    lines[headCloseTagLine] = '  ' + styleSheetLink + '\n</head>';
  }

  if (existingPictureLine == 0) {
    if (showImages) {
      for (var x = _indexHtmlPicture.length - 1; x >= 0; x--) {
        lines[dartScriptTagLine] =
            _indexHtmlPicture[x].replaceFirst('[IMAGEMODE]', imageMode) +
                '\n' +
                lines[dartScriptTagLine];
      }
    }
  } else {
    if (showImages) {
      for (var x = 0; x < _indexHtmlPicture.length; x++) {
        lines[existingPictureLine + x] =
            _indexHtmlPicture[x].replaceFirst('[IMAGEMODE]', imageMode);
      }
    } else {
      lines.removeRange(
          existingPictureLine, existingPictureLine + _indexHtmlPicture.length);
    }
  }
  webIndex.writeAsStringSync(lines.join('\n'));
}