-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
10 changed files
with
270 additions
and
37 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/scheduler.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class ThemeSwitcher with ChangeNotifier { | ||
ThemeMode _themeMode = ThemeMode.system; | ||
bool _didChangeTheme = false; | ||
|
||
ThemeSwitcher() { | ||
_fetchTheme(); | ||
} | ||
|
||
Future<void> _fetchTheme() async { | ||
final instance = await SharedPreferences.getInstance(); | ||
final theme = instance.getString('theme'); | ||
if (theme == null) return; | ||
switch (theme) { | ||
case 'dark': | ||
_themeMode = ThemeMode.dark; | ||
break; | ||
case 'light': | ||
_themeMode = ThemeMode.light; | ||
break; | ||
} | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> _saveTheme(ThemeMode theme) async { | ||
if (!_didChangeTheme) return; | ||
final instance = await SharedPreferences.getInstance(); | ||
await instance.setString( | ||
'theme', theme == ThemeMode.dark ? 'dark' : 'light'); | ||
} | ||
|
||
ThemeMode get themeMode => _themeMode; | ||
|
||
void switchTheme() { | ||
var newTheme; | ||
if (_themeMode == ThemeMode.system) | ||
_themeMode = | ||
SchedulerBinding.instance.window.platformBrightness == Brightness.dark | ||
? ThemeMode.dark | ||
: ThemeMode.light; | ||
switch (_themeMode) { | ||
case ThemeMode.dark: | ||
newTheme = ThemeMode.light; | ||
break; | ||
case ThemeMode.light: | ||
newTheme = ThemeMode.dark; | ||
break; | ||
default: | ||
newTheme = _themeMode; | ||
} | ||
_themeMode = newTheme; | ||
_didChangeTheme = true; | ||
_saveTheme(_themeMode); | ||
notifyListeners(); | ||
} | ||
|
||
void resetTheme() { | ||
_themeMode = ThemeMode.system; | ||
_didChangeTheme = false; | ||
_deleteTheme(); | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> _deleteTheme() async { | ||
final instance = await SharedPreferences.getInstance(); | ||
await instance.remove('theme'); | ||
} | ||
} |
Oops, something went wrong.