Theme Changer (Primitive?)

This commit is contained in:
2023-12-30 15:22:59 +02:00
parent fedc5df0c7
commit 75e21261dc
4 changed files with 54 additions and 79 deletions

View File

@@ -1,9 +1,16 @@
import 'package:flutter/material.dart';
import 'package:witl/theme_manager.dart';
import 'package:witl/main.dart';
class Settings extends StatelessWidget {
class Settings extends StatefulWidget {
const Settings({super.key});
@override
State<Settings> createState() => _Settings();
}
class _Settings extends State<Settings> {
late String selectedTheme = 'Dark';
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -12,8 +19,26 @@ class Settings extends StatelessWidget {
),
body: ListView(
children: [
children:[
ListTile(
title: const Text("App Theme"),
subtitle: const Text("Sets the theme of the app between: Dark, Light, System Default."),
trailing: DropdownButton<String> (
value: selectedTheme,
items: <String>["Light", "Dark", "System Default"].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? updatedTheme) {
setState(() {
selectedTheme = updatedTheme ?? selectedTheme;
MyApp.of(context).changeTheme(selectedTheme);
});
},
),
)
],
)
);

View File

@@ -6,9 +6,18 @@ import 'package:witl/input_data.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
static _MyAppState of(BuildContext context) => context.findAncestorStateOfType<_MyAppState>()!;
}
class _MyAppState extends State<MyApp> {
ThemeMode _theme = ThemeMode.system;
@override
Widget build(BuildContext context) {
return MaterialApp(
@@ -26,11 +35,11 @@ class MyApp extends StatelessWidget {
primaryColor: Colors.black,
brightness: Brightness.dark,
dividerColor: Colors.black12,
scaffoldBackgroundColor: Color(0xFF131313),
scaffoldBackgroundColor: const Color(0xFF131313),
),
themeMode: ThemeMode.system,
themeMode: _theme,
// Navigashun with Routes
// Base "Home" Route
@@ -52,4 +61,17 @@ class MyApp extends StatelessWidget {
},
);
}
void changeTheme(String newTheme) {
setState(() {
switch(newTheme) {
case "Light":
_theme = ThemeMode.light;
case "Dark":
_theme = ThemeMode.dark;
case "System Default":
_theme = ThemeMode.system;
}
});
}
}

View File

@@ -1,27 +0,0 @@
import 'package:shared_preferences/shared_preferences.dart';
class StorageManager {
static void saveData(String key, dynamic value) async {
final prefs = await SharedPreferences.getInstance();
if (value is int) {
prefs.setInt(key, value);
} else if (value is String) {
prefs.setString(key, value);
} else if (value is bool) {
prefs.setBool(key, value);
} else {
throw("Invalid Type (Shared Preference Error)");
}
}
static Future<dynamic> readData(String key) async {
final prefs = await SharedPreferences.getInstance();
dynamic obj = prefs.get(key);
return obj;
}
static Future<bool> deleteData(String key) async {
final prefs = await SharedPreferences.getInstance();
return prefs.remove(key);
}
}

View File

@@ -1,45 +0,0 @@
import 'package:flutter/material.dart';
import 'package:witl/storage_manager.dart';
class ThemeNotifier with ChangeNotifier {
final darkTheme = ThemeData(
primaryColor: Colors.black,
brightness: Brightness.dark,
dividerColor: Colors.black12, colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.grey).copyWith(background: const Color(0xFF212121)),
);
final lightTheme = ThemeData(
primaryColor: Colors.white,
brightness: Brightness.light,
dividerColor: Colors.white54, colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.grey).copyWith(background: const Color(0xFFE5E5E5)),
);
late ThemeData _themeData;
ThemeData getTheme() => _themeData;
ThemeNotifier() {
StorageManager.readData('themeMode').then((value) {
'value read from storage: ' + value.toString();
var themeMode = value ?? 'light';
if (themeMode == 'light') {
_themeData = lightTheme;
} else {
'setting dark theme';
_themeData = darkTheme;
}
notifyListeners();
});
}
void setDarkMode() async {
_themeData = darkTheme;
StorageManager.saveData('themeMode', 'dark');
notifyListeners();
}
void setLightMode() async {
_themeData = lightTheme;
StorageManager.saveData('themeMode', 'light');
notifyListeners();
}
}