initialize method

Future<void> initialize({
  1. List<FontOption>? customFonts,
  2. FontOption? defaultFont,
  3. bool includeGoogleFonts = true,
})

Initialize the controller with default fonts and load saved preferences

Implementation

Future<void> initialize({
  List<FontOption>? customFonts,
  FontOption? defaultFont,
  bool includeGoogleFonts = true,
}) async {
  if (_initialized) return;

  // Clear existing fonts
  _availableFonts.clear();

  // Add custom fonts if provided
  if (customFonts != null && customFonts.isNotEmpty) {
    _availableFonts.addAll(customFonts);
  }

  // Add Google Fonts if requested
  if (includeGoogleFonts) {
    _availableFonts.addAll(FontConstants.defaultGoogleFonts);
  }

  // Add system fonts
  _availableFonts.addAll(FontConstants.systemFonts);

  // Load saved preferences
  await _loadSavedFont();

  // If no font is selected and a default is provided, use it
  if (_selectedFont == null && defaultFont != null) {
    _selectedFont = _findFontOption(defaultFont.fontFamily) ?? defaultFont;

    // If the default font is not in the available fonts, add it
    if (!_availableFonts.contains(_selectedFont)) {
      _availableFonts.add(_selectedFont!);
    }
  }

  // If still no font is selected, use the first available font
  _selectedFont ??= _availableFonts.isNotEmpty
      ? _availableFonts.first
      : const FontOption(fontFamily: 'Roboto', isGoogleFont: true);

  _initialized = true;
  notifyListeners();
}