diff --git a/unit2/lib/bloc/bloc/user_bloc.dart b/unit2/lib/bloc/bloc/user_bloc.dart index d6e8c26..7bd4eb5 100644 --- a/unit2/lib/bloc/bloc/user_bloc.dart +++ b/unit2/lib/bloc/bloc/user_bloc.dart @@ -14,6 +14,7 @@ part 'user_state.dart'; class UserBloc extends Bloc { UserData? _userData; + VersionInfo? _versionInfo; UserBloc() : super(UserInitial()) { // this event is called when opening the app to check if // there is new app version @@ -21,13 +22,17 @@ class UserBloc extends Bloc { try { emit(SplashScreen()); VersionInfo versionInfo = await AuthService.instance.getVersionInfo(); - emit(VersionLoaded(versionInfo: versionInfo)); + _versionInfo = versionInfo; + emit(VersionLoaded(versionInfo: _versionInfo)); } catch (e) { emit(UserError( message: e.toString(), )); } }); + on((event, emit) { + emit(VersionLoaded(versionInfo: _versionInfo)); + }); on((event, emit) async { try { UserData? userData = await AuthService.instance @@ -38,6 +43,16 @@ class UserBloc extends Bloc { emit(UserError(message: e.toString())); } }); + on((event, emit) async { + try { + UserData? userData = await AuthService.instance + .qrLogin(uuid: event.uuid, password: event.password); + _userData = userData; + emit(UserLoggedIn(userData: _userData)); + } catch (e) { + emit(UserError(message: e.toString())); + } + }); on((event, emit) { emit(UserLoggedIn(userData: _userData)); }); diff --git a/unit2/lib/bloc/bloc/user_event.dart b/unit2/lib/bloc/bloc/user_event.dart index 16c0a2f..6aca194 100644 --- a/unit2/lib/bloc/bloc/user_event.dart +++ b/unit2/lib/bloc/bloc/user_event.dart @@ -15,6 +15,8 @@ class UserLogin extends UserEvent { final String? username; final String? password; UserLogin({this.username, this.password}); + @override + List get props => [username!, password!]; } class LoadLoggedInUser extends UserEvent { @@ -25,3 +27,13 @@ class LoadLoggedInUser extends UserEvent { class GetUuid extends UserEvent { GetUuid(); } + +class LoadVersion extends UserEvent {} + +class UuidLogin extends UserEvent { + final String? uuid; + final String? password; + UuidLogin({this.uuid, this.password}); + @override + List get props => [uuid!, password!]; +} diff --git a/unit2/lib/screens/unit2/profile/profile.dart b/unit2/lib/screens/unit2/basic-info/basic-info.dart similarity index 98% rename from unit2/lib/screens/unit2/profile/profile.dart rename to unit2/lib/screens/unit2/basic-info/basic-info.dart index b0004f5..b6b7d3b 100644 --- a/unit2/lib/screens/unit2/profile/profile.dart +++ b/unit2/lib/screens/unit2/basic-info/basic-info.dart @@ -16,8 +16,8 @@ import '../../../theme-data.dart/colors.dart'; import '../../../widgets/splash_screen.dart'; import './components/cover-image.dart'; -class Profile extends StatelessWidget { - const Profile({super.key}); +class BasicInfo extends StatelessWidget { + const BasicInfo({super.key}); @override Widget build(BuildContext context) { diff --git a/unit2/lib/screens/unit2/profile/components/cover-image.dart b/unit2/lib/screens/unit2/basic-info/components/cover-image.dart similarity index 100% rename from unit2/lib/screens/unit2/profile/components/cover-image.dart rename to unit2/lib/screens/unit2/basic-info/components/cover-image.dart diff --git a/unit2/lib/screens/unit2/homepage.dart/components/drawer-screen.dart b/unit2/lib/screens/unit2/homepage.dart/components/drawer-screen.dart index 54a4405..c355f90 100644 --- a/unit2/lib/screens/unit2/homepage.dart/components/drawer-screen.dart +++ b/unit2/lib/screens/unit2/homepage.dart/components/drawer-screen.dart @@ -20,7 +20,6 @@ class _DrawerScreenState extends State { Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { - print("drawer screen" + state.toString()); if (state is UserLoggedIn) { return ZoomDrawer( controller: zoomDrawerController, diff --git a/unit2/lib/screens/unit2/homepage.dart/components/menu-screen.dart b/unit2/lib/screens/unit2/homepage.dart/components/menu-screen.dart index 1145405..c8d3ba4 100644 --- a/unit2/lib/screens/unit2/homepage.dart/components/menu-screen.dart +++ b/unit2/lib/screens/unit2/homepage.dart/components/menu-screen.dart @@ -52,13 +52,13 @@ class _MenuScreenState extends State { ), ), ), - getTile(FontAwesome5.user, "Basic Info", 'profile', context, + getTile(FontAwesome5.user, "Basic Info", '/basic-info', context, widget.userData!), const Divider(), getTile(FontAwesome5.user_circle, "Profile", - '/SelfAddressScreen', context, widget.userData!), + '/profile', context, widget.userData!), const Divider(), - getTile(FontAwesome5.life_ring, "Request SOS", 'request-sos', + getTile(FontAwesome5.life_ring, "Request SOS", '/request-sos', context, widget.userData!), const Divider(), Expanded( diff --git a/unit2/lib/screens/unit2/homepage.dart/components/menu.dart b/unit2/lib/screens/unit2/homepage.dart/components/menu.dart index 7c2544d..2a20438 100644 --- a/unit2/lib/screens/unit2/homepage.dart/components/menu.dart +++ b/unit2/lib/screens/unit2/homepage.dart/components/menu.dart @@ -23,7 +23,7 @@ Widget getTile( context.goNamed("login"); }); } else { - context.goNamed(route,extra: userData); + Navigator.pushNamed(context, route); } }, ); diff --git a/unit2/lib/screens/unit2/login/login.dart b/unit2/lib/screens/unit2/login/login.dart index ba6faa3..93b3a03 100644 --- a/unit2/lib/screens/unit2/login/login.dart +++ b/unit2/lib/screens/unit2/login/login.dart @@ -44,15 +44,12 @@ class _UniT2LoginState extends State { body: ProgressHUD( child: BlocConsumer(listener: (context, state) { if (state is UserLoggedIn) { - print("login" + state.toString()); final progress = ProgressHUD.of(context); progress!.dismiss(); - Navigator.pushReplacementNamed(context, '/module-screen'); + Navigator.pushReplacementNamed(context, '/module-screen'); } if (state is UuidLoaded) { - Navigator.push(context, MaterialPageRoute(builder: (context) { - return QRLogin(); - })); + Navigator.pushNamed(context, '/qr-login'); } }, builder: (context, state) { if (state is VersionLoaded) { @@ -126,8 +123,6 @@ class _UniT2LoginState extends State { name: 'password', validator: FormBuilderValidators.required( errorText: passwordRequired), - - // initialValue: state.password, onChanged: (value) { value!.isEmpty ? setState(() { diff --git a/unit2/lib/screens/unit2/login/qr_login.dart b/unit2/lib/screens/unit2/login/qr_login.dart index fbbd3ae..59b9512 100644 --- a/unit2/lib/screens/unit2/login/qr_login.dart +++ b/unit2/lib/screens/unit2/login/qr_login.dart @@ -4,12 +4,14 @@ import 'package:flutter/src/widgets/container.dart'; import 'package:flutter/src/widgets/framework.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; +import 'package:flutter_progress_hud/flutter_progress_hud.dart'; import 'package:flutter_svg/svg.dart'; import 'package:fluttericon/font_awesome5_icons.dart'; import 'package:fluttericon/font_awesome_icons.dart'; import 'package:form_builder_validators/form_builder_validators.dart'; import 'package:go_router/go_router.dart'; import 'package:unit2/theme-data.dart/btn-style.dart'; +import 'package:unit2/widgets/error_state.dart'; import 'package:unit2/widgets/wave.dart'; import '../../../bloc/bloc/user_bloc.dart'; @@ -32,206 +34,225 @@ class _QRLoginState extends State { final _formKey = GlobalKey(); @override Widget build(BuildContext context) { - return SafeArea( - child: Scaffold( - resizeToAvoidBottomInset: true, - appBar: AppBar( - backgroundColor: primary, - elevation: 0, - automaticallyImplyLeading: true, - title: const Text("Login via QR"), - centerTitle: true, - ), - body: BlocBuilder( - builder: (context, state) { - print(state); - if (state is UuidLoaded) { - return Center( - child: Text(state.uuid), - ); + return WillPopScope( + onWillPop: ()async{ + context.read().add(LoadVersion()); + return true; + }, + child: SafeArea( + child: Scaffold( + resizeToAvoidBottomInset: true, + appBar: AppBar( + backgroundColor: primary, + elevation: 0, + title: const Text("Login via QR"), + centerTitle: true, + ), + body: ProgressHUD( + child: BlocConsumer( + listener: (context,state){ + if (state is UserLoggedIn) { + final progress = ProgressHUD.of(context); + progress!.dismiss(); + Navigator.pushReplacementNamed(context, '/module-screen'); } - return SingleChildScrollView( - child: Stack( - children: [ - Positioned( - bottom: 0, - child: WaveReverse(height: blockSizeVertical * 8)), - Container( - height: screenHeight * .90, - padding: const EdgeInsets.symmetric(horizontal: 15), - child: FormBuilder( - key: _formKey, - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - SvgPicture.asset( - 'assets/svgs/logo.svg', - height: blockSizeVertical * 12, - allowDrawingOutsideViewBox: true, - color: primary, - ), - const SizedBox( - height: 12, - ), - Text(unitApp, - style: TextStyle( - fontSize: blockSizeVertical * 5, - fontWeight: FontWeight.w800, - letterSpacing: .2, - height: 1, - color: Colors.black87)), - const SizedBox( - height: 15, - ), - Text( - "Enter your password", - style: TextStyle( - fontSize: blockSizeVertical * 1.5, - height: 1.5, - fontWeight: FontWeight.w600), - ), - const SizedBox( - height: 15, - ), - // Password - FormBuilderTextField( - name: 'password', - validator: registerPasswordValidator, - // initialValue: state.password, - onChanged: (value) { - value!.isEmpty - ? setState(() { - showSuffixIcon = false; - }) - : setState(() { - showSuffixIcon = true; - }); - }, - autofocus: false, - style: const TextStyle( - fontWeight: FontWeight.bold, - color: Colors.black87), - decoration: loginTextFieldStyle().copyWith( - suffixIcon: Visibility( - visible: showSuffixIcon, - child: _showPassword - ? IconButton( - icon: Icon(FontAwesome5.eye_slash, - size: 24, - color: Theme.of(context) - .textTheme - .displayLarge - ?.color), - onPressed: () { - setState(() { - _showPassword = false; - }); - }, - ) - : IconButton( - onPressed: () { - setState(() { - _showPassword = true; - }); - }, - icon: Icon(FontAwesome5.eye, - size: 24, - color: Theme.of(context) - .textTheme - .displayLarge - ?.color)), + }, + builder: (context, state) { + if (state is UuidLoaded) { + return SingleChildScrollView( + child: Stack( + children: [ + Positioned( + bottom: 0, + child: WaveReverse(height: blockSizeVertical * 8)), + Container( + height: screenHeight * .90, + padding: const EdgeInsets.symmetric(horizontal: 15), + child: FormBuilder( + key: _formKey, + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SvgPicture.asset( + 'assets/svgs/logo.svg', + height: blockSizeVertical * 12, + allowDrawingOutsideViewBox: true, + color: primary, ), - prefixIcon: const Icon(Icons.lock), - labelText: "Password", - hintText: enterPassword), - obscureText: _showPassword ? true : false, + const SizedBox( + height: 12, + ), + Text(unitApp, + style: TextStyle( + fontSize: blockSizeVertical * 5, + fontWeight: FontWeight.w800, + letterSpacing: .2, + height: 1, + color: Colors.black87)), + const SizedBox( + height: 15, + ), + Text( + "Enter your password", + style: TextStyle( + fontSize: blockSizeVertical * 1.5, + height: 1.5, + fontWeight: FontWeight.w600), + ), + const SizedBox( + height: 15, + ), + // Password + FormBuilderTextField( + name: 'password', + validator: registerPasswordValidator, + // initialValue: state.password, + onChanged: (value) { + value!.isEmpty + ? setState(() { + showSuffixIcon = false; + }) + : setState(() { + showSuffixIcon = true; + }); + }, + autofocus: false, + style: const TextStyle( + fontWeight: FontWeight.bold, + color: Colors.black87), + decoration: loginTextFieldStyle().copyWith( + suffixIcon: Visibility( + visible: showSuffixIcon, + child: _showPassword + ? IconButton( + icon: Icon(FontAwesome5.eye_slash, + size: 24, + color: Theme.of(context) + .textTheme + .displayLarge + ?.color), + onPressed: () { + setState(() { + _showPassword = false; + }); + }, + ) + : IconButton( + onPressed: () { + setState(() { + _showPassword = true; + }); + }, + icon: Icon(FontAwesome5.eye, + size: 24, + color: Theme.of(context) + .textTheme + .displayLarge + ?.color)), + ), + prefixIcon: const Icon(Icons.lock), + labelText: "Password", + hintText: enterPassword), + obscureText: _showPassword ? true : false, + ), + const SizedBox( + height: 15, + ), + // FormBuilderTextField( + // name: 'confirmPassword', + // validator: registerPasswordValidator, + // // initialValue: state.password, + // onChanged: (value) { + // value!.isEmpty + // ? setState(() { + // showSuffixIcon = false; + // }) + // : setState(() { + // showSuffixIcon = true; + // }); + // }, + // autofocus: false, + // style: const TextStyle( + // fontWeight: FontWeight.bold, color: Colors.black87), + // decoration: loginTextFieldStyle().copyWith( + // suffixIcon: Visibility( + // visible: showSuffixIcon, + // child: _showPassword + // ? IconButton( + // icon: Icon(FontAwesome5.eye_slash, + // size: 24, + // color: Theme.of(context) + // .textTheme + // .displayLarge + // ?.color), + // onPressed: () { + // setState(() { + // _showPassword = false; + // }); + // }, + // ) + // : IconButton( + // onPressed: () { + // setState(() { + // _showPassword = true; + // }); + // }, + // icon: Icon(FontAwesome5.eye, + // size: 24, + // color: Theme.of(context) + // .textTheme + // .displayLarge + // ?.color)), + // ), + // prefixIcon: const Icon(Icons.lock), + // labelText: confirmPassword, + // hintText: enterConfirmPassword), + // obscureText: _showPassword ? true : false, + // ), + const SizedBox( + height: 15, + ), + SizedBox( + width: double.infinity, + height: blockSizeVertical * 6, + child: ElevatedButton( + style: secondaryBtnStyle( + second, Colors.transparent, primary), + onPressed: () { + if (_formKey.currentState! + .saveAndValidate()) { + final progress = + ProgressHUD.of(context); + progress?.showWithText( + 'Logging in...', + ); + context.read().add(UuidLogin(uuid: state.uuid,password: _formKey.currentState!.value['password'])); + // BlocProvider.of(context) + // .add(UserLogin( + // username: _formKey + // .currentState!.value['username'], + // password: _formKey + // .currentState!.value['password'], + // )); + } + }, + child: const Text(submit)), + ) + ], + ), ), - const SizedBox( - height: 15, - ), - // FormBuilderTextField( - // name: 'confirmPassword', - // validator: registerPasswordValidator, - // // initialValue: state.password, - // onChanged: (value) { - // value!.isEmpty - // ? setState(() { - // showSuffixIcon = false; - // }) - // : setState(() { - // showSuffixIcon = true; - // }); - // }, - // autofocus: false, - // style: const TextStyle( - // fontWeight: FontWeight.bold, color: Colors.black87), - // decoration: loginTextFieldStyle().copyWith( - // suffixIcon: Visibility( - // visible: showSuffixIcon, - // child: _showPassword - // ? IconButton( - // icon: Icon(FontAwesome5.eye_slash, - // size: 24, - // color: Theme.of(context) - // .textTheme - // .displayLarge - // ?.color), - // onPressed: () { - // setState(() { - // _showPassword = false; - // }); - // }, - // ) - // : IconButton( - // onPressed: () { - // setState(() { - // _showPassword = true; - // }); - // }, - // icon: Icon(FontAwesome5.eye, - // size: 24, - // color: Theme.of(context) - // .textTheme - // .displayLarge - // ?.color)), - // ), - // prefixIcon: const Icon(Icons.lock), - // labelText: confirmPassword, - // hintText: enterConfirmPassword), - // obscureText: _showPassword ? true : false, - // ), - const SizedBox( - height: 15, - ), - SizedBox( - width: double.infinity, - height: blockSizeVertical * 6, - child: ElevatedButton( - style: secondaryBtnStyle( - second, Colors.transparent, primary), - onPressed: () { - if (_formKey.currentState! - .saveAndValidate()) { - BlocProvider.of(context) - .add(UserLogin( - username: _formKey - .currentState!.value['username'], - password: _formKey - .currentState!.value['password'], - )); - } - }, - child: const Text(submit)), - ) - ], - ), + ), + ], ), - ), - ], - ), - ); - }, + ); + }if(state is UserError){ + return ErrorState(message: state.message,); + } + return Container(); + }, + ), + ), ), ), ); diff --git a/unit2/lib/sevices/login_service/auth_service.dart b/unit2/lib/sevices/login_service/auth_service.dart index c226746..82b47a5 100644 --- a/unit2/lib/sevices/login_service/auth_service.dart +++ b/unit2/lib/sevices/login_service/auth_service.dart @@ -58,4 +58,22 @@ class AuthService { } return userData!; } + Future qrLogin({String? uuid, String? password})async{ + Map body ={'uuid':uuid!,'password':password!}; + Map baseHeaders = { + 'Content-Type': 'application/json' + }; + String path = Url.instance.authentication(); + UserData? userData; + try{ + http.Response response = await Request.instance.postRequest(path: path,param: {},headers: baseHeaders,body: body); + if(response.statusCode == 200){ + Map data = jsonDecode(response.body); + userData = UserData.fromJson(data['data']); + } + }catch(e){ + throw (e.toString()); + } + return userData!; + } } diff --git a/unit2/lib/utils/app_router.dart b/unit2/lib/utils/app_router.dart index bfe2ba0..97750b2 100644 --- a/unit2/lib/utils/app_router.dart +++ b/unit2/lib/utils/app_router.dart @@ -1,11 +1,11 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:unit2/bloc/bloc/user_bloc.dart'; import 'package:unit2/screens/unit2/login/login.dart'; import 'package:unit2/utils/global_context.dart'; - +import '../screens/unit2/basic-info/basic-info.dart'; import '../screens/unit2/homepage.dart/components/drawer-screen.dart'; +import '../screens/unit2/login/qr_login.dart'; class AppRouter { Route onGenerateRoute(RouteSettings routeSettings) { @@ -18,11 +18,18 @@ class AppRouter { return const UniT2Login(); }); case '/module-screen': - BlocProvider.of( NavigationService.navigatorKey.currentContext!).add(LoadLoggedInUser()); + // BlocProvider.of( NavigationService.navigatorKey.currentContext!).add(LoadLoggedInUser()); return MaterialPageRoute(builder: (_) { return const DrawerScreen(); }); - + case '/basic-info': + return MaterialPageRoute(builder: (_){ + return const BasicInfo(); + }); + case '/qr-login': + return MaterialPageRoute(builder: (_){ + return const QRLogin(); + }); default: return MaterialPageRoute(builder: (context) { return Container();