Skip to content

Commit

Permalink
Added follow system preference for theme
Browse files Browse the repository at this point in the history
  • Loading branch information
git-elliot committed Feb 4, 2024
1 parent bc4e904 commit 5b6171b
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 240 deletions.
14 changes: 9 additions & 5 deletions lib/helper/dark_theme_preference.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import 'package:shared_preferences/shared_preferences.dart';
import 'package:vernet/models/dark_theme_provider.dart';

class DarkThemePreference {
static const themeStatus = 'THEMESTATUS';
static const themeStatus = 'THEMESTATUS_NEW';

Future<void> setDarkTheme(bool value) async {
Future<void> setDarkTheme(ThemePreference value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool(themeStatus, value);
prefs.setString(themeStatus, value.name);
}

Future<bool> getTheme() async {
Future<ThemePreference> getTheme() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool(themeStatus) ?? false;
return ThemePreference.values.firstWhere(
(element) => element.name == prefs.getString(themeStatus),
orElse: () => ThemePreference.light,
);
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class _MyAppState extends State<MyApp> {
}

Future<void> getCurrentAppTheme() async {
themeChangeProvider.darkTheme =
themeChangeProvider.themePref =
await themeChangeProvider.darkThemePreference.getTheme();
}

Expand Down
17 changes: 14 additions & 3 deletions lib/models/dark_theme_provider.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:vernet/helper/dark_theme_preference.dart';

class DarkThemeProvider with ChangeNotifier {
DarkThemePreference darkThemePreference = DarkThemePreference();
bool _darkTheme = false;
ThemePreference _darkTheme = ThemePreference.system;

bool get darkTheme => _darkTheme;
ThemePreference get themePref => _darkTheme;

set darkTheme(bool value) {
set themePref(ThemePreference value) {
_darkTheme = value;
darkThemePreference.setDarkTheme(value);
notifyListeners();
}

bool get darkTheme {
if (themePref == ThemePreference.system) {
return SchedulerBinding.instance.platformDispatcher.platformBrightness ==
Brightness.dark;
}
return ThemePreference.dark == themePref;
}
}

enum ThemePreference { system, dark, light }
Loading

0 comments on commit 5b6171b

Please sign in to comment.