Skip to content

Commit

Permalink
ready
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsinnaqvi505 committed Nov 12, 2022
1 parent 0e2615c commit 948e323
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
11 changes: 6 additions & 5 deletions lib/screens/login/login_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class LoginView extends StatelessWidget {
),
child: Column(
children: [
phoneTextField(),
Form(
key: viewModel.loginFormKey,
child: phoneTextField(),
),
const SizedBox(height: 20),
verifyBtn(),
],
Expand Down Expand Up @@ -117,10 +120,8 @@ class LoginView extends StatelessWidget {
prefixIcon: Icons.phone,
title: 'Phone',
hintText: '03xxxxxxxxx',
keyboardType: TextInputType.phone,
inputFormatters: [
LengthLimitingTextInputFormatter(11),
],
keyboardType: TextInputType.numberWithOptions(),
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
autoValidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
return viewModel.validateTextField(value);
Expand Down
50 changes: 28 additions & 22 deletions lib/screens/login/login_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:task_demo/widgets/getx_helper.dart';
import 'package:task_demo/widgets/loader.dart';

class LoginViewModel extends GetxController {

GlobalKey<FormState> loginFormKey = GlobalKey<FormState>();
TextEditingController phoneController = TextEditingController();

@override
Expand All @@ -17,33 +19,37 @@ class LoginViewModel extends GetxController {
String? validateTextField(String? value) {
if (value == '' || value == null) {
return "Field required";
}else if(value.length != 11){
return "Invalid number";
} else {
return null;
}
}

authentication() async {
FocusScope.of(Get.context!).requestFocus(FocusNode());
FirebaseAuth auth = FirebaseAuth.instance;
Loader.loader.value = true;
print('+92${phoneController.text.replaceFirst('0', '')}');
auth.verifyPhoneNumber(
phoneNumber: '+92${phoneController.text.replaceFirst('0', '')}',
verificationCompleted: (AuthCredential authCredential) {
phoneController.text = '';
Loader.loader.value = false;
Get.offAll(() => const HomeView());
},
verificationFailed: (FirebaseAuthException authException) {
Loader.loader.value = false;
GetxHelper.showSnackBar1(
title: 'Error', message: "${authException.message}");
},
codeAutoRetrievalTimeout: (String value) {
Loader.loader.value = false;
GetxHelper.showSnackBar1(title: 'Error', message: "Timeout error");
},
codeSent: (String verificationId, int? forceResendingToken) {},
);
if(loginFormKey.currentState!.validate()){
FocusScope.of(Get.context!).requestFocus(FocusNode());
FirebaseAuth auth = FirebaseAuth.instance;
Loader.loader.value = true;
print('+92${phoneController.text.replaceFirst('0', '')}');
auth.verifyPhoneNumber(
phoneNumber: '+92${phoneController.text.replaceFirst('0', '')}',
verificationCompleted: (AuthCredential authCredential) {
phoneController.text = '';
Loader.loader.value = false;
Get.offAll(() => const HomeView());
},
verificationFailed: (FirebaseAuthException authException) {
Loader.loader.value = false;
GetxHelper.showSnackBar1(
title: 'Error', message: "${authException.message}");
},
codeAutoRetrievalTimeout: (String value) {
Loader.loader.value = false;
GetxHelper.showSnackBar1(title: 'Error', message: "Timeout error");
},
codeSent: (String verificationId, int? forceResendingToken) {},
);
}
}
}

0 comments on commit 948e323

Please sign in to comment.