Skip to content

Commit

Permalink
Merge pull request #42 from khushi-hura/main
Browse files Browse the repository at this point in the history
Fixes issue #28
  • Loading branch information
bhavik-mangla authored Jan 13, 2025
2 parents 3f5c19f + 8f4c58a commit 46febb0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
12 changes: 12 additions & 0 deletions lib/classes/themeProvider.dart
Original file line number Diff line number Diff line change
@@ -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();
}
}
18 changes: 14 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -37,6 +38,7 @@ void main() async {
ChangeNotifierProvider(
create: (_) => Global(),
),
ChangeNotifierProvider(create: (_) => ThemeProvider()),
],
child: const MyApp(),
),
Expand All @@ -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<ThemeProvider>(context);
return Consumer<ThemeProvider>(
builder: (context, themeProvider, child) {
return MaterialApp(
theme: lightTheme,
darkTheme: darkTheme,
themeMode: themeProvider.themeMode,
debugShowCheckedModeBanner: false,
onGenerateRoute: generateRoute,
initialRoute: '/',
);
}
);
}
}
Expand Down
87 changes: 86 additions & 1 deletion lib/pages/profile.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,6 +53,10 @@ class Profile extends StatefulWidget {
}

class _ProfileState extends State<Profile> {

//initial theme of the system
ThemeMode _themeMode = ThemeMode.light;

// TextEditingController for the name of the user
TextEditingController myName = TextEditingController();

Expand Down Expand Up @@ -69,13 +112,36 @@ class _ProfileState extends State<Profile> {
@override
void initState() {
super.initState();

_loadTheme();
// At the launch we are fetching details using the getDetails function
getDetails();
}

Future<void> _loadTheme() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int? themeIndex = prefs.getInt(themePreferenceKey);
if (themeIndex != null) {
setState(() {
_themeMode = ThemeMode.values[themeIndex];
});
}
}

Future<void> _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<ThemeProvider>(context);
return Scaffold(
appBar: AppBar(
title: const Text(
Expand Down Expand Up @@ -107,6 +173,25 @@ class _ProfileState extends State<Profile> {
},
),
),
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();
Expand Down

0 comments on commit 46febb0

Please sign in to comment.