Flutter认证UI插件appwrite_auth_ui的使用
Flutter认证UI插件appwrite_auth_ui的使用
免责声明
本库的最新版本设计用于与Appwrite 1.4.0配合使用。当前Appwrite云平台正在使用Appwrite 1.1.2版本。因此,本包使用的是Appwrite 9.0.1版本。您可以在此处跟踪该问题的进展:此处。
开始使用
在您的assets
文件夹中创建一个名为config.json
的文件,并添加以下字段:
{
"APPWRITE_PROJECT_ID": "",
"APPWRITE_MAGIC_LINK_URL": ""
}
运行您的应用时,请使用以下命令:
flutter run --dart-define-from-file=assets/config.json
邮箱认证
使用AppwriteEmailAuth
小部件来创建邮箱和密码登录/注册表单。它还包含一个按钮以显示忘记密码的表单。
AppwriteEmailAuth(
redirectUrl: const String.fromEnvironment('APPWRITE_MAGIC_LINK_URL'),
onSignInComplete: (response) {
Navigator.of(context).pushReplacementNamed('/home');
},
onSignUpComplete: (response) {
Navigator.of(context).pushReplacementNamed('/home');
},
metadataFields: [
MetaDataField(
prefixIcon: const Icon(Icons.person),
label: '用户名',
key: 'username',
validator: (val) {
if (val == null || val.isEmpty) {
return '请输入内容';
}
return null;
},
),
],
)
魔法链接认证
使用AppwriteMagicAuth
小部件来创建魔法链接登录表单。
AppwriteMagicAuth(
onSuccess: (response) {
// 执行一些操作,例如:
// Navigator.of(context).pushReplacementNamed('/home');
},
onError: (error) {
// 对错误进行处理
},
redirectUrl: kIsWeb
? null
: 'io.appwrite.flutterquickstart://login-callback',
)
重置密码
使用AppwriteResetPassword
来创建密码重置表单。
AppwriteResetPassword(
onError: (error) {
// 对错误进行处理
},
onSuccess: (response) {
Navigator.of(context).pushReplacementNamed('/home');
},
)
社交认证
使用AppwriteSocialsAuth
来创建社交登录按钮列表。
AppwriteSocialsAuth(
colored: true,
socialProviders: SocialProviders.values,
onSuccess: (session) {
Navigator.of(context).pushReplacementNamed('/home');
},
onError: (error) {
// 对错误进行处理
},
)
主题定制
本库使用纯Flutter组件,因此您可以使用自己的主题控制组件的外观。
完整示例代码
import 'package:example/phone_sign_up.dart';
import 'package:flutter/material.dart';
import 'package:appwrite_auth_ui/appwrite_auth_ui.dart';
import './home.dart';
import './sign_in.dart';
import './magic_link.dart';
import './update_password.dart';
import 'phone_sign_in.dart';
import './verify_phone.dart';
late Client appwrite;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
appwrite = Client().setEndpoint('https://cloud.appwrite.io/v1').setProject(
const String.fromEnvironment('APPWRITE_PROJECT_ID'),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// 这个小部件是您的应用的根。
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Appwrite Auth UI Demo',
theme: ThemeData(
colorSchemeSeed: const Color(0xfff02e65),
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(),
),
),
initialRoute: '/',
routes: {
'/': (context) => const SignUp(),
'/magic_link': (context) => const MagicLink(),
'/update_password': (context) => const UpdatePassword(),
'/phone_sign_in': (context) => const PhoneSignIn(),
'/phone_sign_up': (context) => const PhoneSignUp(),
'/verify_phone': (context) => const VerifyPhone(),
'/home': (context) => const Home(),
},
onUnknownRoute: (RouteSettings settings) {
return MaterialPageRoute<void>(
settings: settings,
builder: (BuildContext context) => const Scaffold(
body: Center(
child: Text(
'未找到',
style: TextStyle(
fontSize: 42,
fontWeight: FontWeight.bold,
),
),
),
),
);
},
);
}
}
更多关于Flutter认证UI插件appwrite_auth_ui的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter认证UI插件appwrite_auth_ui的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,关于如何在Flutter项目中使用appwrite_auth_ui
插件来实现认证UI,下面是一个详细的代码示例。appwrite_auth_ui
是一个Flutter插件,它简化了与Appwrite身份验证服务的集成,提供了一个开箱即用的认证界面。
首先,确保你的Flutter项目中已经添加了appwrite_auth_ui
依赖。在你的pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter:
sdk: flutter
appwrite_auth_ui: ^x.y.z # 替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,配置你的Appwrite项目并获取必要的客户端ID和客户端密钥。确保你的Appwrite项目已经启用了身份验证服务。
以下是一个完整的Flutter应用示例,展示了如何使用appwrite_auth_ui
插件:
import 'package:flutter/material.dart';
import 'package:appwrite_auth_ui/appwrite_auth_ui.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter AppWrite Auth UI Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: AuthScreen(),
);
}
}
class AuthScreen extends StatefulWidget {
@override
_AuthScreenState createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
final AppwriteAuthUi authUi = AppwriteAuthUi(
endpoint: 'https://[YOUR_APPWRITE_ENDPOINT]/v1', // 替换为你的Appwrite服务器端点
projectId: '[YOUR_PROJECT_ID]', // 替换为你的Appwrite项目ID
clientId: '[YOUR_CLIENT_ID]', // 替换为你的Appwrite客户端ID
clientSecret: '[YOUR_CLIENT_SECRET]', // 替换为你的Appwrite客户端密钥
redirectUri: 'https://[YOUR_REDIRECT_URI]', // 替换为你的重定向URI
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AppWrite Auth UI Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
try {
// 显示认证UI
await authUi.show(context);
// 认证成功后处理逻辑
print('User authenticated successfully');
} catch (e) {
// 处理错误
print('Authentication failed: $e');
}
},
child: Text('Login/Sign Up'),
),
),
);
}
}
在上面的代码中,我们做了以下几件事:
- 创建一个Flutter应用并定义了一个
AuthScreen
小部件。 - 在
AuthScreen
的_AuthScreenState
中,初始化了AppwriteAuthUi
实例,并提供了必要的配置参数,包括Appwrite服务器端点、项目ID、客户端ID、客户端密钥和重定向URI。 - 在
AuthScreen
的UI中,添加了一个按钮,当用户点击该按钮时,将显示Appwrite的认证UI。 - 在认证成功后,控制台将打印“User authenticated successfully”。如果认证失败,将捕获异常并打印错误信息。
请确保将占位符(如[YOUR_APPWRITE_ENDPOINT]
、[YOUR_PROJECT_ID]
等)替换为你的实际Appwrite项目信息。
这样,你就可以在Flutter应用中轻松集成Appwrite的认证UI了。