Add theme mode setting

This commit is contained in:
Nutcake 2023-05-25 19:49:09 +02:00
parent 8668e66813
commit c43a93d574
3 changed files with 36 additions and 1 deletions

View file

@ -128,7 +128,7 @@ class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
textTheme: _typography.white,
colorScheme: darkDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark),
),
themeMode: ThemeMode.dark,
themeMode: ThemeMode.values[widget.settingsClient.currentSettings.themeMode.valueOrDefault],
home: Builder( // Builder is necessary here since we need a context which has access to the ClientHolder
builder: (context) {
showUpdateDialogOnFirstBuild(context);

View file

@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/models/sem_ver.dart';
import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';
class SettingsEntry<T> {
@ -38,15 +39,18 @@ class Settings {
final SettingsEntry<int> lastOnlineStatus;
final SettingsEntry<String> lastDismissedVersion;
final SettingsEntry<String> machineId;
final SettingsEntry<int> themeMode;
Settings({
SettingsEntry<bool>? notificationsDenied,
SettingsEntry<int>? lastOnlineStatus,
SettingsEntry<int>? themeMode,
SettingsEntry<String>? lastDismissedVersion,
SettingsEntry<String>? machineId
})
: notificationsDenied = notificationsDenied ?? const SettingsEntry<bool>(deflt: false),
lastOnlineStatus = lastOnlineStatus ?? SettingsEntry<int>(deflt: OnlineStatus.online.index),
themeMode = themeMode ?? SettingsEntry<int>(deflt: ThemeMode.dark.index),
lastDismissedVersion = lastDismissedVersion ?? SettingsEntry<String>(deflt: SemVer.zero().toString()),
machineId = machineId ?? SettingsEntry<String>(deflt: const Uuid().v4());
@ -54,6 +58,7 @@ class Settings {
return Settings(
notificationsDenied: retrieveEntryOrNull<bool>(map["notificationsDenied"]),
lastOnlineStatus: retrieveEntryOrNull<int>(map["lastOnlineStatus"]),
themeMode: retrieveEntryOrNull<int>(map["themeMode"]),
lastDismissedVersion: retrieveEntryOrNull<String>(map["lastDismissedVersion"]),
machineId: retrieveEntryOrNull<String>(map["machineId"]),
);
@ -72,6 +77,7 @@ class Settings {
return {
"notificationsDenied": notificationsDenied.toMap(),
"lastOnlineStatus": lastOnlineStatus.toMap(),
"themeMode": themeMode.toMap(),
"lastDismissedVersion": lastDismissedVersion.toMap(),
"machineId": machineId.toMap(),
};
@ -82,12 +88,14 @@ class Settings {
Settings copyWith({
bool? notificationsDenied,
int? lastOnlineStatus,
int? themeMode,
String? lastDismissedVersion,
String? machineId,
}) {
return Settings(
notificationsDenied: this.notificationsDenied.passThrough(notificationsDenied),
lastOnlineStatus: this.lastOnlineStatus.passThrough(lastOnlineStatus),
themeMode: this.themeMode.passThrough(themeMode),
lastDismissedVersion: this.lastDismissedVersion.passThrough(lastDismissedVersion),
machineId: this.machineId.passThrough(machineId),
);

View file

@ -1,5 +1,7 @@
import 'package:contacts_plus_plus/client_holder.dart';
import 'package:flutter/material.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:intl/intl.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';
@ -27,6 +29,31 @@ class SettingsPage extends StatelessWidget {
initialState: !sClient.currentSettings.notificationsDenied.valueOrDefault,
onChanged: (value) async => await sClient.changeSettings(sClient.currentSettings.copyWith(notificationsDenied: !value)),
),
const ListSectionHeader(name: "Appearance"),
ListTile(
trailing: StatefulBuilder(
builder: (context, setState) {
return DropdownButton<ThemeMode>(
items: ThemeMode.values.map((mode) => DropdownMenuItem<ThemeMode>(
value: mode,
child: Text("${toBeginningOfSentenceCase(mode.name)}",),
)).toList(),
value: ThemeMode.values[sClient.currentSettings.themeMode.valueOrDefault],
onChanged: (ThemeMode? value) async {
final currentSetting = sClient.currentSettings.themeMode.value;
if (currentSetting != value?.index) {
await sClient.changeSettings(sClient.currentSettings.copyWith(themeMode: value?.index));
if (context.mounted) {
Phoenix.rebirth(context);
}
}
setState(() {});
},
);
}
),
title: const Text("Theme Mode"),
),
const ListSectionHeader(name: "Other"),
ListTile(
trailing: const Icon(Icons.logout),