class SmsAuthScreen extends StatefulWidget {
@override
_SmsAuthScreenState createState() => _SmsAuthScreenState();
}
class _SmsAuthScreenState extends State<SmsAuthScreen> {
final TextEditingController _phoneController = TextEditingController();
final TextEditingController _otpController = TextEditingController();
String _selectedDialCode = '+1';
String _selectedCountryCode = 'US';
bool _otpSent = false;
Future<void> _sendOTP() async {
try {
final phoneData = PhoneData(
dialCode: _selectedDialCode,
iso2: _selectedCountryCode,
phone: _phoneController.text,
);
await DynamicSDK.instance.auth.sms.sendOTP(phoneData);
setState(() {
_otpSent = true;
});
} catch (e) {
// Handle error
print('Error sending SMS OTP: $e');
}
}
Future<void> _verifyOTP() async {
try {
await DynamicSDK.instance.auth.sms.verifyOTP(_otpController.text);
// User is now authenticated
Navigator.pushReplacementNamed(context, '/home');
} catch (e) {
// Handle error
print('Error verifying SMS OTP: $e');
}
}
Future<void> _resendOTP() async {
try {
await DynamicSDK.instance.auth.sms.resendOTP();
// Show success message
} catch (e) {
// Handle error
print('Error resending SMS OTP: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('SMS Authentication')),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
if (!_otpSent) ...[
Row(
children: [
DropdownButton<String>(
value: _selectedDialCode,
items: [
DropdownMenuItem(value: '+1', child: Text('+1 (US)')),
DropdownMenuItem(value: '+44', child: Text('+44 (UK)')),
DropdownMenuItem(value: '+81', child: Text('+81 (JP)')),
],
onChanged: (value) {
setState(() {
_selectedDialCode = value!;
_selectedCountryCode = value == '+1' ? 'US' :
value == '+44' ? 'GB' : 'JP';
});
},
),
SizedBox(width: 8),
Expanded(
child: TextField(
controller: _phoneController,
decoration: InputDecoration(
labelText: 'Phone Number',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.phone,
),
),
],
),
SizedBox(height: 16),
ElevatedButton(
onPressed: _sendOTP,
child: Text('Send SMS Code'),
),
] else ...[
Text('Enter the verification code sent to $_selectedDialCode${_phoneController.text}'),
SizedBox(height: 16),
TextField(
controller: _otpController,
decoration: InputDecoration(
labelText: 'Verification Code',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.number,
maxLength: 6,
),
SizedBox(height: 16),
ElevatedButton(
onPressed: _verifyOTP,
child: Text('Verify Code'),
),
SizedBox(height: 8),
TextButton(
onPressed: _resendOTP,
child: Text('Resend Code'),
),
],
],
),
),
);
}
}