Theme Changer (Primitive?)
This commit is contained in:
@@ -1,9 +1,16 @@
|
|||||||
import 'package:flutter/material.dart';
|
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});
|
const Settings({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<Settings> createState() => _Settings();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Settings extends State<Settings> {
|
||||||
|
late String selectedTheme = 'Dark';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -13,7 +20,25 @@ class Settings extends StatelessWidget {
|
|||||||
|
|
||||||
body: ListView(
|
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);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,9 +6,18 @@ import 'package:witl/input_data.dart';
|
|||||||
|
|
||||||
void main() => runApp(const MyApp());
|
void main() => runApp(const MyApp());
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatefulWidget {
|
||||||
const MyApp({super.key});
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
@@ -26,11 +35,11 @@ class MyApp extends StatelessWidget {
|
|||||||
primaryColor: Colors.black,
|
primaryColor: Colors.black,
|
||||||
brightness: Brightness.dark,
|
brightness: Brightness.dark,
|
||||||
dividerColor: Colors.black12,
|
dividerColor: Colors.black12,
|
||||||
scaffoldBackgroundColor: Color(0xFF131313),
|
scaffoldBackgroundColor: const Color(0xFF131313),
|
||||||
|
|
||||||
),
|
),
|
||||||
|
|
||||||
themeMode: ThemeMode.system,
|
themeMode: _theme,
|
||||||
|
|
||||||
// Navigashun with Routes
|
// Navigashun with Routes
|
||||||
// Base "Home" Route
|
// 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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user