From ac48172bb831825e9831b861389591b3fce7621d Mon Sep 17 00:00:00 2001 From: Watch your step! Date: Sun, 26 Mar 2023 18:57:16 +0900 Subject: [PATCH] feat: shared prefreference --- lib/main.dart | 16 ++-- lib/src/core/utils/sharedprefs_util.dart | 15 ++++ lib/src/screens/category/category_screen.dart | 27 +------ .../category/widgets/bottom_nav_button.dart | 33 ++++++++ .../category/{ => widgets}/category_card.dart | 0 .../screens/{ => history}/history_screen.dart | 0 lib/src/screens/home/home_screen.dart | 2 +- .../{ => widgets}/decibel_history_chart.dart | 0 .../{ => onboarding}/onboarding_screen.dart | 57 ++++++++------ macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 77 +++++++++++++++++++ pubspec.yaml | 1 + 12 files changed, 176 insertions(+), 54 deletions(-) create mode 100644 lib/src/core/utils/sharedprefs_util.dart create mode 100644 lib/src/screens/category/widgets/bottom_nav_button.dart rename lib/src/screens/category/{ => widgets}/category_card.dart (100%) rename lib/src/screens/{ => history}/history_screen.dart (100%) rename lib/src/screens/home/{ => widgets}/decibel_history_chart.dart (100%) rename lib/src/screens/{ => onboarding}/onboarding_screen.dart (62%) diff --git a/lib/main.dart b/lib/main.dart index 9500b8b..7ee7684 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 main() async { + WidgetsFlutterBinding.ensureInitialized(); + await SharedPreferencesUtil.initialize(); + final prefs = SharedPreferencesUtil().prefs; + isOnboard = prefs.getBool('isOnboard') ?? false; runApp(ProviderScope(child: const MyApp())); } @@ -21,7 +27,7 @@ class MyApp extends StatelessWidget { primarySwatch: Colors.blue, fontFamily: 'NotoSansKR', scaffoldBackgroundColor: Colors.white), - home: OnboardingScreen(), + home: isOnboard ? const HomseScreen() : const OnboardingScreen(), ); } } diff --git a/lib/src/core/utils/sharedprefs_util.dart b/lib/src/core/utils/sharedprefs_util.dart new file mode 100644 index 0000000..4d0d1b0 --- /dev/null +++ b/lib/src/core/utils/sharedprefs_util.dart @@ -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(); +} diff --git a/lib/src/screens/category/category_screen.dart b/lib/src/screens/category/category_screen.dart index d195eea..6fe4b16 100644 --- a/lib/src/screens/category/category_screen.dart +++ b/lib/src/screens/category/category_screen.dart @@ -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); @@ -103,28 +104,8 @@ class _CategoryScreenState extends State { ), ), ), - 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'), ); } } diff --git a/lib/src/screens/category/widgets/bottom_nav_button.dart b/lib/src/screens/category/widgets/bottom_nav_button.dart new file mode 100644 index 0000000..0a3ac7f --- /dev/null +++ b/lib/src/screens/category/widgets/bottom_nav_button.dart @@ -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, + ), + ), + ), + ); +} diff --git a/lib/src/screens/category/category_card.dart b/lib/src/screens/category/widgets/category_card.dart similarity index 100% rename from lib/src/screens/category/category_card.dart rename to lib/src/screens/category/widgets/category_card.dart diff --git a/lib/src/screens/history_screen.dart b/lib/src/screens/history/history_screen.dart similarity index 100% rename from lib/src/screens/history_screen.dart rename to lib/src/screens/history/history_screen.dart diff --git a/lib/src/screens/home/home_screen.dart b/lib/src/screens/home/home_screen.dart index af3b7c9..0cbe8a1 100644 --- a/lib/src/screens/home/home_screen.dart +++ b/lib/src/screens/home/home_screen.dart @@ -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'; diff --git a/lib/src/screens/home/decibel_history_chart.dart b/lib/src/screens/home/widgets/decibel_history_chart.dart similarity index 100% rename from lib/src/screens/home/decibel_history_chart.dart rename to lib/src/screens/home/widgets/decibel_history_chart.dart diff --git a/lib/src/screens/onboarding_screen.dart b/lib/src/screens/onboarding/onboarding_screen.dart similarity index 62% rename from lib/src/screens/onboarding_screen.dart rename to lib/src/screens/onboarding/onboarding_screen.dart index ab37838..cc1f7c7 100644 --- a/lib/src/screens/onboarding_screen.dart +++ b/lib/src/screens/onboarding/onboarding_screen.dart @@ -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((ref) => false); + +class OnboardingScreen extends ConsumerStatefulWidget { const OnboardingScreen({Key? key}) : super(key: key); @override - State createState() => _OnboardingScreenState(); + ConsumerState createState() => _OnboardingScreenState(); } -class _OnboardingScreenState extends State { +class _OnboardingScreenState extends ConsumerState { 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( @@ -38,6 +56,10 @@ class _OnboardingScreenState extends State { height: 10, ), TextField( + onChanged: (val) { + ref.read(validateNameProvider.notifier).state = + textEditingController.text.trim().isEmpty ? false : true; + }, style: const TextStyle( fontSize: 20, decoration: TextDecoration.none, @@ -55,28 +77,13 @@ class _OnboardingScreenState extends State { ), ), ), - 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'), ); } } diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 5922ad6..97e1b29 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -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")) } diff --git a/pubspec.lock b/pubspec.lock index 70acdc7..cf3074b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: @@ -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: @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index 3e550fb..0f29cf5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,6 +31,7 @@ dependencies: syncfusion_flutter_gauges: ^20.4.54 sqflite: ^2.2.6 path: ^1.8.2 + shared_preferences: ^2.0.20