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, textTheme: _typography.white,
colorScheme: darkDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark), 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 home: Builder( // Builder is necessary here since we need a context which has access to the ClientHolder
builder: (context) { builder: (context) {
showUpdateDialogOnFirstBuild(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/friend.dart';
import 'package:contacts_plus_plus/models/sem_ver.dart'; import 'package:contacts_plus_plus/models/sem_ver.dart';
import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
class SettingsEntry<T> { class SettingsEntry<T> {
@ -38,15 +39,18 @@ class Settings {
final SettingsEntry<int> lastOnlineStatus; final SettingsEntry<int> lastOnlineStatus;
final SettingsEntry<String> lastDismissedVersion; final SettingsEntry<String> lastDismissedVersion;
final SettingsEntry<String> machineId; final SettingsEntry<String> machineId;
final SettingsEntry<int> themeMode;
Settings({ Settings({
SettingsEntry<bool>? notificationsDenied, SettingsEntry<bool>? notificationsDenied,
SettingsEntry<int>? lastOnlineStatus, SettingsEntry<int>? lastOnlineStatus,
SettingsEntry<int>? themeMode,
SettingsEntry<String>? lastDismissedVersion, SettingsEntry<String>? lastDismissedVersion,
SettingsEntry<String>? machineId SettingsEntry<String>? machineId
}) })
: notificationsDenied = notificationsDenied ?? const SettingsEntry<bool>(deflt: false), : notificationsDenied = notificationsDenied ?? const SettingsEntry<bool>(deflt: false),
lastOnlineStatus = lastOnlineStatus ?? SettingsEntry<int>(deflt: OnlineStatus.online.index), lastOnlineStatus = lastOnlineStatus ?? SettingsEntry<int>(deflt: OnlineStatus.online.index),
themeMode = themeMode ?? SettingsEntry<int>(deflt: ThemeMode.dark.index),
lastDismissedVersion = lastDismissedVersion ?? SettingsEntry<String>(deflt: SemVer.zero().toString()), lastDismissedVersion = lastDismissedVersion ?? SettingsEntry<String>(deflt: SemVer.zero().toString()),
machineId = machineId ?? SettingsEntry<String>(deflt: const Uuid().v4()); machineId = machineId ?? SettingsEntry<String>(deflt: const Uuid().v4());
@ -54,6 +58,7 @@ class Settings {
return Settings( return Settings(
notificationsDenied: retrieveEntryOrNull<bool>(map["notificationsDenied"]), notificationsDenied: retrieveEntryOrNull<bool>(map["notificationsDenied"]),
lastOnlineStatus: retrieveEntryOrNull<int>(map["lastOnlineStatus"]), lastOnlineStatus: retrieveEntryOrNull<int>(map["lastOnlineStatus"]),
themeMode: retrieveEntryOrNull<int>(map["themeMode"]),
lastDismissedVersion: retrieveEntryOrNull<String>(map["lastDismissedVersion"]), lastDismissedVersion: retrieveEntryOrNull<String>(map["lastDismissedVersion"]),
machineId: retrieveEntryOrNull<String>(map["machineId"]), machineId: retrieveEntryOrNull<String>(map["machineId"]),
); );
@ -72,6 +77,7 @@ class Settings {
return { return {
"notificationsDenied": notificationsDenied.toMap(), "notificationsDenied": notificationsDenied.toMap(),
"lastOnlineStatus": lastOnlineStatus.toMap(), "lastOnlineStatus": lastOnlineStatus.toMap(),
"themeMode": themeMode.toMap(),
"lastDismissedVersion": lastDismissedVersion.toMap(), "lastDismissedVersion": lastDismissedVersion.toMap(),
"machineId": machineId.toMap(), "machineId": machineId.toMap(),
}; };
@ -82,12 +88,14 @@ class Settings {
Settings copyWith({ Settings copyWith({
bool? notificationsDenied, bool? notificationsDenied,
int? lastOnlineStatus, int? lastOnlineStatus,
int? themeMode,
String? lastDismissedVersion, String? lastDismissedVersion,
String? machineId, String? machineId,
}) { }) {
return Settings( return Settings(
notificationsDenied: this.notificationsDenied.passThrough(notificationsDenied), notificationsDenied: this.notificationsDenied.passThrough(notificationsDenied),
lastOnlineStatus: this.lastOnlineStatus.passThrough(lastOnlineStatus), lastOnlineStatus: this.lastOnlineStatus.passThrough(lastOnlineStatus),
themeMode: this.themeMode.passThrough(themeMode),
lastDismissedVersion: this.lastDismissedVersion.passThrough(lastDismissedVersion), lastDismissedVersion: this.lastDismissedVersion.passThrough(lastDismissedVersion),
machineId: this.machineId.passThrough(machineId), machineId: this.machineId.passThrough(machineId),
); );

View file

@ -1,5 +1,7 @@
import 'package:contacts_plus_plus/client_holder.dart'; import 'package:contacts_plus_plus/client_holder.dart';
import 'package:flutter/material.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:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
@ -27,6 +29,31 @@ class SettingsPage extends StatelessWidget {
initialState: !sClient.currentSettings.notificationsDenied.valueOrDefault, initialState: !sClient.currentSettings.notificationsDenied.valueOrDefault,
onChanged: (value) async => await sClient.changeSettings(sClient.currentSettings.copyWith(notificationsDenied: !value)), 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"), const ListSectionHeader(name: "Other"),
ListTile( ListTile(
trailing: const Icon(Icons.logout), trailing: const Icon(Icons.logout),