diff --git a/lib/main.dart b/lib/main.dart index d7f3bd0..2f6bb02 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,13 +2,20 @@ import 'package:flutter/material.dart'; import 'package:adaptive_theme/adaptive_theme.dart'; import 'package:learn/landing_page.dart'; import 'package:learn/utils/route/routes.dart'; +import 'package:learn/theme_provider.dart'; +import 'package:provider/provider.dart'; DateTime? currentBackPressTime; void main() async { WidgetsFlutterBinding.ensureInitialized(); final savedThemeMode = await AdaptiveTheme.getThemeMode(); - runApp(MyApp(savedThemeMode: savedThemeMode)); + runApp( + ChangeNotifierProvider( + create: (context) => ThemeProvider(), + child: MyApp(savedThemeMode: savedThemeMode), + ), + ); } class MyApp extends StatelessWidget { @@ -18,12 +25,18 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return const MaterialApp( - debugShowCheckedModeBanner: false, - title: 'Learn', - themeMode: ThemeMode.system, - home: LandingPage(), - onGenerateRoute: Routers.generateRoute, + return Consumer( + builder: (context, themeProvider, child) { + return MaterialApp( + debugShowCheckedModeBanner: false, + title: 'Learn', + theme: ThemeData.light(), + darkTheme: ThemeData.dark(), + themeMode: themeProvider.themeMode, + home: const LandingPage(), + onGenerateRoute: Routers.generateRoute, + ); + }, ); } } \ No newline at end of file diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 3f70e28..9375deb 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -1,11 +1,11 @@ -// ignore_for_file: deprecated_member_use - import 'package:adaptive_theme/adaptive_theme.dart'; import 'package:flutter/material.dart'; import 'package:learn/utils/assets_path.dart'; import 'package:learn/utils/const_dimensions.dart'; import 'package:learn/utils/route/route_constant.dart'; +import 'package:provider/provider.dart'; import '../widgets/drawer.dart'; +import '../theme_provider.dart'; class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @@ -16,10 +16,10 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { final List _isImageClicked = List.generate(7, (index) => false); - bool _isDarkTheme = false; @override Widget build(BuildContext context) { + final themeProvider = Provider.of(context); return Scaffold( appBar: AppBar( title: const Text( @@ -31,17 +31,10 @@ class _MyHomePageState extends State { padding: const EdgeInsets.only(right: 16, top: 1), child: IconButton( icon: Icon( - _isDarkTheme ? Icons.dark_mode : Icons.light_mode, + themeProvider.themeMode == ThemeMode.dark ? Icons.dark_mode : Icons.light_mode, ), onPressed: () { - setState(() { - _isDarkTheme = !_isDarkTheme; - }); - final themeMode = - Theme.of(context).brightness == Brightness.dark - ? AdaptiveThemeMode.light - : AdaptiveThemeMode.dark; - AdaptiveTheme.of(context).setThemeMode(themeMode); + themeProvider.toggleTheme(); }, ), ), @@ -56,8 +49,7 @@ class _MyHomePageState extends State { context: context, title: "ALPHABETS", image: AssetsPath.getAlphabetImage(Alphabets.alphabets), - shortDescription: - "Learn A to Z with pronunciation and an example", + shortDescription: "Learn A to Z with pronunciation and an example", route: AllRoutesConstant.atozRoute, index: 0, ), @@ -79,8 +71,7 @@ class _MyHomePageState extends State { context: context, title: "BODY PARTS", image: AssetsPath.getBodyImage(Body.body), - shortDescription: - "Know about body parts and their pronunciation.", + shortDescription: "Know about body parts and their pronunciation.", route: AllRoutesConstant.partsRoute, index: 2, ), @@ -124,8 +115,7 @@ class _MyHomePageState extends State { context: context, title: "FRUITS & VEGETABLES", image: 'assets/fruitsVeges/cover.jpg', - shortDescription: - "Explore and learn about Fruits and Vegetables!", + shortDescription: "Explore and learn about Fruits and Vegetables!", route: AllRoutesConstant.fruitRoute, index: 6, ), diff --git a/lib/theme_provider.dart b/lib/theme_provider.dart new file mode 100644 index 0000000..6523b4e --- /dev/null +++ b/lib/theme_provider.dart @@ -0,0 +1,12 @@ +import 'package:flutter/material.dart'; + +class ThemeProvider with ChangeNotifier { + ThemeMode _themeMode = ThemeMode.light; + + ThemeMode get themeMode => _themeMode; + + void toggleTheme() { + _themeMode = _themeMode == ThemeMode.light ? ThemeMode.dark : ThemeMode.light; + notifyListeners(); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index d679592..7df9b05 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -51,6 +51,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter + provider: ^6.0.3 # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is