Flutter短信验证码UI插件phone_auth_otp_ui的使用
Flutter短信验证码UI插件phone_auth_otp_ui的使用
Phone Auth OTP UI 是一个用于手机号验证码认证的 UI 插件,它具有许多超酷的功能,例如根据用户位置自动更新国家代码、带有搜索功能的国家选择屏幕、用于验证用户手机号码的验证码屏幕以及一套非常漂亮的 UI,适用于任何 Flutter 项目。
安装
在您的 Flutter 项目中添加该插件:
flutter pub add phone_auth_otp_ui
开始使用
以下是一个快速示例,展示如何在您的应用中实现该 UI。
示例代码
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:example/firebase_options.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:phone_auth_otp_ui/phone_auth_otp_ui.dart';
const bool USE_EMULATOR = true;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Firebase
await Firebase.initializeApp(
name: 'phone-auth-otp-ui',
options: DefaultFirebaseOptions.currentPlatform,
);
if (USE_EMULATOR) {
_connectToFirebaseEmulator();
}
runApp(const MyApp());
}
Future _connectToFirebaseEmulator() async {
const fireStorePort = "8080";
const authPort = "9099";
final localHost = Platform.isAndroid ? '10.0.2.2' : 'localhost';
FirebaseFirestore.instance.settings = Settings(
host: "$localHost:$fireStorePort",
sslEnabled: false,
persistenceEnabled: false);
await FirebaseAuth.instance.useEmulator("http://$localHost:$authPort");
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
fontFamily: "regular",
primaryColor: Colors.blue, // 替换为您项目的颜色
backgroundColor: Colors.blue, // 替换为您项目的颜色
colorScheme: ThemeData()
.colorScheme
.copyWith(primary: Colors.blue, secondary: Colors.blue),
),
home: const EditNumberScreen(navScreen: HomeScreen()),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.blue, // 替换为您项目的颜色
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'images/success.png',
fit: BoxFit.cover,
height: 250,
),
const SizedBox(
height: 30,
),
const Text(
'用户认证成功!',
style: TextStyle(
fontSize: 18.0,
fontFamily: 'semi-bold',
),
textAlign: TextAlign.center,
),
const SizedBox(
height: 20,
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Text(
'支持我们,请点赞并关注我的社交媒体以获取更多精彩内容!',
textAlign: TextAlign.center,
),
),
],
),
),
),
);
}
}
更多关于Flutter短信验证码UI插件phone_auth_otp_ui的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter短信验证码UI插件phone_auth_otp_ui的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
phone_auth_otp_ui
是一个用于 Flutter 的插件,它提供了一个现成的 UI 组件,用于处理短信验证码的输入和验证。这个插件可以帮助开发者快速集成短信验证码功能,而无需从头开始设计和实现 UI。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 phone_auth_otp_ui
插件的依赖:
dependencies:
flutter:
sdk: flutter
phone_auth_otp_ui: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
使用插件
以下是一个简单的示例,展示了如何使用 phone_auth_otp_ui
插件来实现短信验证码的输入和验证。
import 'package:flutter/material.dart';
import 'package:phone_auth_otp_ui/phone_auth_otp_ui.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: PhoneAuthScreen(),
);
}
}
class PhoneAuthScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Phone Auth OTP UI'),
),
body: Center(
child: PhoneAuthOtpUI(
phoneNumber: '+1234567890', // 用户的手机号码
onCodeSent: (String verificationId) {
// 当验证码发送成功时调用
print('Verification ID: $verificationId');
},
onVerificationCompleted: (AuthCredential credential) {
// 当验证成功时调用
print('Verification completed: $credential');
},
onVerificationFailed: (AuthException exception) {
// 当验证失败时调用
print('Verification failed: ${exception.message}');
},
onCodeAutoRetrievalTimeout: (String verificationId) {
// 当自动检索验证码超时时调用
print('Code auto retrieval timeout: $verificationId');
},
),
),
);
}
}
参数说明
phoneNumber
: 用户的手机号码,用于发送验证码。onCodeSent
: 当验证码发送成功时调用的回调函数,返回verificationId
。onVerificationCompleted
: 当验证成功时调用的回调函数,返回AuthCredential
。onVerificationFailed
: 当验证失败时调用的回调函数,返回AuthException
。onCodeAutoRetrievalTimeout
: 当自动检索验证码超时时调用的回调函数,返回verificationId
。
自定义 UI
phone_auth_otp_ui
插件提供了一些可选的参数,允许你自定义 UI 的外观和行为。例如,你可以自定义按钮的文本、颜色、字体等。
PhoneAuthOtpUI(
phoneNumber: '+1234567890',
buttonText: 'Send OTP',
buttonColor: Colors.blue,
textColor: Colors.white,
onCodeSent: (String verificationId) {
print('Verification ID: $verificationId');
},
onVerificationCompleted: (AuthCredential credential) {
print('Verification completed: $credential');
},
onVerificationFailed: (AuthException exception) {
print('Verification failed: ${exception.message}');
},
onCodeAutoRetrievalTimeout: (String verificationId) {
print('Code auto retrieval timeout: $verificationId');
},
)
处理验证码
在 onCodeSent
回调中,你可以获取到 verificationId
,然后将其传递给 PhoneAuthProvider.credential
来验证用户输入的验证码。
AuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: smsCode, // 用户输入的验证码
);
FirebaseAuth.instance.signInWithCredential(credential).then((AuthResult result) {
// 验证成功
}).catchError((error) {
// 验证失败
});