-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,32 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:light_dark_theme_flutter/models/ThemeNotifier.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'screens/Home.dart'; | ||
|
||
void main() { | ||
runApp(MainWidget()); | ||
void main() async { | ||
WidgetsFlutterBinding | ||
.ensureInitialized(); //required to use platform channels to call native code. | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
bool themeBool = prefs.getBool("isDark") ?? false; //null check | ||
runApp( | ||
ChangeNotifierProvider( | ||
create: (BuildContext context) => ThemeProvider(isDark: themeBool), | ||
child: MainWidget(), | ||
), | ||
); | ||
} | ||
|
||
class MainWidget extends StatelessWidget { | ||
const MainWidget({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
title: 'Flutter Theme', | ||
home: HomeScreen(), | ||
routes: {}, | ||
); | ||
return Consumer<ThemeProvider>(builder: (context, themeProvider, child) { | ||
return MaterialApp( | ||
title: 'Flutter Theme', | ||
home: HomeScreen(), | ||
theme: themeProvider.getTheme, | ||
routes: {}, | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:flutter/material.dart'; | ||
import '/themes/Themes.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class ThemeProvider extends ChangeNotifier { | ||
late ThemeData _selectedTheme; | ||
late SharedPreferences prefs; | ||
ThemeProvider({bool isDark: false}) { | ||
this._selectedTheme = isDark ? darkTheme : lightTheme; | ||
} | ||
|
||
ThemeData get getTheme => _selectedTheme; | ||
|
||
Future<void> changeTheme() async { | ||
prefs = await SharedPreferences.getInstance(); | ||
|
||
if (_selectedTheme == darkTheme) { | ||
_selectedTheme = lightTheme; | ||
await prefs.setBool("isDark", false); | ||
} else { | ||
_selectedTheme = darkTheme; | ||
await prefs.setBool("isDark", true); | ||
} | ||
|
||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
//LightTheme | ||
ThemeData lightTheme = ThemeData( | ||
brightness: Brightness.light, | ||
primaryColor: Colors.white, | ||
textTheme: lightTextTheme, | ||
); | ||
|
||
TextStyle lightTextStyle = TextStyle( | ||
fontSize: 20, | ||
color: Colors.black, | ||
); | ||
TextTheme lightTextTheme = TextTheme( | ||
bodyText1: lightTextStyle, | ||
); | ||
|
||
//DarkTheme | ||
ThemeData darkTheme = ThemeData( | ||
brightness: Brightness.dark, | ||
primaryColor: Colors.black, | ||
); | ||
|
||
TextStyle darkTextStyle = TextStyle( | ||
fontSize: 20, | ||
color: Colors.white, | ||
); | ||
TextTheme darkTextTheme = TextTheme( | ||
bodyText1: lightTextStyle, | ||
); |