Skip to content

feature: System Theme Mode #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/app/data/providers/secure_storage_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class SecureStorageProvider {
Future<AppTheme> readThemeValue() async {
String themeValue =
await _secureStorage.read(key: 'theme_value') ?? 'AppTheme.dark';
return themeValue == 'AppTheme.dark' ? AppTheme.dark : AppTheme.light;
return themeValue == 'AppTheme.dark' ? AppTheme.dark
: themeValue == 'AppTheme.light' ? AppTheme.light
: AppTheme.system;
}

Future<void> writeThemeValue({
Expand Down
44 changes: 28 additions & 16 deletions lib/app/modules/settings/controllers/theme_controller.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ultimate_alarm_clock/app/data/providers/secure_storage_provider.dart';
import 'package:ultimate_alarm_clock/app/utils/constants.dart';

class ThemeController extends GetxController {
final _secureStorageProvider = SecureStorageProvider();
Brightness platformBrightness = Brightness.dark;
bool isSystemTheme = false;

@override
void onInit() {
_loadThemeValue();
updateThemeColors();
super.onInit();
}

Rx<ThemeMode> currentTheme = ThemeMode.system.obs;

void switchTheme() {
currentTheme.value = currentTheme.value == ThemeMode.light
? ThemeMode.dark
: ThemeMode.light;
updateThemeColors();
}

Rx<Color> primaryColor = kprimaryColor.obs;
Rx<Color> secondaryColor = kLightSecondaryColor.obs;
Rx<Color> primaryBackgroundColor = kLightPrimaryBackgroundColor.obs;
Expand Down Expand Up @@ -51,25 +47,41 @@ class ThemeController extends GetxController {
}

void _loadThemeValue() async {
currentTheme.value =
await _secureStorageProvider.readThemeValue() == AppTheme.light
? ThemeMode.light
: ThemeMode.dark;
AppTheme theme = await _secureStorageProvider.readThemeValue();
currentTheme.value = getCurrentTheme(theme == AppTheme.light ? ThemeMode.light
: theme == AppTheme.dark ? ThemeMode.dark
: ThemeMode.system,
);
updateThemeColors();
Get.changeThemeMode(currentTheme.value);
}

void _saveThemeValuePreference() async {
await _secureStorageProvider.writeThemeValue(
theme: currentTheme.value == ThemeMode.light
? AppTheme.light
theme: isSystemTheme ? AppTheme.system
: currentTheme.value == ThemeMode.light ? AppTheme.light
: AppTheme.dark,
);
}

void toggleThemeValue(bool enabled) {
currentTheme.value = enabled ? ThemeMode.light : ThemeMode.dark;
void toggleThemeValue(ThemeMode mode) {
currentTheme.value = getCurrentTheme(mode);
updateThemeColors();
_saveThemeValuePreference();
Get.changeThemeMode(currentTheme.value);
}

getCurrentTheme(ThemeMode mode){
ThemeMode theme;
if (mode == ThemeMode.system){
isSystemTheme = true;
platformBrightness = Get.mediaQuery.platformBrightness;
theme = platformBrightness == Brightness.light ? ThemeMode.light
: ThemeMode.dark;
}else{
isSystemTheme = false;
theme = mode;
}
return theme;
}
}
81 changes: 47 additions & 34 deletions lib/app/modules/settings/views/theme_value_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:ultimate_alarm_clock/app/modules/settings/controllers/theme_cont
import 'package:ultimate_alarm_clock/app/utils/constants.dart';
import 'package:ultimate_alarm_clock/app/utils/utils.dart';

class ThemeValueTile extends StatefulWidget {
class ThemeValueTile extends StatelessWidget {
const ThemeValueTile({
super.key,
required this.controller,
Expand All @@ -20,52 +20,65 @@ class ThemeValueTile extends StatefulWidget {
final double height;
final double width;

@override
State<ThemeValueTile> createState() => _ThemeValueTileState();
}

class _ThemeValueTileState extends State<ThemeValueTile> {
@override
void initState() {
super.initState();
}
final themes = const <MapEntry>[MapEntry('dark', 'Dark Theme'),
MapEntry('light', 'Light Theme'),
MapEntry('system', 'System Theme'),];

@override
Widget build(BuildContext context) {
ThemeMode theme = themeController.currentTheme.value;
return Obx(
() => Container(
width: widget.width * 0.91,
height: widget.height * 0.1,
width: width * 0.91,
height: height * 0.1,
decoration: Utils.getCustomTileBoxDecoration(
isLightMode:
widget.themeController.currentTheme.value == ThemeMode.light,
themeController.currentTheme.value == ThemeMode.light,
),
child: Padding(
padding: EdgeInsets.only(left: 30, right: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
'Enable Light Mode'.tr,
style: Theme.of(context).textTheme.bodyLarge,
child: Obx(
() => DropdownMenu(
menuStyle: MenuStyle(
backgroundColor: MaterialStatePropertyAll(
themeController.secondaryBackgroundColor.value,
),
),
Obx(
() => Switch.adaptive(
value: widget.themeController.currentTheme.value ==
ThemeMode.light,
activeColor: ksecondaryColor,
onChanged: (bool value) async {
widget.themeController.toggleThemeValue(value);
Get.changeThemeMode(
widget.themeController.currentTheme.value,
);
Utils.hapticFeedback();
},
),
inputDecorationTheme:
const InputDecorationTheme(enabledBorder: InputBorder.none),
trailingIcon: Icon(
Icons.arrow_drop_down_outlined,
size: 40.0,
color: themeController.primaryTextColor.value
.withOpacity(0.8),
),
],
width: width * 0.78,
initialSelection: themeController.isSystemTheme ? themes[2].key
: theme == ThemeMode.dark ? themes[0].key
: themes[1].key,
label: const Text('Select Theme'),
dropdownMenuEntries: themes.map((e) {
return DropdownMenuEntry(
value: e.key,
label: e.value,
style: ButtonStyle(
foregroundColor: MaterialStatePropertyAll(
themeController.primaryTextColor.value,
),
),
);
}).toList(),
onSelected: (newValue) {
if (newValue == themes[0].key) {
themeController.toggleThemeValue(ThemeMode.dark);
} else if (newValue == themes[1].key) {
themeController.toggleThemeValue(ThemeMode.light);
} else {
themeController.toggleThemeValue(ThemeMode.system);
}
Utils.hapticFeedback();
},
),
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/app/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ enum ApiKeys {
openWeatherMap,
}

enum AppTheme { light, dark }
enum AppTheme { light, dark, system }

enum Status { initialized, ongoing, completed }

Expand Down