diff --git a/lib/classes/themeProvider.dart b/lib/classes/themeProvider.dart new file mode 100644 index 0000000..6004f51 --- /dev/null +++ b/lib/classes/themeProvider.dart @@ -0,0 +1,12 @@ +import 'package:flutter/material.dart'; + +class ThemeProvider extends ChangeNotifier { + ThemeMode themeMode = ThemeMode.light; + + ThemeMode getThemeMode() => themeMode; + + void toggleTheme(bool isDarkMode) { + themeMode = isDarkMode ? ThemeMode.dark : ThemeMode.light; + notifyListeners(); + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 67ce4fc..bf66331 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,6 +7,7 @@ import 'package:provider/provider.dart'; import 'classes/global.dart'; import 'encyption/key_storage.dart'; import 'encyption/rsa.dart'; +import 'classes/themeProvider.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -37,6 +38,7 @@ void main() async { ChangeNotifierProvider( create: (_) => Global(), ), + ChangeNotifierProvider(create: (_) => ThemeProvider()), ], child: const MyApp(), ), @@ -48,10 +50,18 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return const MaterialApp( - debugShowCheckedModeBanner: false, - onGenerateRoute: generateRoute, - initialRoute: '/', + final themeProvider = Provider.of(context); + return Consumer( + builder: (context, themeProvider, child) { + return MaterialApp( + theme: lightTheme, + darkTheme: darkTheme, + themeMode: themeProvider.themeMode, + debugShowCheckedModeBanner: false, + onGenerateRoute: generateRoute, + initialRoute: '/', + ); + } ); } } diff --git a/lib/pages/profile.dart b/lib/pages/profile.dart index 6e344e7..417c371 100644 --- a/lib/pages/profile.dart +++ b/lib/pages/profile.dart @@ -1,10 +1,49 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter_nearby_connections_example/classes/themeProvider.dart'; +import 'package:provider/provider.dart'; import 'home_screen.dart'; import 'package:nanoid/nanoid.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../classes/global.dart'; +const String themePreferenceKey = 'themePreference'; + +final ThemeData lightTheme = ThemeData( + brightness: Brightness.light, + primaryColor: Colors.blue, + scaffoldBackgroundColor: Colors.white, + textTheme: TextTheme( + displayLarge: TextStyle( + fontSize: 24.0, + fontWeight: FontWeight.bold, + color: Colors.black, + ), + bodyLarge: TextStyle( + fontSize: 16.0, + color: Colors.black87, + ), + ), +); + +final ThemeData darkTheme = ThemeData( + brightness: Brightness.dark, + primaryColor: Colors.grey[900], + hintColor: Colors.blueAccent, + scaffoldBackgroundColor: Colors.grey[850], + textTheme: TextTheme( + displayLarge: TextStyle( + fontSize: 24.0, + fontWeight: FontWeight.bold, + color: Colors.white, + ), + bodyLarge: TextStyle( + fontSize: 16.0, + color: Colors.white70, + ), + ), +); + class Profile extends StatefulWidget { final bool onLogin; @@ -14,6 +53,10 @@ class Profile extends StatefulWidget { } class _ProfileState extends State { + + //initial theme of the system + ThemeMode _themeMode = ThemeMode.light; + // TextEditingController for the name of the user TextEditingController myName = TextEditingController(); @@ -69,13 +112,36 @@ class _ProfileState extends State { @override void initState() { super.initState(); - + _loadTheme(); // At the launch we are fetching details using the getDetails function getDetails(); } + Future _loadTheme() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + int? themeIndex = prefs.getInt(themePreferenceKey); + if (themeIndex != null) { + setState(() { + _themeMode = ThemeMode.values[themeIndex]; + }); + } + } + + Future _saveTheme(ThemeMode mode) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + await prefs.setInt(themePreferenceKey, mode.index); + } + + void _toggleTheme(bool value) { + setState(() { + _themeMode = value ? ThemeMode.dark : ThemeMode.light; + }); + _saveTheme(_themeMode); + } + @override Widget build(BuildContext context) { + final themeProvider = Provider.of(context); return Scaffold( appBar: AppBar( title: const Text( @@ -107,6 +173,25 @@ class _ProfileState extends State { }, ), ), + Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + Text( + 'Switch to dark theme', + style: const TextStyle( + fontSize: 20, + ), + ), + Switch( + value: themeProvider.themeMode == ThemeMode.dark, + onChanged: (value) { + themeProvider.toggleTheme(value); + }, + activeColor: Colors.blueAccent, + inactiveThumbColor: Colors.grey, + ), + ], + ), ElevatedButton( onPressed: () async { final prefs = await SharedPreferences.getInstance();