Skip to content

Instantly share code, notes, and snippets.

@Andrious
Created November 3, 2020 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrious/85d09837ba6fb46c0d3baa20145f1d7f to your computer and use it in GitHub Desktop.
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.
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