基本完成除了详情

This commit is contained in:
zhutao
2025-09-04 10:16:11 +08:00
commit 4d12f8afc2
110 changed files with 4729 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class AgreePage extends StatelessWidget {
final String title;
final String url;
const AgreePage({super.key, required this.title, required this.url});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: WebViewWidget(
controller: WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(Uri.parse(url)),
),
);
}
}

View File

@@ -0,0 +1,115 @@
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import '../../../api/endpoints/user_api.dart';
import '../../../api/network/safe.dart';
import '../../../providers/app_store.dart';
import '../../../router/config/route_paths.dart';
import '../../../widgets/ui_kit/button/custom_button.dart';
import 'widget/widget.dart';
class LoginCodePage extends StatefulWidget {
final String email;
final String password;
const LoginCodePage({super.key, required this.email, required this.password});
@override
State<LoginCodePage> createState() => _LoginCodePageState();
}
class _LoginCodePageState extends State<LoginCodePage> {
final _codeController = TextEditingController();
var _subLoading = false;
@override
void initState() {
super.initState();
_handSendCode();
}
///发送验证码
void _handSendCode() {
sendEmailCodeApi(widget.email);
EasyLoading.showSuccess("Send success");
}
///提交
void _handSubmit() async {
if (_codeController.text.isNotEmpty) {
setState(() {
_subLoading = true;
});
var res = await safeRequest(
registerApi(
widget.email,
widget.password,
_codeController.text,
),
onError: (error) {
setState(() {
_subLoading = false;
});
},
);
var appStore = context.read<AppStore>();
await appStore.setInfo(res);
context.go(RoutePaths.layout);
setState(() {
_subLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
width: double.infinity,
padding: EdgeInsets.only(left: 20, right: 20, top: 40),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Check your inbox",
style: Theme.of(context).textTheme.titleLarge,
),
Container(
margin: EdgeInsets.only(top: 20, bottom: 40),
child: Text(
"Enter the verification code we just sent to ${widget.email}.",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.labelMedium,
),
),
InputBox(hintText: "Code", controller: _codeController),
Container(
margin: EdgeInsets.only(top: 20),
child: CustomButton(
loading: _subLoading,
onPressed: _handSubmit,
child: Text("Continue"),
),
),
Container(
margin: EdgeInsets.only(top: 20),
child: TextButton(
onPressed: () {
_handSendCode();
},
child: Text(
"Resend code",
style: Theme.of(context).textTheme.labelSmall,
),
),
),
],
),
),
),
);
}
}

View File

@@ -0,0 +1,257 @@
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:go_router/go_router.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:plan/data/models/other_login_type.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import '../../../api/endpoints/user_api.dart';
import '../../../providers/app_store.dart';
import '../../../router/config/route_paths.dart';
import '../../../utils/common.dart';
import '../../../widgets/ui_kit/button/custom_button.dart';
import 'widget/agreement_box.dart';
import 'widget/widget.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
var _subLoading = false;
///协议
bool _agree = false;
///谷歌登陆
final GoogleSignIn _googleSignIn = GoogleSignIn.instance;
///邮箱输入框
final TextEditingController _emailController = TextEditingController(text: "");
final TextEditingController _passwordController = TextEditingController(text: "");
//显示密码
var _hidePassword = true;
@override
void initState() {
super.initState();
_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((_) {
print("登陆成功");
})
.onError((error) {
print('登录错误: $error');
});
}
///谷歌登录
void _handleGoogleSignIn() async {
if (!_agree) {
EasyLoading.showToast('Please read and agree to the terms first.');
return;
}
try {
// 如果用户未登录,则启动标准的 Google 登录
if (_googleSignIn.supportsAuthenticate()) {
// 使用 authenticate() 进行认证
GoogleSignInAccount? user = await _googleSignIn.authenticate();
var auth = user.authentication;
// var res = await Dio().get("https://oauth2.googleapis.com/tokeninfo?id_token=${auth.idToken}");
//登陆
EasyLoading.show(status: "Logging in...");
var res = await thirdLoginApi(auth.idToken!, OtherLoginType.google);
EasyLoading.dismiss();
_onLogin(res);
}
// } catch (e) {
// if (e is GoogleSignInException) {
// if (e.code == GoogleSignInExceptionCode.canceled) {
// // 用户取消登录
// print("User canceled login.");
// } else {
// // 其他错误
// print("Google Sign-In error: $e");
// }
// } else {
// print("Unknown error: $e");
// }
// }
} catch (e) {
EasyLoading.showError("Login failed");
print("登录错误: $e");
}
}
///apple登录
void _handAppleSignIn() async {
if (!_agree) {
EasyLoading.showToast('Please read and agree to the terms first.');
return;
}
try {
final credential = await SignInWithApple.getAppleIDCredential(scopes: [AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName]);
EasyLoading.show(status: "Logging in...");
var res = await thirdLoginApi(credential.identityToken!, OtherLoginType.apple);
EasyLoading.dismiss();
_onLogin(res);
print('Apple Credential: ${credential.identityToken}');
print('Apple Email: ${credential.email}');
} catch (e) {
print('Error during Apple sign-in: $e');
}
}
void _handSubmit() async {
if (!_agree) {
EasyLoading.showToast('Please read and agree to the terms first.');
return;
}
if (_emailController.text.isEmpty) {
//请输入邮箱
EasyLoading.showError("Please enter your email");
return;
} else if (_passwordController.text.isEmpty) {
EasyLoading.showError("Please enter your Password");
return;
}
try {
setState(() {
_subLoading = true;
});
var isRegister = await checkRegisterApi(_emailController.text);
if (!isRegister) {
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 GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
resizeToAvoidBottomInset: false,
body: SafeArea(
child: Container(
width: double.infinity,
padding: EdgeInsets.only(top: 0.07.sh, left: 20, right: 20),
child: ListView(
children: [
LogoBox(),
PageHeader(),
InputBox(hintText: "Email", controller: _emailController),
SizedBox(height: 15),
InputBox(
obscureText: _hidePassword,
hintText: "Password",
controller: _passwordController,
suffix: InkWell(
onTap: () {
setState(() {
_hidePassword = !_hidePassword;
});
},
child: Icon(
_hidePassword ? RemixIcons.eye_off_fill : RemixIcons.eye_fill,
size: 20,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
),
Container(
margin: EdgeInsets.only(top: 20),
height: 45,
child: CustomButton(loading: _subLoading, round: false, onPressed: _handSubmit, child: Text("Continue")),
),
LoginDivider(),
OtherButton(
title: "Continue with Google",
icon: "assets/image/google.png",
onTap: () {
_handleGoogleSignIn();
},
),
SizedBox(height: 15),
OtherButton(
title: "Continue with Apple",
icon: "assets/image/apple.png",
onTap: () {
_handAppleSignIn();
},
),
Container(
width: double.infinity,
margin: EdgeInsets.only(top: 40),
alignment: Alignment.center,
child: AgreementBox(
checked: _agree,
onChanged: (value) {
setState(() {
_agree = value;
});
},
),
),
],
),
),
),
),
);
}
}

View File

@@ -0,0 +1,73 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../../../../router/config/route_paths.dart';
///勾中协议
class AgreementBox extends StatelessWidget {
final bool checked;
final Function(bool) onChanged;
const AgreementBox({
super.key,
this.checked = false,
required this.onChanged,
});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 25,
child: Transform.scale(
scale: 0.8,
child: Checkbox(
value: checked,
shape: CircleBorder(),
onChanged: (value) {
onChanged(value!);
},
),
),
),
GestureDetector(
onTap: () {
onChanged(!checked);
},
child: RichText(
text: TextSpan(
style: Theme.of(context).textTheme.labelSmall,
children: [
TextSpan(
text: "I agree to the ",
),
TextSpan(
text: "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: " & "),
TextSpan(
text: "Privacy Policy",
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"},
),
),
],
),
),
),
],
);
}
}

View File

@@ -0,0 +1,166 @@
import 'package:flutter/material.dart';
///登陆Box
class LogoBox extends StatelessWidget {
const LogoBox({super.key});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 40),
child: Column(
children: [
Image.asset(
"assets/image/logo.png",
width: 43,
),
Text(
"FoodCura",
style: Theme.of(context).textTheme.titleSmall,
),
],
),
);
}
}
///头部文案
class PageHeader extends StatelessWidget {
const PageHeader({super.key});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 30),
child: Column(
children: [
Text(
"Create an account",
style: TextStyle(fontWeight: FontWeight.w700),
),
Text(
"Use your email to get started",
style: Theme.of(context).textTheme.bodySmall,
),
],
),
);
}
}
///输入框
class InputBox extends StatelessWidget {
final bool obscureText;
final String hintText;
final TextEditingController controller;
final Widget? suffix;
const InputBox({
super.key,
this.obscureText = false,
required this.hintText,
required this.controller,
this.suffix,
});
@override
Widget build(BuildContext context) {
//边框
var inputBorder = OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: Theme.of(context).colorScheme.surfaceContainer,
),
);
return TextField(
controller: controller,
maxLength: 100,
obscureText: obscureText,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(
hintText: hintText,
hintStyle: Theme.of(context).textTheme.labelMedium,
counterText: '',
border: inputBorder,
enabledBorder: inputBorder,
suffix: suffix,
suffixIconConstraints: BoxConstraints(
minWidth: 0,
minHeight: 0,
),
),
);
}
}
///分割线
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(
"or",
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 title;
final String icon;
const OtherButton({
super.key,
required this.onTap,
required this.title,
required this.icon,
});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
child: Container(
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Theme.of(context).colorScheme.surfaceContainer,
),
child: Row(
spacing: 10,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(icon, width: 20),
Text(
title,
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
);
}
}

View File

@@ -0,0 +1,65 @@
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import '../../../config/app_context.dart';
import '../../../providers/app_store.dart';
import '../../../router/config/route_paths.dart';
import '../../../router/routes.dart';
class SplashPage extends StatefulWidget {
const SplashPage({super.key});
@override
State<SplashPage> createState() => _SplashPageState();
}
class _SplashPageState extends State<SplashPage> {
@override
void initState() {
super.initState();
init();
}
void init() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
AppContext.setContent(navigatorKey.currentState!.context);
//效验
AppStore appStore = context.read<AppStore>();
await appStore.init();
if (!mounted) return;
if (appStore.token.isEmpty) {
context.go(RoutePaths.login);
} else {
context.go(RoutePaths.layout);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.asset(
"assets/image/logo.png",
width: 68.w,
),
Container(
margin: EdgeInsets.only(top: 16),
child: Text(
"Demacare",
style: Theme.of(context).textTheme.titleMedium,
),
),
],
),
),
);
}
}