Created
November 3, 2020 03:28
-
-
Save Andrious/85d09837ba6fb46c0d3baa20145f1d7f to your computer and use it in GitHub Desktop.
Class to supply 'dark mode' ThemeData and record the dark mode setting using the Preference plugin.
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
import 'package:flutter/material.dart' show ThemeData; | |
import 'package:prefs/prefs.dart'; | |
/// The App's theme controller | |
class ThemeController { | |
factory ThemeController() => _this ??= ThemeController._(); | |
ThemeController._() { | |
_isDarkmode = Prefs.getBool('darkmode', false); | |
} | |
static ThemeController _this; | |
bool _isDarkmode; | |
/// Indicate if in 'dark mode' or not | |
bool get isDarkMode => _isDarkmode; | |
/// Record if the App's in dark mode or not. | |
set isDarkMode(bool set) { | |
if (set == null) { | |
return; | |
} | |
_isDarkmode = set; | |
Prefs.setBool('darkmode', _isDarkmode); | |
} | |
/// Explicitly return the 'dark theme.' | |
ThemeData setDarkMode() { | |
isDarkMode = true; | |
return ThemeData.dark(); | |
} | |
/// Returns 'dark theme' only if specified. | |
/// Otherwise, it returns null. | |
ThemeData setIfDarkMode() => _isDarkmode ? setDarkMode() : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment