#687 enhancement theme-customization

This commit is contained in:
sangharshdeveloper
2025-01-12 20:48:31 +05:30
parent 9e410635a7
commit ced268660b
5 changed files with 50 additions and 2 deletions

View File

@@ -71,6 +71,7 @@ class MyApp extends StatelessWidget {
const MyApp(); // This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (ctx) => AuthProvider()),
@@ -140,7 +141,7 @@ class MyApp extends StatelessWidget {
darkTheme: wgerDarkTheme,
highContrastTheme: wgerLightThemeHc,
highContrastDarkTheme: wgerDarkThemeHc,
themeMode: ThemeMode.system,
themeMode: auth.themeMode, // Change theme based on provider's themeMode
home: auth.isAuth
? const HomeTabsScreen()
: FutureBuilder(

View File

@@ -22,6 +22,7 @@ import 'dart:developer';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'package:package_info_plus/package_info_plus.dart';
@@ -53,8 +54,15 @@ class AuthProvider with ChangeNotifier {
AuthProvider([http.Client? client, bool? checkMetadata]) {
this.client = client ?? http.Client();
_loadThemeMode();
}
bool _isSwitched = false;
ThemeMode _themeMode = ThemeMode.light;
bool get isSwitched => _isSwitched;
ThemeMode get themeMode => _themeMode;
/// flag to indicate that the application has successfully loaded all initial data
bool dataInit = false;
@@ -62,6 +70,27 @@ class AuthProvider with ChangeNotifier {
return token != null;
}
// Load theme mode from SharedPreferences
Future<void> _loadThemeMode() async {
final prefs = await SharedPreferences.getInstance();
final isDarkMode = prefs.getBool('isDarkMode') ?? false; // Default to false (light mode)
_themeMode = isDarkMode ? ThemeMode.dark : ThemeMode.light;
_isSwitched = isDarkMode;
notifyListeners(); // Notify listeners when theme is loaded
}
// Change mode on switch button click
void toggleSwitch(bool value) async {
_isSwitched = value;
_themeMode = value ? ThemeMode.dark : ThemeMode.light;
// Save to SharedPreferences
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('isDarkMode', _isSwitched);
notifyListeners(); // Notify listeners when the theme is updated
}
/// Server application version
Future<void> setServerVersion() async {
final response = await client.get(makeUri(serverUrl!, SERVER_VERSION_URL));

View File

@@ -29,6 +29,7 @@ class UserProvider with ChangeNotifier {
static const PROFILE_URL = 'userprofile';
static const VERIFY_EMAIL = 'verify-email';
Profile? profile;
/// Clear the current profile

View File

@@ -20,8 +20,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:wger/providers/auth.dart';
import 'package:wger/providers/exercises.dart';
import 'package:wger/providers/nutrition.dart';
import 'package:wger/providers/user.dart';
import 'package:wger/theme/theme.dart';
class SettingsPage extends StatelessWidget {
static String routeName = '/SettingsPage';
@@ -32,6 +36,7 @@ class SettingsPage extends StatelessWidget {
Widget build(BuildContext context) {
final exerciseProvider = Provider.of<ExercisesProvider>(context, listen: false);
final nutritionProvider = Provider.of<NutritionPlansProvider>(context, listen: false);
final switchProvider = Provider.of<AuthProvider>(context);
return Scaffold(
appBar: AppBar(
@@ -75,6 +80,18 @@ class SettingsPage extends StatelessWidget {
},
),
),
ListTile(
title: Text('Dark Mode'),
trailing: Switch(
value: switchProvider.isSwitched, // Use the state from the provider
onChanged: (value) {
switchProvider.toggleSwitch(value); // Update the state when the switch is toggled
},
activeColor: wgerPrimaryButtonColor, // Custom color when switch is on
inactiveThumbColor: wgerSecondaryColorLight, // Custom color when switch is off
inactiveTrackColor: wgerSecondaryColor, // Color of the track when the switch is off
),
),
],
),
);

View File

@@ -1583,5 +1583,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.6.0-0 <4.0.0"
dart: ">=3.6.0 <4.0.0"
flutter: ">=3.24.0"