Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added dark mode feature #168

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<ThemeProvider>(
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,
);
},
);
}
}
26 changes: 8 additions & 18 deletions lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -16,10 +16,10 @@ class MyHomePage extends StatefulWidget {

class _MyHomePageState extends State<MyHomePage> {
final List<bool> _isImageClicked = List.generate(7, (index) => false);
bool _isDarkTheme = false;

@override
Widget build(BuildContext context) {
final themeProvider = Provider.of<ThemeProvider>(context);
return Scaffold(
appBar: AppBar(
title: const Text(
Expand All @@ -31,17 +31,10 @@ class _MyHomePageState extends State<MyHomePage> {
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();
},
),
),
Expand All @@ -56,8 +49,7 @@ class _MyHomePageState extends State<MyHomePage> {
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,
),
Expand All @@ -79,8 +71,7 @@ class _MyHomePageState extends State<MyHomePage> {
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,
),
Expand Down Expand Up @@ -124,8 +115,7 @@ class _MyHomePageState extends State<MyHomePage> {
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,
),
Expand Down
12 changes: 12 additions & 0 deletions lib/theme_provider.dart
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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
Expand Down