diff --git a/lib/main.dart b/lib/main.dart index ac4abfe1..4a03a517 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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( diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index 1bfbdca1..a58b06d2 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -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 _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 setServerVersion() async { final response = await client.get(makeUri(serverUrl!, SERVER_VERSION_URL)); diff --git a/lib/providers/user.dart b/lib/providers/user.dart index cb547c49..7e1e8870 100644 --- a/lib/providers/user.dart +++ b/lib/providers/user.dart @@ -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 diff --git a/lib/widgets/core/settings.dart b/lib/widgets/core/settings.dart index 78c8dfa9..941a199d 100644 --- a/lib/widgets/core/settings.dart +++ b/lib/widgets/core/settings.dart @@ -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(context, listen: false); final nutritionProvider = Provider.of(context, listen: false); + final switchProvider = Provider.of(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 + ), + ), ], ), ); diff --git a/pubspec.lock b/pubspec.lock index e65a68f9..d21ed048 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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"