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

Created Favorite Page #196

Merged
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
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