Initial commit: Penyerahan final Source code Tugas Akhir
This commit is contained in:
60
lib/providers/theme_provider.dart
Normal file
60
lib/providers/theme_provider.dart
Normal file
@ -0,0 +1,60 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:initial_folder/theme.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class ThemeProvider with ChangeNotifier {
|
||||
ThemeData _themeData = ThemeClass.darkmode;
|
||||
|
||||
ThemeProvider() {
|
||||
loadTheme();
|
||||
}
|
||||
|
||||
ThemeData get themeData => _themeData;
|
||||
|
||||
set themeData(ThemeData themeData) {
|
||||
_themeData = themeData;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void toggleTheme() {
|
||||
if (_themeData == ThemeClass.lightmode) {
|
||||
themeData = ThemeClass.darkmode;
|
||||
_saveTheme('dark');
|
||||
} else {
|
||||
themeData = ThemeClass.lightmode;
|
||||
_saveTheme('light');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadTheme() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
String? theme = prefs.getString('theme');
|
||||
if (theme == null) {
|
||||
var brightness =
|
||||
WidgetsBinding.instance.platformDispatcher.platformBrightness;
|
||||
|
||||
if (brightness == Brightness.light) {
|
||||
themeData = ThemeClass.lightmode;
|
||||
_saveTheme('light');
|
||||
} else {
|
||||
themeData = ThemeClass.darkmode;
|
||||
_saveTheme('dark');
|
||||
}
|
||||
} else if (theme == 'dark') {
|
||||
themeData = ThemeClass.darkmode;
|
||||
} else {
|
||||
themeData = ThemeClass.lightmode;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> saveCurrentTheme() async {
|
||||
String theme = _themeData == ThemeClass.darkmode ? 'dark' : 'light';
|
||||
await _saveTheme(theme);
|
||||
}
|
||||
|
||||
Future<void> _saveTheme(String theme) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('theme', theme);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user