Skip to content

Instantly share code, notes, and snippets.

@abdullah017
Created January 7, 2023 14:11
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 abdullah017/d295fbe9edb520c202721d6e808c2a02 to your computer and use it in GitHub Desktop.
Save abdullah017/d295fbe9edb520c202721d6e808c2a02 to your computer and use it in GitHub Desktop.
theme_service.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
class ThemeService {
final _box = GetStorage();
final _key = 'isDarkMode';
_saveTheme(bool isDarkMode) => _box.write(_key, isDarkMode);
bool _loadThemeFromBox() => _box.read(_key) ?? false;
ThemeMode get theme => _loadThemeFromBox() ? ThemeMode.dark : ThemeMode.light;
void switchTheme() {
Get.changeThemeMode(_loadThemeFromBox() ? ThemeMode.dark : ThemeMode.light);
_saveTheme(!_loadThemeFromBox());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment