Skip to content

Commit

Permalink
Merge pull request #196 from VITianLalit/implement_favourites_page
Browse files Browse the repository at this point in the history
 Created Favorite Page
  • Loading branch information
sapatevaibhav authored Jul 3, 2024
2 parents 003750d + f91825a commit 286264f
Show file tree
Hide file tree
Showing 4 changed files with 895 additions and 302 deletions.
51 changes: 51 additions & 0 deletions lib/favorite_page_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class FavouriteScreenProvider with ChangeNotifier {
bool _drawingBoard = false;
bool get drawingBoard => _drawingBoard;

List<int> _selectedItemList = [];

List<int> get selectedItemList => _selectedItemList;

FavouriteScreenProvider() {
loadFromPrefs();
}

void setDrawingBoard() {
_drawingBoard = !_drawingBoard;
notifyListeners();
saveToPrefs();
}

void setList(int item) {
_selectedItemList.add(item);
notifyListeners();
saveToPrefs();
}

void removeList(int item) {
_selectedItemList.remove(item);
notifyListeners();
saveToPrefs();
}

Future<void> loadFromPrefs() async {
final prefs = await SharedPreferences.getInstance();
_drawingBoard = prefs.getBool('drawingBoard') ?? false;
_selectedItemList = prefs
.getStringList('selectedItemList')
?.map((e) => int.parse(e))
.toList() ??
[];
notifyListeners();
}

Future<void> saveToPrefs() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('drawingBoard', _drawingBoard);
await prefs.setStringList('selectedItemList',
_selectedItemList.map((e) => e.toString()).toList());
}
}
26 changes: 17 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:learn/favorite_page_provider.dart';
import 'package:learn/landing_page.dart';
import 'package:learn/pages/main_home.dart';
import 'package:learn/utils/route/routes.dart';
Expand All @@ -9,20 +10,25 @@ import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

DateTime? currentBackPressTime;
bool visitedGettingStartedPageOnceBool=false; //to store the value of
bool visitedGettingStartedPageOnceBool = false; //to store the value of

void main() async {
WidgetsFlutterBinding.ensureInitialized();
final savedThemeMode = await AdaptiveTheme.getThemeMode();
final prefs = await SharedPreferences.getInstance();
visitedGettingStartedPageOnceBool = prefs.getBool('visitedGettingStartedPageOnce')??false; //if its is null i.e first time then set to false
visitedGettingStartedPageOnceBool =
prefs.getBool('visitedGettingStartedPageOnce') ??
false; //if its is null i.e first time then set to false

runApp(
ChangeNotifierProvider(
create: (context) => ThemeProvider(),
child: MyApp(savedThemeMode: savedThemeMode),
),
);
runApp(MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => ThemeProvider(),
),
ChangeNotifierProvider(create: (context) => FavouriteScreenProvider()),
],
child: MyApp(savedThemeMode: savedThemeMode),
));
}

class MyApp extends StatelessWidget {
Expand All @@ -40,7 +46,9 @@ class MyApp extends StatelessWidget {
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: themeProvider.themeMode,
home: visitedGettingStartedPageOnceBool? const MainHome(): const LandingPage(), //if page opened for first time , show landing page (getting started page), else show home page
home: visitedGettingStartedPageOnceBool
? const MainHome()
: const LandingPage(), //if page opened for first time , show landing page (getting started page), else show home page
onGenerateRoute: Routers.generateRoute,
);
},
Expand Down
Loading

0 comments on commit 286264f

Please sign in to comment.