@@ -105,9 +105,13 @@ verified manually be calling `verifyOTP`. True if auto verified and false is ver
105105or any internal error occurs, callback is triggered exposing ` FirebaseAuthException `
106106which can be used to handle the error.
107107
108+ ` onCodeSent ` is called when the OTP is successfully sent to the phone number.
109+
108110``` dart
109111FirebasePhoneAuthHandler(
110112 phoneNumber: "+919876543210",
113+ // If true, the user is signed out before the onLoginSuccess callback is fired when the OTP is verified successfully.
114+ signOutOnSuccessfulVerification: false,
111115 builder: (context, controller) {
112116 return SizedBox.shrink();
113117 },
@@ -248,20 +252,20 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
248252 @override
249253 void initState() {
250254 scrollController = ScrollController();
251- WidgetsBinding.instance? .addObserver(this);
255+ WidgetsBinding.instance.addObserver(this);
252256 super.initState();
253257 }
254258
255259 @override
256260 void dispose() {
257- WidgetsBinding.instance? .removeObserver(this);
261+ WidgetsBinding.instance.removeObserver(this);
258262 scrollController.dispose();
259263 super.dispose();
260264 }
261265
262266 @override
263267 void didChangeMetrics() {
264- final bottomViewInsets = WidgetsBinding.instance! .window.viewInsets.bottom;
268+ final bottomViewInsets = WidgetsBinding.instance.window.viewInsets.bottom;
265269 isKeyboardVisible = bottomViewInsets > 0;
266270 }
267271
@@ -285,6 +289,11 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
285289 return SafeArea(
286290 child: FirebasePhoneAuthHandler(
287291 phoneNumber: widget.phoneNumber,
292+ signOutOnSuccessfulVerification: false,
293+ autoRetrievalTimeOutDuration: const Duration(seconds: 60),
294+ onCodeSent: () {
295+ log(VerifyPhoneNumberScreen.id, msg: 'OTP sent!');
296+ },
288297 onLoginSuccess: (userCredential, autoVerified) async {
289298 log(
290299 VerifyPhoneNumberScreen.id,
@@ -320,24 +329,38 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
320329 actions: [
321330 if (controller.codeSent)
322331 TextButton(
323- child: Text(
324- controller.timerIsActive
325- ? '${controller.timerCount.inSeconds}s'
326- : 'Resend',
327- style: const TextStyle(color: Colors.blue, fontSize: 18),
328- ),
329332 onPressed: controller.timerIsActive
330333 ? null
331334 : () async {
332335 log(VerifyPhoneNumberScreen.id, msg: 'Resend OTP');
333336 await controller.sendOTP();
334337 },
338+ child: Text(
339+ controller.timerIsActive
340+ ? '${controller.timerCount.inSeconds}s'
341+ : 'Resend',
342+ style: const TextStyle(color: Colors.blue, fontSize: 18),
343+ ),
335344 ),
336345 const SizedBox(width: 5),
337346 ],
338347 ),
339- body: controller.codeSent
340- ? ListView(
348+ body: controller.isSendingCode
349+ ? Column(
350+ mainAxisAlignment: MainAxisAlignment.center,
351+ crossAxisAlignment: CrossAxisAlignment.center,
352+ children: const [
353+ CustomLoader(),
354+ SizedBox(height: 50),
355+ Center(
356+ child: Text(
357+ 'Sending OTP',
358+ style: TextStyle(fontSize: 25),
359+ ),
360+ ),
361+ ],
362+ )
363+ : ListView(
341364 padding: const EdgeInsets.all(20),
342365 controller: scrollController,
343366 children: [
@@ -381,30 +404,15 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
381404 if (hasFocus) await _scrollToBottomOnKeyboardOpen();
382405 },
383406 onSubmit: (enteredOTP) async {
384- final isValidOTP = await controller.verifyOTP(
385- otp: enteredOTP,
386- );
407+ final isValidOTP =
408+ await controller.verifyOTP(enteredOTP);
387409 // Incorrect OTP
388410 if (!isValidOTP) {
389411 showSnackBar('The entered OTP is invalid!');
390412 }
391413 },
392414 ),
393415 ],
394- )
395- : Column(
396- mainAxisAlignment: MainAxisAlignment.center,
397- crossAxisAlignment: CrossAxisAlignment.center,
398- children: const [
399- CustomLoader(),
400- SizedBox(height: 50),
401- Center(
402- child: Text(
403- 'Sending OTP',
404- style: TextStyle(fontSize: 25),
405- ),
406- ),
407- ],
408416 ),
409417 );
410418 },
0 commit comments