initializeTheme method

Future<void> initializeTheme({
  1. ThemeMode mode = ThemeMode.system,
})

Initialize the app's theme from saved preferences Initialize the app's theme from saved preferences or default mode

Implementation

Future<void> initializeTheme({ThemeMode mode = ThemeMode.system}) async {
  try {
    String? savedTheme = ProKitPrefManager.getString('theme');

    /// Use saved theme if available and valid
    if (_isValidTheme(savedTheme)) {
      _themeMode = themeMap[savedTheme]!;
    } else {
      /// Apply provided default theme mode (from app constructor)
      _themeMode = mode;

      /// Save this default to preferences for persistence
      await ProKitPrefManager.setString('theme', mode.toString().split('.').last);
    }

    /// Update static theme utility as well
    ProKitTheme.changeAppTheme(
      themeType: _themeMode == ThemeMode.light ? ThemeType.light : ThemeType.dark,
    );
  } catch (e) {
    /// Handle error gracefully and fallback
    _themeMode = mode;
  }

  Future.microtask(notifyListeners);
}