登录流程已全部重构
This commit is contained in:
289
lib/pages/system/login/login_page.dart
Normal file
289
lib/pages/system/login/login_page.dart
Normal file
@@ -0,0 +1,289 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:food_health/api/endpoints/user_api.dart';
|
||||
import 'package:food_health/data/models/other_login_type.dart';
|
||||
import 'package:food_health/l10n/l10n.dart';
|
||||
import 'package:food_health/pages/system/login/widgets/login_input.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
import 'package:food_health/stores/app_store.dart';
|
||||
import 'package:food_health/widgets/ui_kit/button/app_button.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
|
||||
|
||||
import '../../../utils/common.dart';
|
||||
import 'widgets/login_agree.dart';
|
||||
import 'widgets/login_other.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
const LoginPage({super.key});
|
||||
|
||||
@override
|
||||
State<LoginPage> createState() => _LoginPageState();
|
||||
}
|
||||
|
||||
class _LoginPageState extends State<LoginPage> {
|
||||
Logger logger = Logger();
|
||||
|
||||
var _subLoading = false;
|
||||
|
||||
///邮箱输入框
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _passwordController = TextEditingController();
|
||||
|
||||
//显示密码
|
||||
bool _hidePassword = true;
|
||||
|
||||
///谷歌登陆
|
||||
final GoogleSignIn _googleSignIn = GoogleSignIn.instance;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_emailController.addListener(() {
|
||||
setState(() {});
|
||||
});
|
||||
_passwordController.addListener(() {
|
||||
setState(() {});
|
||||
});
|
||||
_ensureNetworkPermission();
|
||||
_initGoogleSign();
|
||||
}
|
||||
|
||||
/// 预触发 iOS 网络权限弹窗
|
||||
Future<bool> _ensureNetworkPermission() async {
|
||||
try {
|
||||
await Dio().get(
|
||||
'https://captive.apple.com/hotspot-detect.html',
|
||||
options: Options(
|
||||
sendTimeout: const Duration(seconds: 3),
|
||||
receiveTimeout: const Duration(seconds: 3),
|
||||
headers: {'Cache-Control': 'no-cache'},
|
||||
),
|
||||
);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void _initGoogleSign() {
|
||||
if (isAndroid()) {
|
||||
_googleSignIn.initialize(
|
||||
clientId: null,
|
||||
serverClientId:
|
||||
"512878764950-0bsl98c4q4p695mlmfn35qhmr2ld5n0o.apps.googleusercontent.com",
|
||||
);
|
||||
} else {
|
||||
_googleSignIn.initialize(
|
||||
clientId: "512878764950-1ke7slf0c6dlmchnuk0fqh3fe954gcf2.apps.googleusercontent.com",
|
||||
serverClientId:
|
||||
"512878764950-0bsl98c4q4p695mlmfn35qhmr2ld5n0o.apps.googleusercontent.com",
|
||||
);
|
||||
}
|
||||
|
||||
_googleSignIn.authenticationEvents
|
||||
.listen((_) {
|
||||
logger.d("登陆成功");
|
||||
})
|
||||
.onError((error) {
|
||||
logger.e("登陆错误: $error");
|
||||
});
|
||||
}
|
||||
|
||||
///谷歌登录
|
||||
void _handGoogleSignIn() async {
|
||||
try {
|
||||
// 如果用户未登录,则启动标准的 Google 登录
|
||||
if (_googleSignIn.supportsAuthenticate()) {
|
||||
// 使用 authenticate() 进行认证
|
||||
GoogleSignInAccount? user = await _googleSignIn.authenticate();
|
||||
var auth = user.authentication;
|
||||
|
||||
//登陆
|
||||
EasyLoading.show();
|
||||
var res = await thirdLoginApi(auth.idToken!, OtherLoginType.google);
|
||||
EasyLoading.dismiss();
|
||||
_onLogin(res);
|
||||
}
|
||||
} catch (e) {
|
||||
EasyLoading.showError(L10n.of.login_error_text);
|
||||
logger.e("登录错误: $e");
|
||||
}
|
||||
}
|
||||
|
||||
///apple登录
|
||||
void _handAppleSignIn() async {
|
||||
try {
|
||||
final credential = await SignInWithApple.getAppleIDCredential(
|
||||
scopes: [
|
||||
AppleIDAuthorizationScopes.email,
|
||||
AppleIDAuthorizationScopes.fullName,
|
||||
],
|
||||
);
|
||||
EasyLoading.show();
|
||||
var res = await thirdLoginApi(credential.identityToken!, OtherLoginType.apple);
|
||||
EasyLoading.dismiss();
|
||||
_onLogin(res);
|
||||
logger.d('Apple Credential: ${credential.identityToken}');
|
||||
logger.d('Apple Email: ${credential.email}');
|
||||
} catch (e) {
|
||||
logger.e("登录错误: $e");
|
||||
}
|
||||
}
|
||||
|
||||
void _handSubmit() async {
|
||||
if (_emailController.text.isEmpty) {
|
||||
//请输入邮箱
|
||||
EasyLoading.showToast(L10n.of.login_email_hint);
|
||||
return;
|
||||
} else if (_passwordController.text.isEmpty) {
|
||||
EasyLoading.showToast(L10n.of.login_password_hint);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setState(() {
|
||||
_subLoading = true;
|
||||
});
|
||||
var isRegister = await checkRegisterApi(_emailController.text);
|
||||
if (!isRegister && mounted) {
|
||||
context.push(
|
||||
RoutePaths.loginCode,
|
||||
extra: {
|
||||
"email": _emailController.text,
|
||||
"password": _passwordController.text,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
var res = await loginApi(_emailController.text, _passwordController.text);
|
||||
_onLogin(res);
|
||||
}
|
||||
setState(() {
|
||||
_subLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_subLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
///登陆的操作
|
||||
void _onLogin(dynamic res) {
|
||||
var appStore = context.read<AppStore>();
|
||||
appStore.setInfo(res);
|
||||
context.go(RoutePaths.layout);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
resizeToAvoidBottomInset: false,
|
||||
body: SafeArea(
|
||||
child: Container(
|
||||
padding: EdgeInsets.only(
|
||||
left: 20,
|
||||
right: 20,
|
||||
top: 0.08.sh,
|
||||
bottom: 40,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 40),
|
||||
child: Text(
|
||||
L10n.of.login_title,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
),
|
||||
LoginInput(
|
||||
hintText: L10n.of.login_email_hint,
|
||||
controller: _emailController,
|
||||
suffix: Visibility(
|
||||
visible: _emailController.text.isNotEmpty,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
_emailController.clear();
|
||||
},
|
||||
child: Icon(
|
||||
RemixIcons.close_circle_fill,
|
||||
size: 20,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
LoginInput(
|
||||
hintText: L10n.of.login_password_hint,
|
||||
controller: _passwordController,
|
||||
obscureText: _hidePassword,
|
||||
suffix: InkWell(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_hidePassword = !_hidePassword;
|
||||
});
|
||||
},
|
||||
child: Icon(
|
||||
_hidePassword ? RemixIcons.eye_off_fill : RemixIcons.eye_fill,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 40),
|
||||
height: 50,
|
||||
child: AppButton(
|
||||
disabled: _emailController.text.isEmpty || _passwordController.text.isEmpty,
|
||||
loading: _subLoading,
|
||||
onPressed: _handSubmit,
|
||||
child: Text(L10n.of.login_button),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
margin: EdgeInsets.only(top: 20),
|
||||
alignment: Alignment.center,
|
||||
child: LoginAgree(),
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
LoginDivider(),
|
||||
Row(
|
||||
spacing: 20,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
OtherButton(
|
||||
onTap: () {
|
||||
_handGoogleSignIn();
|
||||
},
|
||||
icon: "assets/image/google.png",
|
||||
),
|
||||
OtherButton(
|
||||
onTap: () {
|
||||
_handAppleSignIn();
|
||||
},
|
||||
icon: "assets/image/apple.png",
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
50
lib/pages/system/login/widgets/login_agree.dart
Normal file
50
lib/pages/system/login/widgets/login_agree.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/l10n/l10n.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class LoginAgree extends StatelessWidget {
|
||||
const LoginAgree({
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RichText(
|
||||
text: TextSpan(
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
children: [
|
||||
TextSpan(
|
||||
text: "${L10n.of.login_tip_start} ",
|
||||
),
|
||||
TextSpan(
|
||||
text: L10n.of.login_terms,
|
||||
style: TextStyle(color: Theme.of(context).primaryColor),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () => context.push(
|
||||
RoutePaths.agreement,
|
||||
extra: {
|
||||
"title": "Terms of Service",
|
||||
"url": "https://support.curain.ai/privacy/foodcura/terms_service.html",
|
||||
},
|
||||
),
|
||||
),
|
||||
TextSpan(text: " ${L10n.of.login_and} "),
|
||||
TextSpan(
|
||||
text: L10n.of.login_privacy,
|
||||
style: TextStyle(color: Theme.of(context).primaryColor),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () => context.push(
|
||||
RoutePaths.agreement,
|
||||
extra: {
|
||||
"title": "Privacy",
|
||||
"url": "https://support.curain.ai/privacy/foodcura/privacy_policy.html",
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
46
lib/pages/system/login/widgets/login_input.dart
Normal file
46
lib/pages/system/login/widgets/login_input.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LoginInput extends StatelessWidget {
|
||||
final bool obscureText;
|
||||
final String hintText;
|
||||
final TextEditingController controller;
|
||||
final Widget? suffix;
|
||||
|
||||
const LoginInput({
|
||||
super.key,
|
||||
this.obscureText = false,
|
||||
required this.hintText,
|
||||
required this.controller,
|
||||
this.suffix,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextField(
|
||||
controller: controller,
|
||||
obscureText: obscureText,
|
||||
maxLength: 100,
|
||||
decoration: InputDecoration(
|
||||
hintText: hintText,
|
||||
hintStyle: Theme.of(context).textTheme.labelLarge?.copyWith(fontSize: 16),
|
||||
counterText: '',
|
||||
filled: true,
|
||||
fillColor: Theme.of(context).colorScheme.surfaceContainer,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(60),
|
||||
borderSide: BorderSide.none, // 去掉边框
|
||||
),
|
||||
isCollapsed: true,
|
||||
contentPadding: EdgeInsets.symmetric(vertical: 14, horizontal: 20),
|
||||
suffixIcon: Container(
|
||||
padding: EdgeInsets.only(right: 20),
|
||||
child: suffix,
|
||||
),
|
||||
suffixIconConstraints: BoxConstraints(
|
||||
minWidth: 0,
|
||||
minHeight: 0,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
67
lib/pages/system/login/widgets/login_other.dart
Normal file
67
lib/pages/system/login/widgets/login_other.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/l10n/l10n.dart';
|
||||
|
||||
///分割线
|
||||
class LoginDivider extends StatelessWidget {
|
||||
const LoginDivider({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 20, bottom: 20),
|
||||
child: Row(
|
||||
spacing: 8,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Container(
|
||||
height: 1,
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
L10n.of.login_other_login,
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
),
|
||||
Expanded(
|
||||
child: Container(
|
||||
height: 1,
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class OtherButton extends StatelessWidget {
|
||||
final Function() onTap;
|
||||
final String icon;
|
||||
|
||||
const OtherButton({
|
||||
super.key,
|
||||
required this.onTap,
|
||||
required this.icon,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
width: 45,
|
||||
padding: EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
),
|
||||
),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: Image.asset(icon),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user