Skip to content

Commit

Permalink
feat: shared prefreference
Browse files Browse the repository at this point in the history
  • Loading branch information
watchstep committed Mar 26, 2023
1 parent 5f0a689 commit ac48172
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 54 deletions.
16 changes: 11 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:see_our_sounds/src/screens/category/category_card.dart';
import 'package:see_our_sounds/src/screens/category/category_screen.dart';
import 'package:see_our_sounds/src/core/utils/sharedprefs_util.dart';
import 'package:see_our_sounds/src/screens/home/home_screen.dart';
import 'package:see_our_sounds/src/screens/onboarding_screen.dart';

void main() {
import 'package:see_our_sounds/src/screens/onboarding/onboarding_screen.dart';

late bool isOnboard;

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await SharedPreferencesUtil.initialize();
final prefs = SharedPreferencesUtil().prefs;
isOnboard = prefs.getBool('isOnboard') ?? false;
runApp(ProviderScope(child: const MyApp()));
}

Expand All @@ -21,7 +27,7 @@ class MyApp extends StatelessWidget {
primarySwatch: Colors.blue,
fontFamily: 'NotoSansKR',
scaffoldBackgroundColor: Colors.white),
home: OnboardingScreen(),
home: isOnboard ? const HomseScreen() : const OnboardingScreen(),
);
}
}
15 changes: 15 additions & 0 deletions lib/src/core/utils/sharedprefs_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:shared_preferences/shared_preferences.dart';

class SharedPreferencesUtil {
static late final SharedPreferences _prefs;

SharedPreferences get prefs => _prefs;
static final SharedPreferencesUtil _prefsUtil =
SharedPreferencesUtil._internal();

SharedPreferencesUtil._internal();

factory SharedPreferencesUtil() => _prefsUtil;

static initialize() async => _prefs = await SharedPreferences.getInstance();
}
27 changes: 4 additions & 23 deletions lib/src/screens/category/category_screen.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:see_our_sounds/src/core/app_constants.dart';
import 'package:see_our_sounds/src/screens/category/category_card.dart';
import 'package:see_our_sounds/src/screens/category/widgets/category_card.dart';
import 'package:see_our_sounds/src/screens/category/widgets/bottom_nav_button.dart';

class CategoryScreen extends StatefulWidget {
const CategoryScreen({Key? key}) : super(key: key);
Expand Down Expand Up @@ -103,28 +104,8 @@ class _CategoryScreenState extends State<CategoryScreen> {
),
),
),
bottomNavigationBar: Container(
width: double.infinity,
height: 55,
margin: const EdgeInsets.fromLTRB(20, 0, 20, 20),
decoration: BoxDecoration(
color: AppColor.primaryColor,
borderRadius: const BorderRadius.all(Radius.circular(15)),
boxShadow: [
BoxShadow(
color: Colors.grey.shade100, blurRadius: 2, spreadRadius: 1)
]),
child: const Center(
child: Text(
'Done',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
textAlign: TextAlign.center,
),
),
),
bottomNavigationBar:
bottomNavButton(onTap: () {}, validate: false, text: 'Done'),
);
}
}
33 changes: 33 additions & 0 deletions lib/src/screens/category/widgets/bottom_nav_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:flutter/material.dart';
import 'package:see_our_sounds/src/core/app_constants.dart';

Widget bottomNavButton(
{required VoidCallback onTap,
required bool validate,
required String text}) {
return GestureDetector(
onTap: onTap,
child: Container(
width: double.infinity,
height: 55,
margin: const EdgeInsets.fromLTRB(20, 0, 20, 20),
decoration: BoxDecoration(
color: validate ? AppColor.primaryColor : AppColor.lightGrayColor,
borderRadius: const BorderRadius.all(Radius.circular(15)),
boxShadow: [
BoxShadow(
color: Colors.grey.shade100, blurRadius: 2, spreadRadius: 1)
]),
child: Center(
child: Text(
text,
style: const TextStyle(
color: Colors.white,
fontSize: 16,
),
textAlign: TextAlign.center,
),
),
),
);
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/src/screens/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:see_our_sounds/src/providers/audio_tagging_api_provider.dart';
import 'package:see_our_sounds/src/providers/audio_tagging_db_provider.dart';
import 'package:see_our_sounds/src/providers/decibel_provider.dart';
import 'package:see_our_sounds/src/providers/stt_provider.dart';
import 'package:see_our_sounds/src/screens/home/decibel_history_chart.dart';
import 'package:see_our_sounds/src/screens/home/widgets/decibel_history_chart.dart';
import 'package:see_our_sounds/src/screens/home/widgets/toggle_button.dart';

import 'package:syncfusion_flutter_gauges/gauges.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:see_our_sounds/src/core/utils/sharedprefs_util.dart';
import 'package:see_our_sounds/src/core/app_constants.dart';
import 'package:see_our_sounds/src/screens/category/widgets/bottom_nav_button.dart';

class OnboardingScreen extends StatefulWidget {
final validateNameProvider = StateProvider<bool>((ref) => false);

class OnboardingScreen extends ConsumerStatefulWidget {
const OnboardingScreen({Key? key}) : super(key: key);

@override
State<OnboardingScreen> createState() => _OnboardingScreenState();
ConsumerState<OnboardingScreen> createState() => _OnboardingScreenState();
}

class _OnboardingScreenState extends State<OnboardingScreen> {
class _OnboardingScreenState extends ConsumerState<OnboardingScreen> {
TextEditingController textEditingController = TextEditingController();
final _prefs = SharedPreferencesUtil().prefs;

@override
void initState() {
super.initState();
}

@override
void dispose() {
textEditingController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
bool validateName = ref.watch(validateNameProvider);
return Scaffold(
body: SafeArea(
child: Padding(
Expand All @@ -38,6 +56,10 @@ class _OnboardingScreenState extends State<OnboardingScreen> {
height: 10,
),
TextField(
onChanged: (val) {
ref.read(validateNameProvider.notifier).state =
textEditingController.text.trim().isEmpty ? false : true;
},
style: const TextStyle(
fontSize: 20,
decoration: TextDecoration.none,
Expand All @@ -55,28 +77,13 @@ class _OnboardingScreenState extends State<OnboardingScreen> {
),
),
),
bottomNavigationBar: Container(
width: double.infinity,
height: 55,
margin: const EdgeInsets.fromLTRB(20, 0, 20, 20),
decoration: BoxDecoration(
color: AppColor.primaryColor,
borderRadius: const BorderRadius.all(Radius.circular(15)),
boxShadow: [
BoxShadow(
color: Colors.grey.shade100, blurRadius: 2, spreadRadius: 1)
]),
child: const Center(
child: Text(
'Next',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
textAlign: TextAlign.center,
),
),
),
bottomNavigationBar: bottomNavButton(
onTap: () {
_prefs.setString('userName', textEditingController.text);
_prefs.setBool('isOnboard', true);
},
validate: validateName,
text: 'Next'),
);
}
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import FlutterMacOS
import Foundation

import flutter_local_notifications
import shared_preferences_foundation
import speech_to_text_macos
import sqflite

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SpeechToTextMacosPlugin.register(with: registry.registrar(forPlugin: "SpeechToTextMacosPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
}
77 changes: 77 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.10"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
pedantic:
dependency: transitive
description:
Expand Down Expand Up @@ -527,6 +548,55 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.2"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.20"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.17"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
shelf:
dependency: transitive
description:
Expand Down Expand Up @@ -721,6 +791,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
xdg_directories:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
syncfusion_flutter_gauges: ^20.4.54
sqflite: ^2.2.6
path: ^1.8.2
shared_preferences: ^2.0.20



Expand Down

0 comments on commit ac48172

Please sign in to comment.