From efc93fc61970ba9d810f9b339a3fbdaac52608af Mon Sep 17 00:00:00 2001 From: Mohamed Ali Date: Sat, 23 Dec 2023 00:32:22 +0200 Subject: [PATCH] addresses --- lib/core/app/app.dart | 16 +-- lib/core/router/app_router.dart | 8 +- .../screens/add_edit_address.dart | 109 +++++++++++------- lib/presentation/screens/addresses.dart | 17 ++- lib/presentation/screens/checkout.dart | 2 +- lib/presentation/screens/login.dart | 66 ++++++----- lib/presentation/screens/profile.dart | 2 +- lib/presentation/screens/signup.dart | 78 +++++++------ 8 files changed, 171 insertions(+), 127 deletions(-) diff --git a/lib/core/app/app.dart b/lib/core/app/app.dart index debed82..69af8ec 100644 --- a/lib/core/app/app.dart +++ b/lib/core/app/app.dart @@ -55,6 +55,7 @@ class MyApp extends StatelessWidget { create: (context) => di.sl(), ), BlocProvider( + lazy: false, create: (context) => di.sl()..fetchDeliveryInfo(), ), @@ -67,8 +68,8 @@ class MyApp extends StatelessWidget { BlocProvider( create: (context) => di.sl(), ), - BlocProvider( - lazy: false, + BlocProvider( + lazy: false, create: (context) => di.sl()..init(), ), ], @@ -96,12 +97,11 @@ class MyApp extends StatelessWidget { ), ), outlinedButtonTheme: OutlinedButtonThemeData( - style: OutlinedButton.styleFrom( - shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.zero, - ), - ) - ), + style: OutlinedButton.styleFrom( + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.zero, + ), + )), iconTheme: const IconThemeData(color: AppColors.CommonCyan, size: 30), floatingActionButtonTheme: const FloatingActionButtonThemeData( backgroundColor: AppColors.CommonCyan, diff --git a/lib/core/router/app_router.dart b/lib/core/router/app_router.dart index 459571a..6b5dc70 100644 --- a/lib/core/router/app_router.dart +++ b/lib/core/router/app_router.dart @@ -32,7 +32,7 @@ sealed class AppRouter { static const String filter = '/filter'; static const String signup = '/signup'; static const String login = '/login'; - static const String adress = '/adress'; + static const String addresses = '/addresses'; static const String addadress = '/addadress'; static const String checkout = '/checkout'; static const String contact = '/contact'; @@ -68,12 +68,12 @@ sealed class AppRouter { return MaterialPageRoute(builder: (_) => ContactScreen()); case cart: return MaterialPageRoute(builder: (_) => const CartScreen()); - case adress: - return MaterialPageRoute(builder: (_) => const AdressScreen()); + case addresses: + return MaterialPageRoute(builder: (_) => const AddressesScreen()); case addadress: DeliveryInfo? deliveryInfo = routeSettings.arguments as DeliveryInfo?; return MaterialPageRoute( - builder: (_) => AddAdressScreen( + builder: (_) => AddAddressScreen( deliveryInfo: deliveryInfo, )); case checkout: diff --git a/lib/presentation/screens/add_edit_address.dart b/lib/presentation/screens/add_edit_address.dart index 6a02f7d..78553f3 100644 --- a/lib/presentation/screens/add_edit_address.dart +++ b/lib/presentation/screens/add_edit_address.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:flutter_phoenix/flutter_phoenix.dart'; import 'package:piiicks/configs/configs.dart'; +import 'package:piiicks/core/router/app_router.dart'; import 'package:piiicks/presentation/widgets/custom_appbar.dart'; import 'package:piiicks/presentation/widgets/mobile_number_textfield.dart'; import 'package:piiicks/presentation/widgets/textfield_toptext.dart'; @@ -9,49 +9,60 @@ import 'package:piiicks/presentation/widgets/textfield_toptext.dart'; import '../../application/delivery_info_action_cubit/delivery_info_action_cubit.dart'; import '../../application/delivery_info_fetch_cubit/delivery_info_fetch_cubit.dart'; import '../../application/notifications_cubit/notifications_cubit.dart'; -import '../../core/router/app_router.dart'; import '../../data/models/delivery/delivery_info_model.dart'; import '../../domain/entities/delivery/delivery_info.dart'; import '../widgets/custom_textfield.dart'; -class AddAdressScreen extends StatefulWidget { +class AddAddressScreen extends StatefulWidget { final DeliveryInfo? deliveryInfo; - const AddAdressScreen({ + const AddAddressScreen({ super.key, this.deliveryInfo, }); @override - State createState() => _AddAdressScreenState(); + State createState() => _AddAddressScreenState(); } -class _AddAdressScreenState extends State { +class _AddAddressScreenState extends State { String? id; - final TextEditingController firstName = TextEditingController(); - final TextEditingController lastName = TextEditingController(); - final TextEditingController addressLineOne = TextEditingController(); - final TextEditingController addressLineTwo = TextEditingController(); - final TextEditingController city = TextEditingController(); - final TextEditingController zipCode = TextEditingController(); - final TextEditingController contactNumber = TextEditingController(); + final TextEditingController _firstName = TextEditingController(); + final TextEditingController _lastName = TextEditingController(); + final TextEditingController _addressLineOne = TextEditingController(); + final TextEditingController _addressLineTwo = TextEditingController(); + final TextEditingController _city = TextEditingController(); + final TextEditingController _zipCode = TextEditingController(); + final TextEditingController _contactNumber = TextEditingController(); final _formKey = GlobalKey(); @override void initState() { if (widget.deliveryInfo != null) { id = widget.deliveryInfo!.id; - firstName.text = widget.deliveryInfo!.firstName; - lastName.text = widget.deliveryInfo!.lastName; - addressLineOne.text = widget.deliveryInfo!.addressLineOne; - addressLineTwo.text = widget.deliveryInfo!.addressLineTwo; - city.text = widget.deliveryInfo!.city; - zipCode.text = widget.deliveryInfo!.zipCode; - contactNumber.text = widget.deliveryInfo!.contactNumber; + _firstName.text = widget.deliveryInfo!.firstName; + _lastName.text = widget.deliveryInfo!.lastName; + _addressLineOne.text = widget.deliveryInfo!.addressLineOne; + _addressLineTwo.text = widget.deliveryInfo!.addressLineTwo; + _city.text = widget.deliveryInfo!.city; + _zipCode.text = widget.deliveryInfo!.zipCode; + _contactNumber.text = widget.deliveryInfo!.contactNumber; } super.initState(); } + @override + void dispose() { + _firstName.dispose(); + _lastName.dispose(); + _addressLineOne.dispose(); + _addressLineTwo.dispose(); + _city.dispose(); + _zipCode.dispose(); + _contactNumber.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -81,19 +92,19 @@ class _AddAdressScreenState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ TextFieldTopText("First Name*"), - buildTextFormField(firstName, 'First name'), + buildTextFormField(_firstName, 'First name'), TextFieldTopText("Last name*"), - buildTextFormField(lastName, 'Last name'), + buildTextFormField(_lastName, 'Last name'), TextFieldTopText("Line One*"), - buildTextFormField(addressLineOne, 'Address line one'), + buildTextFormField(_addressLineOne, 'Address line one'), TextFieldTopText("Line Two*"), - buildTextFormField(addressLineTwo, 'Address line two'), + buildTextFormField(_addressLineTwo, 'Address line two'), TextFieldTopText("City*"), - buildTextFormField(city, 'City'), + buildTextFormField(_city, 'City'), TextFieldTopText("Zip Code*"), - buildTextFormField(zipCode, 'Zip code'), + buildTextFormField(_zipCode, 'Zip code'), TextFieldTopText("Contact number*"), - MobileNumberTextField(contactNumber, 'Contact number'), + MobileNumberTextField(_contactNumber, 'Contact number'), Space.yf(2), SizedBox( width: double.infinity, @@ -105,37 +116,49 @@ class _AddAdressScreenState extends State { .read() .addDeliveryInfo(DeliveryInfoModel( id: '', - firstName: firstName.text, - lastName: lastName.text, - addressLineOne: addressLineOne.text, - addressLineTwo: addressLineTwo.text, - city: city.text, - zipCode: zipCode.text, - contactNumber: contactNumber.text, + firstName: _firstName.text, + lastName: _lastName.text, + addressLineOne: _addressLineOne.text, + addressLineTwo: _addressLineTwo.text, + city: _city.text, + zipCode: _zipCode.text, + contactNumber: _contactNumber.text, )); context .read() .showAndSaveNotification("Addresses Update", "Congratulations, You have successfully updated your Address Book."); - Phoenix.rebirth(context); + // Phoenix.rebirth(context); + context + .read() + .fetchDeliveryInfo(); + Navigator.pop(context); + + Navigator.of(context) + .popAndPushNamed(AppRouter.addresses); } else { context .read() .editDeliveryInfo(DeliveryInfoModel( id: id!, - firstName: firstName.text, - lastName: lastName.text, - addressLineOne: addressLineOne.text, - addressLineTwo: addressLineTwo.text, - city: city.text, - zipCode: zipCode.text, - contactNumber: contactNumber.text, + firstName: _firstName.text, + lastName: _lastName.text, + addressLineOne: _addressLineOne.text, + addressLineTwo: _addressLineTwo.text, + city: _city.text, + zipCode: _zipCode.text, + contactNumber: _contactNumber.text, )); context .read() .showAndSaveNotification("Addresses Update", "Congratulations, You have successfully updated your Address Book."); - Phoenix.rebirth(context); + // Phoenix.rebirth(context); + context + .read() + .fetchDeliveryInfo(); + Navigator.pop(context); + // Navigator.of(context).popAndPushNamed(AppRouter.addresses); } } }, diff --git a/lib/presentation/screens/addresses.dart b/lib/presentation/screens/addresses.dart index fc1c93d..84255aa 100644 --- a/lib/presentation/screens/addresses.dart +++ b/lib/presentation/screens/addresses.dart @@ -10,14 +10,20 @@ import '../../application/delivery_info_action_cubit/delivery_info_action_cubit. import '../../application/delivery_info_fetch_cubit/delivery_info_fetch_cubit.dart'; import '../widgets/adress_card.dart'; -class AdressScreen extends StatefulWidget { - const AdressScreen({Key? key}) : super(key: key); +class AddressesScreen extends StatefulWidget { + const AddressesScreen({Key? key}) : super(key: key); @override - State createState() => _AdressScreenState(); + State createState() => _AddressesScreenState(); } -class _AdressScreenState extends State { +class _AddressesScreenState extends State { + @override + void initState() { + context.read().fetchDeliveryInfo(); + super.initState(); + } + @override Widget build(BuildContext context) { return BlocListener( @@ -33,7 +39,8 @@ class _AdressScreenState extends State { } }, child: Scaffold( - appBar: CustomAppBar("ADD ADDRESS", context,automaticallyImplyLeading: true), + appBar: CustomAppBar("ADD ADDRESS", context, + automaticallyImplyLeading: true), body: Padding( padding: Space.all(1.2, 1), child: Column( diff --git a/lib/presentation/screens/checkout.dart b/lib/presentation/screens/checkout.dart index 244c6a3..a5ac43c 100644 --- a/lib/presentation/screens/checkout.dart +++ b/lib/presentation/screens/checkout.dart @@ -72,7 +72,7 @@ class _CheckOutScreenState extends State { context: context, onTap: () { Navigator.of(context) - .pushNamed(AppRouter.adress); + .pushNamed(AppRouter.addresses); }, buttonText: "Add Address") ], diff --git a/lib/presentation/screens/login.dart b/lib/presentation/screens/login.dart index 0443317..3534f11 100644 --- a/lib/presentation/screens/login.dart +++ b/lib/presentation/screens/login.dart @@ -28,7 +28,7 @@ class _LoginScreenState extends State { final TextEditingController _passwordController = TextEditingController(); final _formKey = GlobalKey(); - bool isLoading = false; +/* bool isLoading = false; void _startLoading() { setState(() { isLoading = true; @@ -39,10 +39,16 @@ class _LoginScreenState extends State { isLoading = false; }); }); + }*/ + + @override + void dispose() { + _emailController.dispose(); + _passwordController.dispose(); + super.dispose(); } @override Widget build(BuildContext context) { - return Scaffold( appBar: CustomAppBar("LOGIN", context, automaticallyImplyLeading: true), body: SingleChildScrollView( @@ -88,13 +94,9 @@ class _LoginScreenState extends State { ], ), Space.yf(1.7), - BlocListener( + BlocConsumer( listener: (context, state) { - if (state is UserLoading) { - setState(() { - _startLoading(); - }); - } else if (state is UserLogged) { + if (state is UserLogged) { showSuccessfulAuthDialog(context, "logged in"); } else if (state is UserLoggedFail) { if (state.failure is CredentialFailure) { @@ -104,32 +106,34 @@ class _LoginScreenState extends State { } } }, - child: ElevatedButton( - onPressed: () { - if (_formKey.currentState!.validate()) { - context.read().add( - SignInUser( - SignInParams( - username: _emailController.text, - password: _passwordController.text, + builder: (context, state) { + return ElevatedButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + context.read().add( + SignInUser( + SignInParams( + username: _emailController.text, + password: _passwordController.text, + ), ), - ), - ); - } - }, - style: ButtonStyle( - minimumSize: MaterialStatePropertyAll( - Size( - double.infinity, - AppDimensions.normalize(20), + ); + } + }, + style: ButtonStyle( + minimumSize: MaterialStatePropertyAll( + Size( + double.infinity, + AppDimensions.normalize(20), + ), ), ), - ), - child: Text( - isLoading == true ? "Wait..." : "Login", - style: AppText.h3b?.copyWith(color: Colors.white), - ), - ), + child: Text( + (state is UserLoading) ? "Wait..." : "Login", + style: AppText.h3b?.copyWith(color: Colors.white), + ), + ); + }, ), Space.yf(1.5), Center( diff --git a/lib/presentation/screens/profile.dart b/lib/presentation/screens/profile.dart index 6eefaa3..fd17503 100644 --- a/lib/presentation/screens/profile.dart +++ b/lib/presentation/screens/profile.dart @@ -87,7 +87,7 @@ class ProfileScreen extends StatelessWidget { GestureDetector( onTap: () { Navigator.of(context) - .pushNamed(AppRouter.adress); + .pushNamed(AppRouter.addresses); }, child: Row( mainAxisAlignment: diff --git a/lib/presentation/screens/signup.dart b/lib/presentation/screens/signup.dart index 555a794..f502138 100644 --- a/lib/presentation/screens/signup.dart +++ b/lib/presentation/screens/signup.dart @@ -31,10 +31,10 @@ class _SignUpScreenState extends State { TextEditingController(); final _formKey = GlobalKey(); - bool isLoading = false; +// bool isLoading = false; bool isChecked = false; - void _startLoading() { + /* void _startLoading() { setState(() { isLoading = true; }); @@ -44,7 +44,16 @@ class _SignUpScreenState extends State { isLoading = false; }); }); + }*/ + @override + void dispose() { + _nameController.dispose(); + _emailController.dispose(); + _passwordController.dispose(); + _confirmPasswordController.dispose(); + super.dispose(); } + @override Widget build(BuildContext context) { return Scaffold( @@ -128,13 +137,9 @@ class _SignUpScreenState extends State { ], ), Space.yf(1.5), - BlocListener( + BlocConsumer( listener: (context, state) { - if (state is UserLoading) { - setState(() { - _startLoading(); - }); - } else if (state is UserLogged) { + if (state is UserLogged) { showSuccessfulAuthDialog(context, "Registered"); } else if (state is UserLoggedFail) { if (state.failure is CredentialFailure) { @@ -144,35 +149,40 @@ class _SignUpScreenState extends State { } } }, - child: ElevatedButton( - onPressed: () { - if (_formKey.currentState!.validate()) { - if (_passwordController.text != - _confirmPasswordController.text) { - } else { - context.read().add(SignUpUser(SignUpParams( - firstName: _nameController.text, - lastName: _nameController.text, - email: _emailController.text, - password: _passwordController.text, - ))); + builder: (context, state) { + return ElevatedButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + if (_passwordController.text != + _confirmPasswordController.text) { + } else { + context.read().add( + SignUpUser( + SignUpParams( + firstName: _nameController.text, + lastName: _nameController.text, + email: _emailController.text, + password: _passwordController.text, + ), + ), + ); + } } - } - }, - style: ButtonStyle( - minimumSize: MaterialStatePropertyAll( - Size( - double.infinity, - AppDimensions.normalize(20), + }, + style: ButtonStyle( + minimumSize: MaterialStatePropertyAll( + Size( + double.infinity, + AppDimensions.normalize(20), + ), ), ), - ), - child: - Text( - isLoading == true ? "Wait..." : "Signup", - style: AppText.h3b?.copyWith(color: Colors.white), - ), - ), + child: Text( + (state is UserLoading) ? "Wait..." : "Signup", + style: AppText.h3b?.copyWith(color: Colors.white), + ), + ); + }, ), Space.yf(1.5), Center(