cofounderella/lib/themes/theme_provider.dart

36 lines
941 B
Dart
Raw Permalink Normal View History

import 'package:cofounderella/constants.dart';
import 'package:cofounderella/themes/dark_mode.dart';
import 'package:cofounderella/themes/light_mode.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ThemeProvider extends ChangeNotifier {
ThemeData _themeData;
ThemeProvider({required bool initialIsDarkMode})
: _themeData = initialIsDarkMode ? darkMode : lightMode;
ThemeData get themeData => _themeData;
bool get isDarkMode => _themeData == darkMode;
set themeData(ThemeData themeData) {
_themeData = themeData;
notifyListeners();
_saveTheme();
}
void toggleTheme() {
if (_themeData == lightMode) {
themeData = darkMode;
} else {
themeData = lightMode;
}
}
void _saveTheme() async {
final prefs = await SharedPreferences.getInstance();
prefs.setBool(Constants.prefKeyThemeDarkMode, isDarkMode);
}
}