Untitled
cashlinkml2021
plain_text
a year ago
4.2 kB
3
Indexable
Never
import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; import '../../consts/colors.dart'; class PhoneInputScreen extends StatefulWidget { @override _PhoneInputScreenState createState() => _PhoneInputScreenState(); } class _PhoneInputScreenState extends State<PhoneInputScreen> { final _phoneNumberController = TextEditingController(); Future<void> verifyPhoneNumber(BuildContext context) async { await FirebaseAuth.instance.verifyPhoneNumber( phoneNumber: _phoneNumberController.text, verificationCompleted: (PhoneAuthCredential credential) { // This callback will be called when auto-retrieval completes // For example, auto sign-in (not implemented in this basic example) }, verificationFailed: (FirebaseAuthException e) { // Handle verification failure print('Verification Failed: ${e.message}'); }, codeSent: (String verificationId, int? resendToken) { // Navigate to the OTP screen with verificationId Navigator.push( context, MaterialPageRoute( builder: (context) => OTPScreen(verificationId), ), ); }, codeAutoRetrievalTimeout: (String verificationId) {}, ); } @override Widget build(BuildContext context) { return Scaffold(backgroundColor: redColor, appBar: AppBar( title: Text('Phone Number Verification'), ), body: Padding( padding: EdgeInsets.all(20.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ TextField( controller: _phoneNumberController, keyboardType: TextInputType.phone, decoration: InputDecoration(labelText: 'Enter Phone Number'), ), SizedBox(height: 20), ElevatedButton( onPressed: () => verifyPhoneNumber(context), child: Text('Send OTP'), ), ], ), ), ); } } class OTPScreen extends StatefulWidget { final String verificationId; OTPScreen(this.verificationId); @override _OTPScreenState createState() => _OTPScreenState(); } class _OTPScreenState extends State<OTPScreen> { final _otpController = TextEditingController(); Future<void> signInWithOTP(String smsCode) async { try { PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.credential( verificationId: widget.verificationId, smsCode: smsCode, ); await FirebaseAuth.instance.signInWithCredential(phoneAuthCredential); // Navigate to the home screen after successful verification // Replace 'HomeScreen()' with your actual home screen. Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => HomeScreen()), ); } catch (e) { // Handle OTP verification failure print('Failed to verify OTP: $e'); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Enter OTP'), ), body: Padding( padding: EdgeInsets.all(20.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ TextField( controller: _otpController, keyboardType: TextInputType.number, decoration: InputDecoration(labelText: 'Enter OTP'), ), SizedBox(height: 20), ElevatedButton( onPressed: () => signInWithOTP(_otpController.text), child: Text('Verify OTP'), ), ], ), ), ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home'), ), body: Center( child: Text('Welcome to the Home Screen!'), ), ); } }