-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added follow system preference for theme
- Loading branch information
1 parent
bc4e904
commit 5b6171b
Showing
8 changed files
with
344 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
Oops, something went wrong.