Skip to content

Commit

Permalink
change to share pref
Browse files Browse the repository at this point in the history
  • Loading branch information
dungngminh committed Oct 9, 2021
1 parent bcc334b commit ac90722
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 46 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A new Flutter project.

## Getting Started

-

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:
Expand Down
23 changes: 23 additions & 0 deletions lib/core/utils/shared_pref.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:shared_preferences/shared_preferences.dart';

class SharedPrefUtils {
static setBoolKey(String key, bool value) async {
final SharedPreferences pref = await SharedPreferences.getInstance();
await pref.setBool(key, value);
}

static setStringKey(String key, String value) async {
final SharedPreferences pref = await SharedPreferences.getInstance();
await pref.setString(key, value);
}

static getBoolKey(String key) async {
final SharedPreferences pref = await SharedPreferences.getInstance();
return pref.getBool(key);
}

static getStringKey(String key) async {
final SharedPreferences pref = await SharedPreferences.getInstance();
return pref.getString(key);
}
}
4 changes: 0 additions & 4 deletions lib/data/services/user_api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

Expand Down Expand Up @@ -47,9 +46,6 @@ class UserApi {
print(response.body);
var result = convert.jsonDecode(response.body);
print("result " + result['token']);
var box = await Hive.openBox('box');
box.put('jwt', result['token']);
print(box.get('jwt'));
return true;
} else {
print(response.body);
Expand Down
17 changes: 8 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:get/get.dart';
import 'package:happy_care/core/utils/shared_pref.dart';
import 'package:happy_care/routes/app_pages.dart';
import 'package:hive_flutter/hive_flutter.dart';

Future<void> main() async {
await dotenv.load(fileName: "assets/.env");
await Hive.initFlutter();
await Hive.openBox('jwt');
print(Hive.box('jwt').get('jwt'));
runApp(MyApp());
var isNotFirstTime = await SharedPrefUtils.getBoolKey("first_time") ?? true;
runApp(MyApp(
isNotFirstTime: isNotFirstTime,
));
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
const MyApp({Key? key, required this.isNotFirstTime}) : super(key: key);
final bool isNotFirstTime;

@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: "Happy Care",
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.blue),
initialRoute: Hive.box('jwt').get('jwt') == null
? AppPages.initial
: AppPages.initial2,
initialRoute: isNotFirstTime ? AppRoutes.rOnboarding : AppRoutes.rSignIn,
getPages: AppPages.routes,
);
}
Expand Down
27 changes: 15 additions & 12 deletions lib/modules/onboarding/onboarding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:happy_care/core/themes/colors.dart';
import 'package:happy_care/core/utils/shared_pref.dart';
import 'package:happy_care/modules/onboarding/widget/onboarding_custom.dart';
import 'package:happy_care/routes/app_pages.dart';
import 'package:onboarding/onboarding.dart';
Expand Down Expand Up @@ -50,19 +51,21 @@ class OnboardingScreen extends StatelessWidget {
return Onboarding(
background: kBackgroundColor,
proceedButtonStyle: ProceedButtonStyle(
proceedButtonPadding:
EdgeInsets.symmetric(horizontal: 17.0, vertical: 1.7),
proceedButtonColor: kMainColor,
proceedpButtonText: Text(
"Đăng nhập",
style: GoogleFonts.openSans(
color: Colors.white,
fontSize: 16,
letterSpacing: 1.0,
proceedButtonPadding:
EdgeInsets.symmetric(horizontal: 17.0, vertical: 1.7),
proceedButtonColor: kMainColor,
proceedpButtonText: Text(
"Đăng nhập",
style: GoogleFonts.openSans(
color: Colors.white,
fontSize: 16,
letterSpacing: 1.0,
),
),
),
proceedButtonRoute: (context) => Get.offAndToNamed(AppRoutes.rSignIn),
),
proceedButtonRoute: (context) async {
await SharedPrefUtils.setBoolKey('first_time', false);
Get.offAndToNamed(AppRoutes.rSignIn);
}),
isSkippable: false,
pages: onboardingPagesList,
indicator: Indicator(
Expand Down
1 change: 0 additions & 1 deletion lib/modules/sign_in/sign_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class SignInScreen extends GetWidget<SignInController> {
),
Text(
"Happy Care".toUpperCase(),
//TODO: spacing of title
style: GoogleFonts.openSans(
color: kMainColor,
fontSize: 28,
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/sign_in/sign_in_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SignInController extends GetxController {
),
),
);
await Future.delayed(Duration(seconds: 1));
await Future.delayed(Duration(seconds: 2));
btnController.reset();
} else {
print(emailController.text);
Expand Down
3 changes: 1 addition & 2 deletions lib/routes/app_pages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import 'package:happy_care/modules/sign_up/sign_up_binding.dart';
part 'app_routes.dart';

class AppPages {
static const initial = AppRoutes.rSignIn;
static const initial2 = AppRoutes.rSignUp;
static const initial = AppRoutes.rOnboarding;
static final routes = [
GetPage(
name: AppRoutes.rOnboarding,
Expand Down
56 changes: 42 additions & 14 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
hive:
dependency: "direct main"
description:
name: hive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
hive_flutter:
dependency: "direct main"
description:
name: hive_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
http:
dependency: transitive
description:
Expand Down Expand Up @@ -366,6 +352,48 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.27.2"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ dependencies:
rounded_loading_button: ^2.0.9
flutter_dotenv: ^5.0.2
connectivity: ^3.0.6
hive: ^2.0.4
hive_flutter: ^1.1.0
shared_preferences: ^2.0.8


dev_dependencies:
flutter_test:
Expand Down
4 changes: 3 additions & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import 'package:happy_care/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(MyApp(
isNotFirstTime: true,
));

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit ac90722

Please sign in to comment.