Skip to content

Commit

Permalink
Add theme setting (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
EwuUwe authored Mar 11, 2024
1 parent 9f8ff2b commit 3568586
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 12 deletions.
44 changes: 34 additions & 10 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:iyox_wormhole/pages/router.dart';
import 'package:flutter/services.dart';
import 'package:iyox_wormhole/utils/settings.dart';
import 'package:logger/logger.dart';
import 'package:path_provider/path_provider.dart';

Expand Down Expand Up @@ -79,10 +80,17 @@ class WormholeAppState extends State<WormholeApp> with WidgetsBindingObserver {
initBackend();
initApp().then((_) => debugPrint("App Init Completed"));

var brightness = WidgetsBinding.instance.platformDispatcher.platformBrightness;
setState(() {
themeMode =
brightness == Brightness.dark ? ThemeMode.dark : ThemeMode.light;
var brightness =
WidgetsBinding.instance.platformDispatcher.platformBrightness;
getCurrentAppTheme().then((value) {
setState(() {
if (value == ThemeMode.system) {
themeMode =
brightness == Brightness.dark ? ThemeMode.dark : ThemeMode.light;
} else {
themeMode = value;
}
});
});
}

Expand All @@ -96,14 +104,26 @@ class WormholeAppState extends State<WormholeApp> with WidgetsBindingObserver {
void didChangePlatformBrightness() {
super.didChangePlatformBrightness();

_setTheme();
setTheme();
}

Future<ThemeMode> getCurrentAppTheme() async {
return await Settings.getThemeMode();
}

void _setTheme() {
void setTheme() {
var brightness = View.of(context).platformDispatcher.platformBrightness;
setState(() {
themeMode =
getCurrentAppTheme().then((value) {
debugPrint(value.toString());

setState(() {
if (value == ThemeMode.system) {
themeMode =
brightness == Brightness.dark ? ThemeMode.dark : ThemeMode.light;
} else {
themeMode = value;
}
});
});
}

Expand All @@ -116,8 +136,12 @@ class WormholeAppState extends State<WormholeApp> with WidgetsBindingObserver {
Widget build(BuildContext context) {
return DynamicColorBuilder(builder: (lightColorScheme, darkColorScheme) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: themeMode == ThemeMode.dark ? darkColorScheme?.background: lightColorScheme?.background,
systemNavigationBarColor: themeMode == ThemeMode.dark ? darkColorScheme?.surface: lightColorScheme?.surface));
statusBarColor: themeMode == ThemeMode.dark
? darkColorScheme?.background
: lightColorScheme?.background,
systemNavigationBarColor: themeMode == ThemeMode.dark
? darkColorScheme?.surface
: lightColorScheme?.surface));
return MaterialApp(
navigatorKey: NavigationService.navigatorKey,
debugShowCheckedModeBanner: false,
Expand Down
111 changes: 111 additions & 0 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:icons_launcher/cli_commands.dart';
import 'package:iyox_wormhole/utils/settings.dart';
import 'package:iyox_wormhole/widgets/settings_field.dart';
import 'package:iyox_wormhole/widgets/settings_header.dart';
import 'package:restart_app/restart_app.dart';

class SettingsPage extends StatefulWidget {
const SettingsPage({Key? key}) : super(key: key);
Expand Down Expand Up @@ -102,6 +104,115 @@ class _SettingsPageState extends State<SettingsPage> {
height: 0.0,
);
}),
const SettingsHeader("Appearance"),
FutureBuilder(
future: Settings.getThemeMode(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return FilledButton.tonal(
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
shape: MaterialStateProperty.all(LinearBorder.none),
backgroundColor: MaterialStateProperty.all(
Theme.of(context).colorScheme.background),
textStyle: MaterialStateProperty.all(
TextStyle(color: Theme.of(context).colorScheme.primary),
),
),
onPressed: () {
showDialog(
context: context,
builder: (context) {
ThemeMode theme = snapshot.data!;
return StatefulBuilder(builder: (context, setState) {
return AlertDialog(
title: const Text("Theme"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
for (var value in ThemeMode.values)
RadioListTile(
title: Text(value
.toString()
.split('.')
.last
.capitalize()),
value: value,
groupValue: theme,
onChanged: (value) {
setState(() {
theme = value!;
});
}),
],
),
actions: [
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context)
.textTheme
.labelLarge,
),
child: const Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context)
.textTheme
.labelLarge,
),
child: const Text('Save'),
onPressed: () {
this.setState(() {
Settings.setThemeMode(theme);
Restart.restartApp();
});
Navigator.of(context).pop();
},
),
]);
});
});
},
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 17, 20, 5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
"Theme",
style: TextStyle(
fontSize: 20,
color: Theme.of(context)
.colorScheme
.onBackground),
),
Text(
snapshot.data!
.toString()
.split('.')
.last
.capitalize(),
style: TextStyle(
fontSize: 15,
color: Theme.of(context)
.colorScheme
.onBackground),
)
]))),
);
}
return const SizedBox(
width: 0.0,
height: 0.0,
);
}),
const SettingsHeader("Connection"),
FutureBuilder(
future: Settings.getRendezvousUrl(),
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Settings {
static const _rendezvousUrl = 'RENDEZVOUS_URL';
static const _transitUrl = 'TRANSIT_URL';
static const _recentFiles = 'RECENT_FILES';
static const _themeMode = 'THEME_MODE';

static setWordLength(int? value) async {
await _setField(value, _wordLength);
Expand Down Expand Up @@ -45,6 +46,10 @@ class Settings {
await _setField(recentFiles, _recentFiles);
}

static setThemeMode(ThemeMode value) async {
await _setField(value.toString(), _themeMode);
}

static Future<int> getWordLength() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt(_wordLength) ?? 3;
Expand All @@ -65,6 +70,12 @@ class Settings {
return prefs.getStringList(_recentFiles) ?? [];
}

static Future<ThemeMode> getThemeMode() async {
final prefs = await SharedPreferences.getInstance();
final themeModeString = prefs.getString(_themeMode) ?? 'ThemeMode.system';
return ThemeMode.values.firstWhere((e) => e.toString() == themeModeString);
}

static _setField<T>(T? value, String field) async {
final prefs = await SharedPreferences.getInstance();
if (value == null) {
Expand Down
5 changes: 3 additions & 2 deletions lib/widgets/settings_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ class SettingField extends StatefulWidget {
{super.key,
required this.title,
required this.initialValue,
required this.onSubmit});
required this.onSubmit, this.editWidget});

final String title;
final String initialValue;
final void Function(String) onSubmit;
final Widget? editWidget;

@override
State<SettingField> createState() => _SettingFieldState();
Expand Down Expand Up @@ -67,7 +68,7 @@ class _SettingFieldState extends State<SettingField> {
context: context,
builder: (context) => AlertDialog(
title: Text(widget.title),
content: TextField(
content: widget.editWidget ?? TextField(
controller: _textController,
onSubmitted: widget.onSubmit,
),
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.2.1"
restart_app:
dependency: "direct main"
description:
name: restart_app
sha256: b37daeb1c02fcab30e19d9e30b6fdd215bd53577efd927042eb77cf6f09daadb
url: "https://pub.dev"
source: hosted
version: "1.2.1"
share_handler:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies:
shared_preferences: ^2.2.2
share_handler: ^0.0.17
icons_launcher: ^2.1.7
restart_app: ^1.2.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 3568586

Please sign in to comment.