From 4c888a235d6ef210c95a26fc8cc6e440f6f09299 Mon Sep 17 00:00:00 2001 From: davidpanonce-nx Date: Thu, 12 Sep 2024 11:04:36 +0800 Subject: [PATCH 1/2] refactor(splash) : Adjusted splash page and utilized flutter hooks --- .../my_candidates_municipal_tab.dart | 2 +- .../my_candidates_national_tab.dart | 2 +- .../my_candidates_provincial_tab.dart | 2 +- .../my_candidate_data_controller.dart | 2 +- lib/core/routes/routing.dart | 2 +- lib/main_common.dart | 2 +- lib/views/candidates/candidate_selection.dart | 2 +- lib/views/dashboard_wrapper.dart | 2 +- lib/views/{ => splash}/splash.dart | 89 ++++--------------- lib/views/splash/use_splash_page.dart | 38 ++++++++ lib/views/veripol_candidates_wrapper.dart | 2 +- pubspec.yaml | 2 +- 12 files changed, 66 insertions(+), 81 deletions(-) rename lib/views/{ => splash}/splash.dart (62%) create mode 100644 lib/views/splash/use_splash_page.dart diff --git a/lib/components/my_candidates_municipal_tab.dart b/lib/components/my_candidates_municipal_tab.dart index 4d4c802..f31a351 100644 --- a/lib/components/my_candidates_municipal_tab.dart +++ b/lib/components/my_candidates_municipal_tab.dart @@ -6,7 +6,7 @@ import '../controller/candidate_data_controller.dart'; import '../controller/data_controller.dart'; import '../controller/pagination_controllers.dart'; import '../views/candidates/add_candidate.dart'; -import '../views/splash.dart'; +import '../views/splash/splash.dart'; import 'dialog_boxes.dart'; import 'my_candidate_add_button.dart'; import 'my_candidate_selected_card.dart'; diff --git a/lib/components/my_candidates_national_tab.dart b/lib/components/my_candidates_national_tab.dart index ef31a55..c7d97f4 100644 --- a/lib/components/my_candidates_national_tab.dart +++ b/lib/components/my_candidates_national_tab.dart @@ -9,7 +9,7 @@ import 'package:veripol/views/candidates/add_candidate.dart'; import '../controller/candidate_data_controller.dart'; import '../controller/data_controller.dart'; import '../controller/pagination_controllers.dart'; -import '../views/splash.dart'; +import '../views/splash/splash.dart'; import 'themes.dart'; class MyCandidatesNationalTab extends StatefulWidget { diff --git a/lib/components/my_candidates_provincial_tab.dart b/lib/components/my_candidates_provincial_tab.dart index c8bc781..704fa96 100644 --- a/lib/components/my_candidates_provincial_tab.dart +++ b/lib/components/my_candidates_provincial_tab.dart @@ -6,7 +6,7 @@ import '../controller/candidate_data_controller.dart'; import '../controller/data_controller.dart'; import '../controller/pagination_controllers.dart'; import '../views/candidates/add_candidate.dart'; -import '../views/splash.dart'; +import '../views/splash/splash.dart'; import 'dialog_boxes.dart'; import 'my_candidate_add_button.dart'; import 'my_candidate_selected_card.dart'; diff --git a/lib/controller/my_candidate_data_controller.dart b/lib/controller/my_candidate_data_controller.dart index c1bff4a..9071c8c 100644 --- a/lib/controller/my_candidate_data_controller.dart +++ b/lib/controller/my_candidate_data_controller.dart @@ -14,7 +14,7 @@ import '../components/my_candidate_selected_card.dart'; import '../components/themes.dart'; import '../models/models.dart'; import '../views/candidates/add_candidate.dart'; -import '../views/splash.dart'; +import '../views/splash/splash.dart'; import 'pagination_controllers.dart'; class MyCandidatesDataController extends ChangeNotifier { diff --git a/lib/core/routes/routing.dart b/lib/core/routes/routing.dart index 554032d..2cf7b50 100644 --- a/lib/core/routes/routing.dart +++ b/lib/core/routes/routing.dart @@ -5,7 +5,7 @@ import 'package:veripol/views/authentication/sign_in.dart'; import 'package:veripol/views/authentication/sign_up1.dart'; import 'package:veripol/views/onboarding/onboarding_1.dart'; import 'package:veripol/views/signup_dashboard.dart'; -import 'package:veripol/views/splash.dart'; +import 'package:veripol/views/splash/splash.dart'; class Routing { static final router = GoRouter( diff --git a/lib/main_common.dart b/lib/main_common.dart index af9fc00..7696687 100644 --- a/lib/main_common.dart +++ b/lib/main_common.dart @@ -10,7 +10,7 @@ import 'package:veripol/firebase_options.dart'; import 'package:veripol/utils/flavors.dart'; import 'package:veripol/views/dashboard_wrapper.dart'; import 'package:veripol/views/signup_dashboard.dart'; -import 'package:veripol/views/splash.dart'; +import 'package:veripol/views/splash/splash.dart'; import 'controller/candidate_data_controller.dart'; import 'controller/data_controller.dart'; diff --git a/lib/views/candidates/candidate_selection.dart b/lib/views/candidates/candidate_selection.dart index 0dbdcb9..a2e9500 100644 --- a/lib/views/candidates/candidate_selection.dart +++ b/lib/views/candidates/candidate_selection.dart @@ -5,7 +5,7 @@ import 'package:veripol/controller/candidate_data_controller.dart'; import 'package:veripol/controller/pagination_controllers.dart'; import 'package:veripol/views/candidates/candidates_list.dart'; import 'package:veripol/views/candidates/position_details.dart'; -import 'package:veripol/views/splash.dart'; +import 'package:veripol/views/splash/splash.dart'; import '../../components/candidate_type_card_nav.dart'; import '../../components/themes.dart'; diff --git a/lib/views/dashboard_wrapper.dart b/lib/views/dashboard_wrapper.dart index 2c241ef..6de6455 100644 --- a/lib/views/dashboard_wrapper.dart +++ b/lib/views/dashboard_wrapper.dart @@ -5,7 +5,7 @@ import 'package:provider/provider.dart'; import 'package:veripol/controller/data_controller.dart'; import 'package:veripol/controller/page_controllers.dart'; import 'package:veripol/models/models.dart'; -import 'package:veripol/views/splash.dart'; +import 'package:veripol/views/splash/splash.dart'; import '../components/themes.dart'; import '../controller/my_candidate_data_controller.dart'; diff --git a/lib/views/splash.dart b/lib/views/splash/splash.dart similarity index 62% rename from lib/views/splash.dart rename to lib/views/splash/splash.dart index d7a250e..f6fdad6 100644 --- a/lib/views/splash.dart +++ b/lib/views/splash/splash.dart @@ -1,101 +1,48 @@ import 'dart:math'; import 'package:flutter/material.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:go_router/go_router.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:veripol/components/themes.dart'; +import 'package:veripol/core/extensions/build_extensions.dart'; import 'package:veripol/core/routes/routes.dart'; -class VeripolSplash extends StatefulWidget { - const VeripolSplash({super.key}); - - @override - State createState() => _VeripolSplashState(); -} - -class _VeripolSplashState extends State with TickerProviderStateMixin { - late AnimationController _breathingAnimation; - late final Animation _breathe = Tween( - begin: 0.8, - end: 1.3, - ).animate( - CurvedAnimation( - parent: _breathingAnimation, - curve: Curves.easeOut, - ), - ); - - @override - void initState() { - _breathingAnimation = AnimationController( - vsync: this, - duration: const Duration(milliseconds: 500), - ); - - WidgetsBinding.instance.addPostFrameCallback((timeStamp) async => await _checkFirstInstall()); +part 'use_splash_page.dart'; - super.initState(); - } - - Future _checkFirstInstall() async { - await Future.delayed(const Duration(seconds: 2)); - SharedPreferences prefs = await SharedPreferences.getInstance(); - final firstInstall = prefs.getBool('firstInstall') ?? true; - if (mounted) { - if (firstInstall) { - context.pushReplacement(Routes.onboarding); - prefs.setBool('firstInstall', false); - } else { - context.pushReplacement(Routes.authHome); - } - } - } - - @override - void dispose() { - _breathingAnimation.dispose(); - super.dispose(); - } +class VeripolSplash extends HookWidget { + const VeripolSplash({super.key}); @override Widget build(BuildContext context) { - final size = MediaQuery.of(context).size; - + final animation = useSplashPage(); return Scaffold( body: SizedBox( - width: size.width, - height: size.height, + width: context.screenWidth, + height: context.screenHeight, child: Stack( + alignment: Alignment.center, children: [ Image.asset( 'assets/bg_pattern.png', ), - Positioned( - left: 134, - top: 307, - child: AnimatedBuilder( - animation: _breathingAnimation..forward(), - builder: (BuildContext context, Widget? child) { - return Transform.scale( - scale: _breathe.value, - child: Image.asset( - 'assets/veripol_logo.png', - ), - ); - }, + Transform.scale( + scale: animation, + child: Image.asset( + 'assets/veripol_logo.png', ), ), const Positioned( - left: 221, + left: 300, top: -261, - child: StackedBoxes(), + child: _StackedBoxes(), ), Positioned( left: -195, top: 565, child: Transform.rotate( angle: pi, - child: const StackedBoxes(), + child: const _StackedBoxes(), ), ), ], @@ -105,8 +52,8 @@ class _VeripolSplashState extends State with TickerProviderStateM } } -class StackedBoxes extends StatelessWidget { - const StackedBoxes({super.key}); +class _StackedBoxes extends StatelessWidget { + const _StackedBoxes(); @override Widget build(BuildContext context) { diff --git a/lib/views/splash/use_splash_page.dart b/lib/views/splash/use_splash_page.dart new file mode 100644 index 0000000..9799b32 --- /dev/null +++ b/lib/views/splash/use_splash_page.dart @@ -0,0 +1,38 @@ +part of 'splash.dart'; + +double useSplashPage() { + final context = useContext(); + // Using Flutter Hook to manage the breathing animation controller + final breathingAnimation = useAnimationController( + duration: const Duration(milliseconds: 500), + )..forward(); + + final breathe = useAnimation(Tween(begin: 0.8, end: 1.5).animate( + CurvedAnimation(parent: breathingAnimation, curve: Curves.easeOut), + )); + + Future checkFirstInstall(BuildContext context) async { + await Future.delayed(const Duration(seconds: 2)); + SharedPreferences prefs = await SharedPreferences.getInstance(); + final firstInstall = prefs.getBool('firstInstall') ?? true; + + if (context.mounted) { + if (firstInstall) { + context.pushReplacement(Routes.onboarding); + prefs.setBool('firstInstall', false); + } else { + context.pushReplacement(Routes.authHome); + } + } + } + + useEffect(() { + // Trigger the first-install check after the first frame. + WidgetsBinding.instance.addPostFrameCallback((_) async { + await checkFirstInstall(context); + }); + return null; + }, []); + + return breathe; +} diff --git a/lib/views/veripol_candidates_wrapper.dart b/lib/views/veripol_candidates_wrapper.dart index 104c1f7..3177daf 100644 --- a/lib/views/veripol_candidates_wrapper.dart +++ b/lib/views/veripol_candidates_wrapper.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:provider/provider.dart'; import 'package:veripol/views/registration%20location/registered_voter.dart'; -import 'package:veripol/views/splash.dart'; +import 'package:veripol/views/splash/splash.dart'; import '../controller/data_controller.dart'; import '../controller/my_candidate_data_controller.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 2e25347..39df85f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ dependencies: firebase_core: 3.4.1 flutter: sdk: flutter - flutter_hooks: 0.20.5 + flutter_hooks: ^0.20.5 flutter_launcher_icons: 0.13.1 flutter_svg: 2.0.10+1 go_router: 14.2.7 From aaeaeb3141904d6e8e4569609f6b54c8b93a73d3 Mon Sep 17 00:00:00 2001 From: davidpanonce-nx Date: Thu, 12 Sep 2024 11:08:18 +0800 Subject: [PATCH 2/2] Update flutter hooks package --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 39df85f..2e25347 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ dependencies: firebase_core: 3.4.1 flutter: sdk: flutter - flutter_hooks: ^0.20.5 + flutter_hooks: 0.20.5 flutter_launcher_icons: 0.13.1 flutter_svg: 2.0.10+1 go_router: 14.2.7