基本完成
This commit is contained in:
15
lib/api/dto/base_dto.dart
Normal file
15
lib/api/dto/base_dto.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
class ApiDto<T> {
|
||||
final int code;
|
||||
final String message;
|
||||
final T data;
|
||||
|
||||
ApiDto({required this.code, required this.message, required this.data});
|
||||
|
||||
factory ApiDto.fromJson(Map<String, dynamic> json) {
|
||||
return ApiDto<T>(
|
||||
code: json['code'],
|
||||
message: json['message'],
|
||||
data: json['data'],
|
||||
);
|
||||
}
|
||||
}
|
||||
49
lib/api/dto/food_scan_dto.dart
Normal file
49
lib/api/dto/food_scan_dto.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
class FoodScanDto {
|
||||
int? id;
|
||||
String? foodName;
|
||||
String? foodDesc;
|
||||
List<String>? ingredientsList;
|
||||
String? explanation;
|
||||
String? suggestions;
|
||||
List<String>? healthConcernsList;
|
||||
int? foodType;
|
||||
String? imageUrl;
|
||||
|
||||
FoodScanDto({
|
||||
this.id,
|
||||
this.foodName,
|
||||
this.foodDesc,
|
||||
this.ingredientsList,
|
||||
this.explanation,
|
||||
this.suggestions,
|
||||
this.healthConcernsList,
|
||||
this.foodType,
|
||||
this.imageUrl,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["food_name"] = foodName;
|
||||
map["food_desc"] = foodDesc;
|
||||
map["ingredients"] = ingredientsList;
|
||||
map["explanation"] = explanation;
|
||||
map["suggestions"] = suggestions;
|
||||
map["health_concerns"] = healthConcernsList;
|
||||
map["food_type"] = foodType;
|
||||
map["image_url"] = imageUrl;
|
||||
return map;
|
||||
}
|
||||
|
||||
FoodScanDto.fromJson(dynamic json) {
|
||||
id = json["id"] ?? 0;
|
||||
foodName = json["food_name"] ?? "";
|
||||
foodDesc = json["food_desc"] ?? "";
|
||||
ingredientsList = json["ingredients"] != null ? json["ingredients"].cast<String>() : [];
|
||||
explanation = json["explanation"] ?? "";
|
||||
suggestions = json["suggestions"] ?? "";
|
||||
healthConcernsList = json["health_concerns"] != null ? json["health_concerns"].cast<String>() : [];
|
||||
foodType = json["food_type"] ?? 0;
|
||||
imageUrl = json["image_url"] ?? "";
|
||||
}
|
||||
}
|
||||
86
lib/api/dto/login_dto.dart
Normal file
86
lib/api/dto/login_dto.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
class UserInfo {
|
||||
int? id;
|
||||
String? name;
|
||||
dynamic avatar;
|
||||
String? email;
|
||||
dynamic emailVerifiedAt;
|
||||
dynamic googleId;
|
||||
dynamic appleId;
|
||||
String? lastLoginIp;
|
||||
String? lastLoginTime;
|
||||
dynamic lastUsedTime;
|
||||
int? status;
|
||||
String? createdAt;
|
||||
String? updatedAt;
|
||||
|
||||
UserInfo({
|
||||
this.id,
|
||||
this.name,
|
||||
this.avatar,
|
||||
this.email,
|
||||
this.emailVerifiedAt,
|
||||
this.googleId,
|
||||
this.appleId,
|
||||
this.lastLoginIp,
|
||||
this.lastLoginTime,
|
||||
this.lastUsedTime,
|
||||
this.status,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["name"] = name;
|
||||
map["avatar"] = avatar;
|
||||
map["email"] = email;
|
||||
map["email_verified_at"] = emailVerifiedAt;
|
||||
map["google_id"] = googleId;
|
||||
map["apple_id"] = appleId;
|
||||
map["last_login_ip"] = lastLoginIp;
|
||||
map["last_login_time"] = lastLoginTime;
|
||||
map["last_used_time"] = lastUsedTime;
|
||||
map["status"] = status;
|
||||
map["created_at"] = createdAt;
|
||||
map["updated_at"] = updatedAt;
|
||||
return map;
|
||||
}
|
||||
|
||||
UserInfo.fromJson(dynamic json) {
|
||||
id = json["id"] ?? 0;
|
||||
name = json["name"] ?? "";
|
||||
avatar = json["avatar"];
|
||||
email = json["email"] ?? "";
|
||||
emailVerifiedAt = json["email_verified_at"];
|
||||
googleId = json["google_id"];
|
||||
appleId = json["apple_id"];
|
||||
lastLoginIp = json["last_login_ip"] ?? "";
|
||||
lastLoginTime = json["last_login_time"] ?? "";
|
||||
lastUsedTime = json["last_used_time"];
|
||||
status = json["status"] ?? 0;
|
||||
createdAt = json["created_at"] ?? "";
|
||||
updatedAt = json["updated_at"] ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
class LoginDto {
|
||||
String? accessToken;
|
||||
UserInfo? userInfo;
|
||||
|
||||
LoginDto({this.accessToken, this.userInfo});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["accessToken"] = accessToken;
|
||||
if (userInfo != null) {
|
||||
map["userInfo"] = userInfo?.toJson();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
LoginDto.fromJson(dynamic json) {
|
||||
accessToken = json["accessToken"] ?? "";
|
||||
userInfo = json["userInfo"] != null ? UserInfo.fromJson(json["userInfo"]) : null;
|
||||
}
|
||||
}
|
||||
18
lib/api/dto/profile_options_dto.dart
Normal file
18
lib/api/dto/profile_options_dto.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
class ProfileOptionDto {
|
||||
String? key;
|
||||
List<String>? valuesList;
|
||||
|
||||
ProfileOptionDto({this.key, this.valuesList});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["key"] = key;
|
||||
map["values"] = valuesList;
|
||||
return map;
|
||||
}
|
||||
|
||||
ProfileOptionDto.fromJson(dynamic json) {
|
||||
key = json["key"] ?? "";
|
||||
valuesList = json["values"] != null ? json["values"].cast<String>() : [];
|
||||
}
|
||||
}
|
||||
58
lib/api/dto/user_profile_dto.dart
Normal file
58
lib/api/dto/user_profile_dto.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
class UserProfileDto {
|
||||
num id;
|
||||
String name;
|
||||
String email;
|
||||
String avatar;
|
||||
String ageRange;
|
||||
List<String> foodAllergiesList;
|
||||
List<String> dietaryPreferencesList;
|
||||
List<String> medicalInformationList;
|
||||
List<String> currentMedicationsList;
|
||||
String activityLevel;
|
||||
|
||||
UserProfileDto({
|
||||
this.id = 0,
|
||||
this.name = "",
|
||||
this.email = "",
|
||||
this.avatar = "",
|
||||
this.ageRange = "",
|
||||
List<String>? foodAllergiesList,
|
||||
List<String>? dietaryPreferencesList,
|
||||
List<String>? medicalInformationList,
|
||||
List<String>? currentMedicationsList,
|
||||
this.activityLevel = "",
|
||||
}) : foodAllergiesList = foodAllergiesList ?? [],
|
||||
dietaryPreferencesList = dietaryPreferencesList ?? [],
|
||||
medicalInformationList = medicalInformationList ?? [],
|
||||
currentMedicationsList = currentMedicationsList ?? [];
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"id": id,
|
||||
"name": name,
|
||||
"email": email,
|
||||
"avatar": avatar,
|
||||
"age_range": ageRange,
|
||||
"food_allergies": foodAllergiesList,
|
||||
"dietary_preferences": dietaryPreferencesList,
|
||||
"medical_information": medicalInformationList,
|
||||
"current_medications": currentMedicationsList,
|
||||
"activity_level": activityLevel,
|
||||
};
|
||||
}
|
||||
|
||||
factory UserProfileDto.fromJson(Map<String, dynamic> json) {
|
||||
return UserProfileDto(
|
||||
id: json["id"] ?? 0,
|
||||
name: json["name"] ?? "",
|
||||
email: json["email"] ?? "",
|
||||
avatar: json["avatar"] ?? "",
|
||||
ageRange: json["age_range"] ?? "",
|
||||
foodAllergiesList: (json["food_allergies"] as List?)?.cast<String>() ?? [],
|
||||
dietaryPreferencesList: (json["dietary_preferences"] as List?)?.cast<String>() ?? [],
|
||||
medicalInformationList: (json["medical_information"] as List?)?.cast<String>() ?? [],
|
||||
currentMedicationsList: (json["current_medications"] as List?)?.cast<String>() ?? [],
|
||||
activityLevel: json["activity_level"] ?? "",
|
||||
);
|
||||
}
|
||||
}
|
||||
23
lib/api/endpoints/food_api.dart
Normal file
23
lib/api/endpoints/food_api.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:food_health/api/dto/food_scan_dto.dart';
|
||||
|
||||
import '../network/request.dart';
|
||||
|
||||
///食物检测
|
||||
Future<FoodScanDto> foodScanApi(List<int> bytes) async {
|
||||
FormData formData = FormData.fromMap({
|
||||
"food_image": MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: "upload.jpg",
|
||||
contentType: DioMediaType("image", "jpeg"),
|
||||
),
|
||||
});
|
||||
var res = await Request().post("/food/scan", formData);
|
||||
return FoodScanDto.fromJson(res);
|
||||
}
|
||||
|
||||
///食物检测列表
|
||||
Future<List<FoodScanDto>> foodScanListApi() async {
|
||||
var res = await Request().get("/food/records");
|
||||
return res['list'].map<FoodScanDto>((e) => FoodScanDto.fromJson(e)).toList();
|
||||
}
|
||||
27
lib/api/endpoints/profile_api.dart
Normal file
27
lib/api/endpoints/profile_api.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:food_health/api/dto/profile_options_dto.dart';
|
||||
import 'package:food_health/api/dto/user_profile_dto.dart';
|
||||
import 'package:food_health/api/network/request.dart';
|
||||
|
||||
///获取用户档案
|
||||
Future<UserProfileDto> getUserProfileApi() async {
|
||||
var res = await Request().get("/user/profile");
|
||||
return UserProfileDto.fromJson(res);
|
||||
}
|
||||
|
||||
///获取档案选项
|
||||
Future<List<ProfileOptionDto>> getProfileOptionsApi() async {
|
||||
var res = await Request().get("/user/get_user_profile_options");
|
||||
return (res as List).map((e) => ProfileOptionDto.fromJson(e)).toList();
|
||||
}
|
||||
|
||||
///更新档案
|
||||
Future<void> updateProfileApi(UserProfileDto userProfile) async {
|
||||
await Request().post("/user/update_profile", {
|
||||
"age_range": userProfile.ageRange,
|
||||
"food_allergies": userProfile.foodAllergiesList,
|
||||
"dietary_preferences": userProfile.dietaryPreferencesList,
|
||||
"medical_information": userProfile.medicalInformationList,
|
||||
"current_medications": userProfile.currentMedicationsList,
|
||||
"activity_level": userProfile.activityLevel,
|
||||
});
|
||||
}
|
||||
49
lib/api/endpoints/user_api.dart
Normal file
49
lib/api/endpoints/user_api.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'package:food_health/api/dto/login_dto.dart';
|
||||
import 'package:food_health/api/network/request.dart';
|
||||
import 'package:food_health/data/models/other_login_type.dart';
|
||||
|
||||
///检查是否注册
|
||||
Future<bool> checkRegisterApi(String email) async {
|
||||
var res = await Request().post("/auth/email_check", {
|
||||
"email": email,
|
||||
});
|
||||
if (res["next"] == "login") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
///邮箱密码登陆
|
||||
Future<LoginDto> loginApi(String email, String password) async {
|
||||
var res = await Request().post("/auth/login/account", {
|
||||
"email": email,
|
||||
"password": password,
|
||||
});
|
||||
return LoginDto.fromJson(res);
|
||||
}
|
||||
|
||||
///注册处
|
||||
Future<LoginDto> registerApi(String email, String password, String code) async {
|
||||
var res = await Request().post("/auth/register", {
|
||||
"email": email,
|
||||
"password": password,
|
||||
"email_code": code,
|
||||
});
|
||||
return LoginDto.fromJson(res);
|
||||
}
|
||||
|
||||
///发送邮箱验证码
|
||||
Future<void> sendEmailCodeApi(String email) async {
|
||||
return Request().post("/send_email_code", {
|
||||
"email": email,
|
||||
});
|
||||
}
|
||||
|
||||
///三方登录
|
||||
Future<LoginDto> thirdLoginApi(String token, OtherLoginType type) async {
|
||||
var res = await Request().post("/auth/login/oauth", {
|
||||
"login_token": token,
|
||||
"login_type": type.value,
|
||||
});
|
||||
return LoginDto.fromJson(res);
|
||||
}
|
||||
64
lib/api/network/interceptor.dart
Normal file
64
lib/api/network/interceptor.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import '../../providers/app_store.dart';
|
||||
import '../dto/base_dto.dart';
|
||||
|
||||
///请求拦截器
|
||||
void onRequest(
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
String token = await AppStore.getToken();
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
///响应拦截器
|
||||
void onResponse(
|
||||
Response<dynamic> response,
|
||||
ResponseInterceptorHandler handler,
|
||||
) {
|
||||
var apiResponse = ApiDto.fromJson(response.data);
|
||||
if (apiResponse.code == 1) {
|
||||
response.data = apiResponse.data;
|
||||
return handler.next(response);
|
||||
} else {
|
||||
showError(apiResponse.message);
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: response.requestOptions,
|
||||
response: response,
|
||||
error: {'code': 0, 'message': apiResponse.message},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///错误响应
|
||||
void onError(
|
||||
DioException e,
|
||||
ErrorInterceptorHandler handler,
|
||||
) {
|
||||
var title = "";
|
||||
if (e.type == DioExceptionType.connectionTimeout) {
|
||||
title = "请求超时";
|
||||
} else if (e.type == DioExceptionType.badResponse) {
|
||||
if (e.response?.statusCode == 404) {
|
||||
title = "接口404不存在";
|
||||
} else {
|
||||
title = "500";
|
||||
}
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
title = "网络连接失败";
|
||||
} else {
|
||||
title = "异常其他错误";
|
||||
}
|
||||
showError(title);
|
||||
handler.next(e);
|
||||
}
|
||||
|
||||
///显示错误信息
|
||||
void showError(String message) {
|
||||
EasyLoading.showError(message);
|
||||
}
|
||||
46
lib/api/network/request.dart
Normal file
46
lib/api/network/request.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../config/env.dart';
|
||||
import 'interceptor.dart';
|
||||
|
||||
class Request {
|
||||
static final Request _instance = Request._internal();
|
||||
static Dio _dio = Dio();
|
||||
|
||||
//返回单例
|
||||
factory Request() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
//初始化
|
||||
Request._internal() {
|
||||
//创建基本配置
|
||||
final BaseOptions options = BaseOptions(
|
||||
baseUrl: Config.baseUrl(),
|
||||
connectTimeout: const Duration(seconds: 30),
|
||||
receiveTimeout: const Duration(seconds: 30),
|
||||
);
|
||||
|
||||
_dio = Dio(options);
|
||||
|
||||
_dio.interceptors.add(
|
||||
InterceptorsWrapper(
|
||||
onRequest: onRequest,
|
||||
onResponse: onResponse,
|
||||
onError: onError,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///get请求
|
||||
Future<T> get<T>(String path, [Map<String, dynamic>? params]) async {
|
||||
var res = await _dio.get(path, queryParameters: params);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
///post请求
|
||||
Future<dynamic> post(String path, Object? data) async {
|
||||
var res = await _dio.post(path, data: data);
|
||||
return res.data;
|
||||
}
|
||||
}
|
||||
14
lib/api/network/safe.dart
Normal file
14
lib/api/network/safe.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
/// 网络请求的错误处理封装
|
||||
Future<T> safeRequest<T>(
|
||||
Future<T> request, {
|
||||
void Function(DioException error)? onError,
|
||||
}) async {
|
||||
try {
|
||||
return await request;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e); // 额外 hook
|
||||
rethrow; // 继续往上传
|
||||
}
|
||||
}
|
||||
23
lib/config/app_context.dart
Normal file
23
lib/config/app_context.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AppContext {
|
||||
static late BuildContext _context;
|
||||
|
||||
AppContext._();
|
||||
|
||||
///私有构造函数,防止外部实例化
|
||||
static void setContent(BuildContext context) {
|
||||
_context = context;
|
||||
}
|
||||
|
||||
///获取全局上下文
|
||||
static BuildContext get context => _context;
|
||||
|
||||
///获取主题
|
||||
static TextTheme get textTheme => Theme.of(_context).textTheme;
|
||||
|
||||
static ColorScheme get colorScheme => Theme.of(_context).colorScheme;
|
||||
|
||||
///页面内间距
|
||||
static double get pagePadding => 15;
|
||||
}
|
||||
19
lib/config/env.dart
Normal file
19
lib/config/env.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
///环境配置
|
||||
class Config {
|
||||
Config._();
|
||||
|
||||
///获取环境
|
||||
static String getEnv() {
|
||||
const env = String.fromEnvironment('ENV', defaultValue: 'dev');
|
||||
return env;
|
||||
}
|
||||
|
||||
///获取接口地址
|
||||
static String baseUrl() {
|
||||
if (getEnv() == 'dev') {
|
||||
return 'https://food-api.curain.ai/api';
|
||||
} else {
|
||||
return 'https://food-api.curain.ai/api';
|
||||
}
|
||||
}
|
||||
}
|
||||
14
lib/config/theme/custom_colors.dart
Normal file
14
lib/config/theme/custom_colors.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
///扩展颜色
|
||||
extension CustomColors on ColorScheme {
|
||||
Color get success => const Color(0xff57be80);
|
||||
|
||||
Color get warning => const Color(0xffff9800);
|
||||
|
||||
Color get info => const Color(0xff909399);
|
||||
|
||||
Color get danger => const Color(0xfff44545);
|
||||
|
||||
Color get primaryEnd => const Color(0xff06b6d4);
|
||||
}
|
||||
30
lib/config/theme/theme.dart
Normal file
30
lib/config/theme/theme.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
///颜色
|
||||
final scheme = ColorScheme.fromSeed(
|
||||
primary: Color(0xff3784f1),
|
||||
seedColor: Color(0xff3885f2),
|
||||
brightness: Brightness.light,
|
||||
//卡片色
|
||||
surface: Colors.white,
|
||||
surfaceContainerLow: Color(0xFFF4F8FB),
|
||||
surfaceContainer: Color(0xFFE9ECF3),
|
||||
surfaceContainerHigh: Color(0xFFDDE2EA),
|
||||
//颜色
|
||||
onSurfaceVariant: Color(0xFF828282),
|
||||
|
||||
shadow: Color.fromRGBO(0, 0, 0, 0.1),
|
||||
);
|
||||
|
||||
///字体
|
||||
final textTheme = TextTheme(
|
||||
titleLarge: TextStyle(fontSize: 24, fontWeight: FontWeight.w700, color: scheme.onSurface),
|
||||
titleMedium: TextStyle(fontSize: 20, fontWeight: FontWeight.w700, color: scheme.onSurface),
|
||||
titleSmall: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: scheme.onSurface),
|
||||
bodyLarge: TextStyle(fontSize: 18),
|
||||
bodyMedium: TextStyle(fontSize: 16),
|
||||
bodySmall: TextStyle(fontSize: 14),
|
||||
labelLarge: TextStyle(fontSize: 16, color: scheme.onSurfaceVariant),
|
||||
labelMedium: TextStyle(fontSize: 14, color: scheme.onSurfaceVariant),
|
||||
labelSmall: TextStyle(fontSize: 12, color: scheme.onSurfaceVariant),
|
||||
);
|
||||
48
lib/data/local/storage.dart
Normal file
48
lib/data/local/storage.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class Storage {
|
||||
//存储数据
|
||||
static Future<void> set(String key, dynamic value) async {
|
||||
SharedPreferences sp = await SharedPreferences.getInstance();
|
||||
if (value is String) {
|
||||
sp.setString(key, value);
|
||||
} else if (value is int) {
|
||||
sp.setInt(key, value);
|
||||
} else if (value is bool) {
|
||||
sp.setBool(key, value);
|
||||
} else if (value is double) {
|
||||
sp.setDouble(key, value);
|
||||
} else if (value is Map) {
|
||||
String jsonStr = jsonEncode(value);
|
||||
sp.setString(key, jsonStr);
|
||||
}
|
||||
}
|
||||
|
||||
//获取数据
|
||||
static Future<dynamic> get(String key) async {
|
||||
SharedPreferences sp = await SharedPreferences.getInstance();
|
||||
var value = sp.get(key);
|
||||
if (value is String) {
|
||||
try {
|
||||
return jsonDecode(value);
|
||||
} catch (e) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
//删除数据
|
||||
static Future<void> remove(key) async {
|
||||
SharedPreferences sp = await SharedPreferences.getInstance();
|
||||
sp.remove(key);
|
||||
}
|
||||
|
||||
//判断键是否存在
|
||||
static Future<bool> hasKey(String key) async {
|
||||
SharedPreferences sp = await SharedPreferences.getInstance();
|
||||
return sp.containsKey(key);
|
||||
}
|
||||
}
|
||||
8
lib/data/models/other_login_type.dart
Normal file
8
lib/data/models/other_login_type.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
enum OtherLoginType {
|
||||
google('google'),
|
||||
apple('apple');
|
||||
|
||||
const OtherLoginType(this.value);
|
||||
|
||||
final String value;
|
||||
}
|
||||
22
lib/data/models/skin_check_status.dart
Normal file
22
lib/data/models/skin_check_status.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
enum SkinCheckStatus {
|
||||
/// 正常
|
||||
normal(1),
|
||||
|
||||
/// 警告
|
||||
warning(2),
|
||||
|
||||
/// 危险
|
||||
danger(3),
|
||||
|
||||
/// 未知
|
||||
unknown(0);
|
||||
|
||||
final int value;
|
||||
|
||||
const SkinCheckStatus(this.value);
|
||||
|
||||
/// 根据 int 值返回对应的枚举,默认返回 online
|
||||
static SkinCheckStatus fromValue(int value) {
|
||||
return SkinCheckStatus.values.firstWhere((e) => e.value == value, orElse: () => SkinCheckStatus.unknown);
|
||||
}
|
||||
}
|
||||
65
lib/layout/layout_page.dart
Normal file
65
lib/layout/layout_page.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
import '../page/home/home_page.dart';
|
||||
import '../page/profile/my/my_page.dart';
|
||||
import '../page/record/list/record_list_page.dart';
|
||||
import 'tabbar.dart';
|
||||
|
||||
class LayoutPage extends StatefulWidget {
|
||||
const LayoutPage({super.key});
|
||||
|
||||
@override
|
||||
State<LayoutPage> createState() => _LayoutPageState();
|
||||
}
|
||||
|
||||
class _LayoutPageState extends State<LayoutPage> {
|
||||
///分页
|
||||
final PageController _pageController = PageController(initialPage: 0);
|
||||
|
||||
int get currentPage {
|
||||
if (!_pageController.hasClients) return 1; // 没 attach 直接 0
|
||||
return _pageController.page?.round() ?? 1;
|
||||
}
|
||||
|
||||
//TabBar列表
|
||||
final List<PageItem> _pages = [
|
||||
PageItem(
|
||||
name: "Home",
|
||||
icon: RemixIcons.home_2_line,
|
||||
page: HomePage(),
|
||||
),
|
||||
PageItem(name: "record", icon: RemixIcons.history_line, page: RecordListPage()),
|
||||
PageItem(
|
||||
name: "Home",
|
||||
icon: RemixIcons.book_open_line,
|
||||
page: MyPage(),
|
||||
),
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: PageView(
|
||||
controller: _pageController,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
children: _pages.map((item) => item.page).toList(),
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
currentIndex: currentPage,
|
||||
onTap: (index) {
|
||||
_pageController.jumpToPage(index);
|
||||
setState(() {});
|
||||
},
|
||||
items: _pages.map((item) {
|
||||
return BottomNavigationBarItem(
|
||||
icon: Icon(item.icon),
|
||||
label: item.name,
|
||||
);
|
||||
}).toList(),
|
||||
showSelectedLabels: false,
|
||||
showUnselectedLabels: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
9
lib/layout/tabbar.dart
Normal file
9
lib/layout/tabbar.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class PageItem {
|
||||
final String name;
|
||||
final IconData icon;
|
||||
final Widget page;
|
||||
|
||||
PageItem({required this.name, required this.icon, required this.page});
|
||||
}
|
||||
55
lib/main.dart
Normal file
55
lib/main.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:food_health/router/routes.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'config/theme/theme.dart';
|
||||
import 'providers/app_store.dart';
|
||||
|
||||
void main() {
|
||||
runApp(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(create: (context) => AppStore()),
|
||||
],
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ScreenUtilInit(
|
||||
designSize: const Size(375, 694),
|
||||
useInheritedMediaQuery: true,
|
||||
child: MaterialApp.router(
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerConfig: goRouter,
|
||||
localizationsDelegates: [],
|
||||
themeMode: ThemeMode.light,
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: scheme,
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: scheme.primary,
|
||||
foregroundColor: scheme.onPrimary,
|
||||
),
|
||||
),
|
||||
textTheme: textTheme,
|
||||
scaffoldBackgroundColor: Color(0xffFAFAFE),
|
||||
appBarTheme: AppBarTheme(
|
||||
backgroundColor: scheme.surface,
|
||||
scrolledUnderElevation: 0,
|
||||
titleTextStyle: textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
builder: EasyLoading.init(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
30
lib/page/home/home_page.dart
Normal file
30
lib/page/home/home_page.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:food_health/page/home/widget/home_header.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../widgets/common/app_backend.dart';
|
||||
import '../../widgets/common/app_header.dart';
|
||||
import 'widget/upload_panel.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
State<HomePage> createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
return Scaffold(
|
||||
resizeToAvoidBottomInset: false,
|
||||
body: AppBackend(
|
||||
child: ListView(
|
||||
children: [AppHeader(), HomeHeader(), UploadPanel()],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
28
lib/page/home/widget/home_header.dart
Normal file
28
lib/page/home/widget/home_header.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class HomeHeader extends StatelessWidget {
|
||||
const HomeHeader({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 30, bottom: 40),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
"Food Safety Scanner",
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 10),
|
||||
child: Text(
|
||||
"Upload a photo to check if this food is safe for you",
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
193
lib/page/home/widget/upload_panel.dart
Normal file
193
lib/page/home/widget/upload_panel.dart
Normal file
@@ -0,0 +1,193 @@
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:food_health/api/endpoints/food_api.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
class UploadPanel extends StatefulWidget {
|
||||
const UploadPanel({super.key});
|
||||
|
||||
@override
|
||||
State<UploadPanel> createState() => _UploadPanelState();
|
||||
}
|
||||
|
||||
class _UploadPanelState extends State<UploadPanel> {
|
||||
final ImagePicker _picker = ImagePicker();
|
||||
|
||||
///打开相机拍照
|
||||
void _handTakePhoto() async {
|
||||
var photo = await _picker.pickImage(source: ImageSource.camera);
|
||||
if (photo != null) {
|
||||
_startDetect(photo.path);
|
||||
}
|
||||
}
|
||||
|
||||
///选择图片
|
||||
void _handPickImage() async {
|
||||
var result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.image,
|
||||
allowMultiple: false,
|
||||
);
|
||||
if (result != null) {
|
||||
_startDetect(result.files[0].path!);
|
||||
}
|
||||
}
|
||||
|
||||
///开始检测
|
||||
void _startDetect(String path) async {
|
||||
//压缩
|
||||
final result = await FlutterImageCompress.compressWithFile(
|
||||
path,
|
||||
minWidth: 1080,
|
||||
minHeight: 1920,
|
||||
quality: 85,
|
||||
rotate: 0,
|
||||
);
|
||||
EasyLoading.show(
|
||||
status: 'Checking, please wait...',
|
||||
maskType: EasyLoadingMaskType.clear,
|
||||
);
|
||||
var res = await foodScanApi(result!);
|
||||
EasyLoading.dismiss();
|
||||
context.push(RoutePaths.detail, extra: res);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).cardColor,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Color(0x1A000000),
|
||||
spreadRadius: 2,
|
||||
blurRadius: 5,
|
||||
offset: Offset(1, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(30),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
width: 70,
|
||||
height: 70,
|
||||
margin: EdgeInsets.only(bottom: 20),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Theme.of(context).colorScheme.primary,
|
||||
Theme.of(context).colorScheme.primaryEnd,
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Icon(
|
||||
RemixIcons.image_line,
|
||||
color: Colors.white,
|
||||
size: 26,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
"Upload Food Photo",
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 5),
|
||||
child: Text(
|
||||
"Take a clear photo of your food and we'll analyze it for safety based on your health profile",
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 40, bottom: 20),
|
||||
child: Column(
|
||||
spacing: 20,
|
||||
children: [
|
||||
_buttonItem(
|
||||
title: "Take Photo",
|
||||
icon: RemixIcons.camera_line,
|
||||
style: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Theme.of(context).colorScheme.primary,
|
||||
Theme.of(context).colorScheme.primaryEnd,
|
||||
],
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
_handTakePhoto();
|
||||
},
|
||||
),
|
||||
_buttonItem(
|
||||
title: "Upload File",
|
||||
icon: RemixIcons.upload_2_line,
|
||||
color: Colors.black,
|
||||
style: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
),
|
||||
onTap: () {
|
||||
_handPickImage();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Text(
|
||||
"Supports JPG, PNG, HEIC formats • Max 10MB",
|
||||
style: Theme.of(context).textTheme.labelSmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buttonItem({
|
||||
required String title,
|
||||
required IconData icon,
|
||||
Color color = Colors.white,
|
||||
required BoxDecoration style,
|
||||
required VoidCallback onTap,
|
||||
}) {
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
width: 170,
|
||||
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
|
||||
decoration: style.copyWith(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
spacing: 10,
|
||||
children: [
|
||||
Icon(icon, color: color, size: 20),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
25
lib/page/profile/edit/data/state.dart
Normal file
25
lib/page/profile/edit/data/state.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/user_profile_dto.dart';
|
||||
|
||||
class SelectionState extends ChangeNotifier {
|
||||
UserProfileDto userProfile = UserProfileDto();
|
||||
|
||||
SelectionState(this.userProfile);
|
||||
|
||||
void update(void Function(UserProfileDto) updater) {
|
||||
updater(userProfile);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class SelectionProvider extends InheritedNotifier<SelectionState> {
|
||||
const SelectionProvider({
|
||||
super.key,
|
||||
required SelectionState super.notifier,
|
||||
required super.child,
|
||||
});
|
||||
|
||||
static SelectionState of(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<SelectionProvider>()!.notifier!;
|
||||
}
|
||||
}
|
||||
292
lib/page/profile/edit/my_edit_page.dart
Normal file
292
lib/page/profile/edit/my_edit_page.dart
Normal file
@@ -0,0 +1,292 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:food_health/api/dto/profile_options_dto.dart';
|
||||
import 'package:food_health/api/dto/user_profile_dto.dart';
|
||||
import 'package:food_health/api/endpoints/profile_api.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:food_health/page/profile/edit/widget/food_allergies.dart';
|
||||
import 'package:food_health/widgets/common/app_backend.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
import 'data/state.dart';
|
||||
import 'widget/dietary_preferences.dart';
|
||||
import 'widget/health_profile.dart';
|
||||
|
||||
class MyEditPage extends StatefulWidget {
|
||||
final UserProfileDto userProfile;
|
||||
|
||||
const MyEditPage({super.key, required this.userProfile});
|
||||
|
||||
@override
|
||||
State<MyEditPage> createState() => _MyEditPageState();
|
||||
}
|
||||
|
||||
class _MyEditPageState extends State<MyEditPage> {
|
||||
late SelectionState selectionState;
|
||||
|
||||
List<ProfileOptionDto> _options = [];
|
||||
|
||||
var _loading = true;
|
||||
|
||||
///步骤
|
||||
var _step = 0;
|
||||
|
||||
var stepList = [
|
||||
StepItem(
|
||||
title: "Food Allergies",
|
||||
icon: RemixIcons.shield_line,
|
||||
subTitle: "Help us keep you safe by telling us about your allergies",
|
||||
),
|
||||
StepItem(
|
||||
title: "Dietary Preferences",
|
||||
icon: RemixIcons.heart_line,
|
||||
subTitle: "What dietary restrictions or preferences do you follow?",
|
||||
),
|
||||
StepItem(
|
||||
title: "Health Profile",
|
||||
icon: RemixIcons.user_line,
|
||||
subTitle: "Share relevant health information for personalized recommendations",
|
||||
),
|
||||
];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_init();
|
||||
}
|
||||
|
||||
void _init() async {
|
||||
selectionState = SelectionState(widget.userProfile);
|
||||
var res = await getProfileOptionsApi();
|
||||
setState(() {
|
||||
_options = res;
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
///设置步骤
|
||||
void _handStep(bool isNext) {
|
||||
if (_step == 2 && isNext) {
|
||||
_submit();
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_step = (_step + (isNext ? 1 : -1)).clamp(0, 2);
|
||||
});
|
||||
}
|
||||
|
||||
void _submit() async {
|
||||
EasyLoading.show(
|
||||
status: 'Saving…',
|
||||
maskType: EasyLoadingMaskType.clear,
|
||||
);
|
||||
await updateProfileApi(selectionState.userProfile);
|
||||
EasyLoading.dismiss();
|
||||
context.pop();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_loading) {
|
||||
return Center(child: CircularProgressIndicator());
|
||||
}
|
||||
return Scaffold(
|
||||
body: AppBackend(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.only(top: 30),
|
||||
children: [
|
||||
buildHeader(),
|
||||
buildStep(),
|
||||
buildStepInfo(),
|
||||
SelectionProvider(
|
||||
notifier: selectionState,
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
if (_step == 0) {
|
||||
return FoodAllergies(
|
||||
options: _options,
|
||||
);
|
||||
} else if (_step == 1) {
|
||||
return DietaryPreferences(
|
||||
options: _options,
|
||||
);
|
||||
} else if (_step == 2) {
|
||||
return HealthProfile(
|
||||
options: _options,
|
||||
);
|
||||
}
|
||||
return SizedBox();
|
||||
},
|
||||
),
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 30),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Opacity(
|
||||
opacity: _step == 0 ? 0.4 : 1,
|
||||
child: buildItemButton(
|
||||
title: "Previous",
|
||||
color: Colors.black,
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
),
|
||||
onTap: () {
|
||||
_handStep(false);
|
||||
},
|
||||
),
|
||||
),
|
||||
buildItemButton(
|
||||
title: _step == 2 ? "Complete Setup" : "Continue",
|
||||
decoration: BoxDecoration(
|
||||
color: _step == 2 ? Theme.of(context).colorScheme.success : null,
|
||||
gradient: _step == 2
|
||||
? null
|
||||
: LinearGradient(
|
||||
colors: [
|
||||
Theme.of(context).colorScheme.primary,
|
||||
Theme.of(context).colorScheme.primaryEnd,
|
||||
],
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
_handStep(true);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///构建顶部
|
||||
Widget buildHeader() {
|
||||
return Column(
|
||||
children: [
|
||||
Text(
|
||||
"Welcome to FoodSafe",
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 5),
|
||||
child: Text(
|
||||
"Let's personalize your food safety experience",
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
///步骤条
|
||||
Widget buildStep() {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 20),
|
||||
child: Row(
|
||||
spacing: 10,
|
||||
children: stepList.asMap().entries.map((entre) {
|
||||
//数据
|
||||
var item = entre.value;
|
||||
var index = entre.key;
|
||||
var isLast = index == stepList.length - 1;
|
||||
//颜色
|
||||
var selectColor = Theme.of(context).colorScheme.primary;
|
||||
var unselectedColor = Theme.of(context).colorScheme.surfaceContainerHigh;
|
||||
return Expanded(
|
||||
flex: isLast ? 0 : 1,
|
||||
child: Row(
|
||||
spacing: 10,
|
||||
children: [
|
||||
Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
decoration: BoxDecoration(
|
||||
color: _step >= index ? selectColor : unselectedColor,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
item.icon,
|
||||
color: _step >= index ? Colors.white : Color(0xff9ca3af),
|
||||
),
|
||||
),
|
||||
|
||||
Visibility(
|
||||
visible: !isLast,
|
||||
child: Expanded(
|
||||
child: Container(
|
||||
height: 3,
|
||||
color: _step > index ? selectColor : unselectedColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///步骤条信息
|
||||
Widget buildStepInfo() {
|
||||
var stepInfo = stepList[_step];
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 20, bottom: 30),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
stepInfo.title,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 5),
|
||||
child: Text(
|
||||
stepInfo.subTitle,
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///item按钮
|
||||
Widget buildItemButton({
|
||||
required String title,
|
||||
Color color = Colors.white,
|
||||
required BoxDecoration decoration,
|
||||
required Function() onTap,
|
||||
}) {
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
|
||||
decoration: decoration.copyWith(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
title,
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class StepItem {
|
||||
final IconData icon;
|
||||
final String title;
|
||||
final String subTitle;
|
||||
|
||||
StepItem({
|
||||
required this.icon,
|
||||
required this.title,
|
||||
required this.subTitle,
|
||||
});
|
||||
}
|
||||
9
lib/page/profile/edit/util/common.dart
Normal file
9
lib/page/profile/edit/util/common.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
import 'package:food_health/api/dto/profile_options_dto.dart';
|
||||
|
||||
List<String> getOptions(List<ProfileOptionDto> options, String key) {
|
||||
var data = options.firstWhere((item) {
|
||||
return item.key == key;
|
||||
});
|
||||
//
|
||||
return data.valuesList ?? [];
|
||||
}
|
||||
100
lib/page/profile/edit/widget/common.dart
Normal file
100
lib/page/profile/edit/widget/common.dart
Normal file
@@ -0,0 +1,100 @@
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
///步骤内容卡片
|
||||
class StepContentCard extends StatelessWidget {
|
||||
final List<Widget> children;
|
||||
|
||||
const StepContentCard({super.key, required this.children});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Theme.of(context).colorScheme.shadow,
|
||||
blurRadius: 7,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: children,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///卡片标题
|
||||
class CardTitle extends StatelessWidget {
|
||||
final String title;
|
||||
|
||||
const CardTitle({super.key, required this.title});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 15),
|
||||
child: Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.titleSmall,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///配置列表
|
||||
class OptionList extends StatelessWidget {
|
||||
final List<String> options;
|
||||
final List<String> selects;
|
||||
final double widthFactor;
|
||||
final Function(String) onTap;
|
||||
|
||||
const OptionList({
|
||||
super.key,
|
||||
required this.options,
|
||||
required this.selects,
|
||||
this.widthFactor = 0.5,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Wrap(
|
||||
runSpacing: 20,
|
||||
children: options.map((item) {
|
||||
return FractionallySizedBox(
|
||||
widthFactor: widthFactor,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
onTap(item);
|
||||
},
|
||||
child: Row(
|
||||
spacing: 5,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: Checkbox(
|
||||
value: selects.contains(item),
|
||||
onChanged: (_) {
|
||||
onTap(item);
|
||||
},
|
||||
),
|
||||
),
|
||||
Text(item),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
127
lib/page/profile/edit/widget/dietary_preferences.dart
Normal file
127
lib/page/profile/edit/widget/dietary_preferences.dart
Normal file
@@ -0,0 +1,127 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/profile_options_dto.dart';
|
||||
|
||||
import '../data/state.dart';
|
||||
import '../util/common.dart';
|
||||
import 'common.dart';
|
||||
|
||||
class DietaryPreferences extends StatefulWidget {
|
||||
final List<ProfileOptionDto> options;
|
||||
|
||||
const DietaryPreferences({super.key, required this.options});
|
||||
|
||||
@override
|
||||
State<DietaryPreferences> createState() => _DietaryPreferencesState();
|
||||
}
|
||||
|
||||
class _DietaryPreferencesState extends State<DietaryPreferences> {
|
||||
///切换标签
|
||||
void _handToggle(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
if (getIsSelect(tag)) {
|
||||
state.update((p) => p.dietaryPreferencesList.remove(tag));
|
||||
} else {
|
||||
state.update((p) => p.dietaryPreferencesList.add(tag));
|
||||
}
|
||||
}
|
||||
|
||||
///选中年龄
|
||||
void _handAgeRange(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
state.update((p) => p.ageRange = tag);
|
||||
}
|
||||
|
||||
///等级
|
||||
void _handActivityLevel(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
state.update((p) => p.activityLevel = tag);
|
||||
}
|
||||
|
||||
///是否标签选中
|
||||
bool getIsSelect(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
return state.userProfile.dietaryPreferencesList.contains(tag);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var state = SelectionProvider.of(context);
|
||||
return StepContentCard(
|
||||
children: [
|
||||
CardTitle(title: "Dietary Restrictions & Preferences"),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 15),
|
||||
child: OptionList(
|
||||
options: getOptions(widget.options, "dietary_restrictions"),
|
||||
selects: state.userProfile.dietaryPreferencesList,
|
||||
onTap: _handToggle,
|
||||
),
|
||||
),
|
||||
CardTitle(title: "Age Range"),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 15),
|
||||
child: RadioGroup(
|
||||
options: getOptions(widget.options, "age_ranges"),
|
||||
value: state.userProfile.ageRange,
|
||||
onChanged: _handAgeRange,
|
||||
),
|
||||
),
|
||||
CardTitle(title: "Activity Level"),
|
||||
RadioGroup(
|
||||
options: getOptions(widget.options, "activity_levels"),
|
||||
value: state.userProfile.activityLevel,
|
||||
onChanged: _handActivityLevel,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///单选列表
|
||||
class RadioGroup extends StatelessWidget {
|
||||
final List<String> options;
|
||||
final String value;
|
||||
final int crossAxisCount;
|
||||
final Function(String) onChanged;
|
||||
|
||||
const RadioGroup({
|
||||
super.key,
|
||||
required this.options,
|
||||
required this.value,
|
||||
this.crossAxisCount = 3,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisSpacing: 10,
|
||||
mainAxisSpacing: 10,
|
||||
crossAxisCount: crossAxisCount,
|
||||
mainAxisExtent: 40,
|
||||
),
|
||||
itemBuilder: (context, index) {
|
||||
var data = options[index];
|
||||
var isSelected = value == data;
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
onChanged(data);
|
||||
},
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.surfaceContainerLow,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: isSelected ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.surfaceContainer),
|
||||
),
|
||||
child: Text(data, style: TextStyle(color: isSelected ? Colors.white : Colors.black)),
|
||||
),
|
||||
);
|
||||
},
|
||||
itemCount: options.length,
|
||||
);
|
||||
}
|
||||
}
|
||||
144
lib/page/profile/edit/widget/food_allergies.dart
Normal file
144
lib/page/profile/edit/widget/food_allergies.dart
Normal file
@@ -0,0 +1,144 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/profile_options_dto.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
import '../data/state.dart';
|
||||
import '../util/common.dart';
|
||||
import 'common.dart';
|
||||
|
||||
class FoodAllergies extends StatefulWidget {
|
||||
final List<ProfileOptionDto> options;
|
||||
|
||||
const FoodAllergies({super.key, required this.options});
|
||||
|
||||
@override
|
||||
State<FoodAllergies> createState() => _FoodAllergiesState();
|
||||
}
|
||||
|
||||
class _FoodAllergiesState extends State<FoodAllergies> {
|
||||
final TextEditingController _otherController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
///切换标签
|
||||
void _handToggle(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
if (getIsSelect(tag)) {
|
||||
state.update((p) => p.foodAllergiesList.remove(tag));
|
||||
} else {
|
||||
state.update((p) => p.foodAllergiesList.add(tag));
|
||||
}
|
||||
}
|
||||
|
||||
void _handConfirmCustom() {
|
||||
var state = SelectionProvider.of(context);
|
||||
if (!getIsSelect(_otherController.text)) {
|
||||
state.update((p) => p.foodAllergiesList.add(_otherController.text));
|
||||
_otherController.text = "";
|
||||
}
|
||||
}
|
||||
|
||||
///是否标签选中
|
||||
bool getIsSelect(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
return state.userProfile.foodAllergiesList.contains(tag);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var state = SelectionProvider.of(context);
|
||||
return StepContentCard(
|
||||
children: [
|
||||
CardTitle(title: "Common Food Allergies"),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 15),
|
||||
child: OptionList(
|
||||
options: getOptions(widget.options, "common_food_allergies"),
|
||||
selects: state.userProfile.foodAllergiesList,
|
||||
onTap: _handToggle,
|
||||
),
|
||||
),
|
||||
CardTitle(title: "Other Allergies"),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 15),
|
||||
child: Row(
|
||||
spacing: 15,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _otherController,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
decoration: InputDecoration(
|
||||
isCollapsed: true,
|
||||
contentPadding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
||||
hintText: "Add custom allergy...",
|
||||
filled: true,
|
||||
fillColor: Theme.of(context).colorScheme.surfaceContainerLow,
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: BorderSide(width: 1, color: Theme.of(context).colorScheme.surfaceContainer),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: BorderSide(width: 1, color: Theme.of(context).colorScheme.surfaceContainer),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
InkWell(
|
||||
onTap: _handConfirmCustom,
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Icon(RemixIcons.add_fill),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
CardTitle(title: "Your Allergies"),
|
||||
Wrap(
|
||||
runSpacing: 10,
|
||||
spacing: 10,
|
||||
children: state.userProfile.foodAllergiesList.map((item) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
_handToggle(item);
|
||||
},
|
||||
child: Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 3, horizontal: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.danger,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
spacing: 5,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
item,
|
||||
style: TextStyle(color: Colors.white, fontSize: 12),
|
||||
),
|
||||
Icon(
|
||||
RemixIcons.close_fill,
|
||||
color: Colors.white,
|
||||
size: 20,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
151
lib/page/profile/edit/widget/health_profile.dart
Normal file
151
lib/page/profile/edit/widget/health_profile.dart
Normal file
@@ -0,0 +1,151 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/profile_options_dto.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
import '../data/state.dart';
|
||||
import '../util/common.dart';
|
||||
import 'common.dart';
|
||||
|
||||
class HealthProfile extends StatefulWidget {
|
||||
final List<ProfileOptionDto> options;
|
||||
|
||||
const HealthProfile({super.key, required this.options});
|
||||
|
||||
@override
|
||||
State<HealthProfile> createState() => _HealthProfileState();
|
||||
}
|
||||
|
||||
class _HealthProfileState extends State<HealthProfile> {
|
||||
final TextEditingController _otherController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
///切换标签
|
||||
void _handToggle(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
if (getIsSelect(tag)) {
|
||||
state.update((p) => p.medicalInformationList.remove(tag));
|
||||
} else {
|
||||
state.update((p) => p.medicalInformationList.add(tag));
|
||||
}
|
||||
}
|
||||
|
||||
///确认搜索内容
|
||||
void _handConfirmCustom() {
|
||||
var state = SelectionProvider.of(context);
|
||||
if (!getIsSelect(_otherController.text)) {
|
||||
state.update((p) => p.currentMedicationsList.add(_otherController.text));
|
||||
_otherController.text = "";
|
||||
}
|
||||
}
|
||||
|
||||
///移除搜索标签
|
||||
void _handRemoveCustom(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
state.update((p) => p.currentMedicationsList.remove(tag));
|
||||
}
|
||||
|
||||
///是否标签选中
|
||||
bool getIsSelect(String tag) {
|
||||
var state = SelectionProvider.of(context);
|
||||
return state.userProfile.medicalInformationList.contains(tag);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var state = SelectionProvider.of(context);
|
||||
return StepContentCard(
|
||||
children: [
|
||||
CardTitle(title: "Medical Conditions"),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 15),
|
||||
child: OptionList(
|
||||
widthFactor: 1,
|
||||
options: getOptions(widget.options, "medical_conditions"),
|
||||
selects: state.userProfile.medicalInformationList,
|
||||
onTap: _handToggle,
|
||||
),
|
||||
),
|
||||
CardTitle(title: "Current Medications"),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 15),
|
||||
child: Row(
|
||||
spacing: 15,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _otherController,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
decoration: InputDecoration(
|
||||
isCollapsed: true,
|
||||
contentPadding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
||||
hintText: "Add medication...",
|
||||
filled: true,
|
||||
fillColor: Theme.of(context).colorScheme.surfaceContainerLow,
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: BorderSide(width: 1, color: Theme.of(context).colorScheme.surfaceContainer),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: BorderSide(width: 1, color: Theme.of(context).colorScheme.surfaceContainer),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
InkWell(
|
||||
onTap: _handConfirmCustom,
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Icon(RemixIcons.add_fill),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Wrap(
|
||||
runSpacing: 10,
|
||||
spacing: 10,
|
||||
children: state.userProfile.currentMedicationsList.map((item) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
_handRemoveCustom(item);
|
||||
},
|
||||
child: Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 3, horizontal: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.danger,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
spacing: 5,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
item,
|
||||
style: TextStyle(color: Colors.white, fontSize: 12),
|
||||
),
|
||||
Icon(
|
||||
RemixIcons.close_fill,
|
||||
color: Colors.white,
|
||||
size: 20,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
155
lib/page/profile/my/my_page.dart
Normal file
155
lib/page/profile/my/my_page.dart
Normal file
@@ -0,0 +1,155 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/user_profile_dto.dart';
|
||||
import 'package:food_health/api/endpoints/profile_api.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
import '../../../router/config/route_paths.dart';
|
||||
import 'widget/title_card.dart';
|
||||
import 'widget/user_card.dart';
|
||||
|
||||
class MyPage extends StatefulWidget {
|
||||
const MyPage({super.key});
|
||||
|
||||
@override
|
||||
State<MyPage> createState() => _MyPageState();
|
||||
}
|
||||
|
||||
class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
|
||||
UserProfileDto _userProfile = UserProfileDto();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_init();
|
||||
}
|
||||
|
||||
void _init() async {
|
||||
var res = await getUserProfileApi();
|
||||
setState(() {
|
||||
_userProfile = res;
|
||||
});
|
||||
}
|
||||
|
||||
void _goEdit() async {
|
||||
await context.push(RoutePaths.myEdit, extra: _userProfile);
|
||||
_init();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SafeArea(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.all(15),
|
||||
children: [
|
||||
UserCard(
|
||||
detail: _userProfile,
|
||||
onEdit: _goEdit,
|
||||
),
|
||||
Column(
|
||||
children: [
|
||||
TitleCard(
|
||||
title: "Food Allergies",
|
||||
icon: Icon(
|
||||
RemixIcons.shield_line,
|
||||
color: Theme.of(context).colorScheme.danger,
|
||||
),
|
||||
child: buildTagList(
|
||||
emptyText: "No food allergies reported",
|
||||
tags: _userProfile.foodAllergiesList,
|
||||
color: Theme.of(context).colorScheme.danger,
|
||||
),
|
||||
),
|
||||
TitleCard(
|
||||
title: "No preferences",
|
||||
icon: Icon(
|
||||
RemixIcons.heart_line,
|
||||
color: Theme.of(context).colorScheme.success,
|
||||
),
|
||||
child: buildTagList(
|
||||
emptyText: "No dietary preferences reported",
|
||||
tags: _userProfile.dietaryPreferencesList,
|
||||
color: Theme.of(context).colorScheme.success,
|
||||
),
|
||||
),
|
||||
TitleCard(
|
||||
title: "Medical Information",
|
||||
icon: Icon(
|
||||
RemixIcons.user_line,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 10),
|
||||
child: Text("Medical Conditions"),
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 10),
|
||||
child: buildTagList(
|
||||
emptyText: "No medical conditions reported",
|
||||
tags: _userProfile.medicalInformationList,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 10),
|
||||
child: Text("Current Medications"),
|
||||
),
|
||||
buildTagList(
|
||||
emptyText: "No medications reported",
|
||||
tags: _userProfile.currentMedicationsList,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget buildTagList({
|
||||
required String emptyText,
|
||||
required List<String> tags,
|
||||
required Color color,
|
||||
}) {
|
||||
if (tags.isEmpty) {
|
||||
return Text(
|
||||
emptyText,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.success,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return Wrap(
|
||||
spacing: 15,
|
||||
runSpacing: 15,
|
||||
children: tags.map((item) {
|
||||
return Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Text(
|
||||
item,
|
||||
style: TextStyle(
|
||||
color: color,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
50
lib/page/profile/my/widget/title_card.dart
Normal file
50
lib/page/profile/my/widget/title_card.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class TitleCard extends StatelessWidget {
|
||||
final String title;
|
||||
final Widget icon;
|
||||
final Widget child;
|
||||
|
||||
const TitleCard({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.icon,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(15),
|
||||
margin: EdgeInsets.only(top: 15),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
boxShadow: [
|
||||
BoxShadow(color: Theme.of(context).colorScheme.shadow, blurRadius: 7),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
margin: EdgeInsets.only(bottom: 10),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
spacing: 10,
|
||||
children: [
|
||||
icon,
|
||||
Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
child,
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
180
lib/page/profile/my/widget/user_card.dart
Normal file
180
lib/page/profile/my/widget/user_card.dart
Normal file
@@ -0,0 +1,180 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/user_profile_dto.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:food_health/providers/app_store.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
import 'package:food_health/utils/common.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
class UserCard extends StatefulWidget {
|
||||
final UserProfileDto detail;
|
||||
final Function() onEdit;
|
||||
|
||||
const UserCard({
|
||||
super.key,
|
||||
required this.detail,
|
||||
required this.onEdit,
|
||||
});
|
||||
|
||||
@override
|
||||
State<UserCard> createState() => _UserCardState();
|
||||
}
|
||||
|
||||
class _UserCardState extends State<UserCard> {
|
||||
void _goEdit() {
|
||||
widget.onEdit();
|
||||
}
|
||||
|
||||
void _handLogout() {
|
||||
var appStore = context.read<AppStore>();
|
||||
appStore.logout();
|
||||
context.go(RoutePaths.login);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Theme.of(context).colorScheme.shadow,
|
||||
blurRadius: 7,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
avatarWidget(),
|
||||
Container(
|
||||
margin: const EdgeInsets.only(top: 12),
|
||||
child: Text(getNotEmpty(widget.detail.name) ?? "user", style: Theme.of(context).textTheme.titleMedium),
|
||||
),
|
||||
Container(
|
||||
margin: const EdgeInsets.only(top: 5),
|
||||
child: Text(widget.detail.email ?? "", style: Theme.of(context).textTheme.labelMedium),
|
||||
),
|
||||
buildTitledTags(
|
||||
title: "Age Range",
|
||||
tag: getNotEmpty(widget.detail.ageRange),
|
||||
),
|
||||
buildTitledTags(
|
||||
title: "Activity Level",
|
||||
tag: getNotEmpty(widget.detail.activityLevel),
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
btnItem(
|
||||
title: "Edit Profile",
|
||||
icon: RemixIcons.edit_box_line,
|
||||
color: Colors.white,
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Theme.of(context).colorScheme.primary,
|
||||
Theme.of(context).colorScheme.primaryEnd,
|
||||
],
|
||||
),
|
||||
),
|
||||
onTap: _goEdit,
|
||||
),
|
||||
btnItem(
|
||||
title: "Logout",
|
||||
icon: RemixIcons.logout_circle_line,
|
||||
decoration: BoxDecoration(color: Theme.of(context).colorScheme.surfaceContainer),
|
||||
onTap: _handLogout,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///头像
|
||||
Widget avatarWidget() {
|
||||
return Container(
|
||||
width: 70,
|
||||
height: 70,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Theme.of(context).colorScheme.primary,
|
||||
Theme.of(context).colorScheme.primaryEnd,
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Icon(
|
||||
RemixIcons.user_3_line,
|
||||
color: Colors.white,
|
||||
size: 30,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///标题标签
|
||||
Widget buildTitledTags({
|
||||
required String title,
|
||||
String? tag,
|
||||
}) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(top: 20),
|
||||
width: double.infinity,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title, style: Theme.of(context).textTheme.bodyMedium),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 3),
|
||||
margin: const EdgeInsets.only(top: 5),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerLow,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
tag ?? "Untitled",
|
||||
style: Theme.of(context).textTheme.labelMedium?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///按钮
|
||||
Widget btnItem({
|
||||
required String title,
|
||||
required IconData icon,
|
||||
required BoxDecoration decoration,
|
||||
Color color = Colors.black,
|
||||
required Function() onTap,
|
||||
}) {
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(top: 15),
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
decoration: decoration.copyWith(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(icon, size: 20, color: color),
|
||||
SizedBox(width: 10),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(color: color, fontWeight: FontWeight.w500),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
41
lib/page/record/detail/record_detail_page.dart
Normal file
41
lib/page/record/detail/record_detail_page.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:food_health/api/dto/food_scan_dto.dart';
|
||||
|
||||
import 'widget/detailed_analysis.dart';
|
||||
import 'widget/health_recommend.dart';
|
||||
import 'widget/result_chip.dart';
|
||||
|
||||
class RecordDetailPage extends StatefulWidget {
|
||||
final FoodScanDto detail;
|
||||
|
||||
const RecordDetailPage({super.key, required this.detail});
|
||||
|
||||
@override
|
||||
State<RecordDetailPage> createState() => _RecordDetailPageState();
|
||||
}
|
||||
|
||||
class _RecordDetailPageState extends State<RecordDetailPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Colors.transparent,
|
||||
systemOverlayStyle: SystemUiOverlayStyle(
|
||||
statusBarIconBrightness: Brightness.dark, // 状态栏图标深色
|
||||
statusBarBrightness: Brightness.light, // iOS
|
||||
),
|
||||
),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.only(left: 15, right: 15, bottom: 15),
|
||||
children: [
|
||||
ResultChip(detail: widget.detail),
|
||||
DetailedAnalysis(detail: widget.detail),
|
||||
HealthRecommend(
|
||||
detail: widget.detail,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
76
lib/page/record/detail/widget/detailed_analysis.dart
Normal file
76
lib/page/record/detail/widget/detailed_analysis.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/food_scan_dto.dart';
|
||||
import 'package:markdown_widget/widget/markdown.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
class DetailedAnalysis extends StatelessWidget {
|
||||
final FoodScanDto detail;
|
||||
|
||||
const DetailedAnalysis({super.key, required this.detail});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 20),
|
||||
padding: EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Theme.of(context).colorScheme.shadow,
|
||||
blurRadius: 10,
|
||||
offset: Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
spacing: 10,
|
||||
children: [
|
||||
Row(
|
||||
spacing: 10,
|
||||
children: [
|
||||
Icon(
|
||||
RemixIcons.eye_line,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
Text(
|
||||
"Detailed Analysis",
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
MarkdownWidget(
|
||||
shrinkWrap: true,
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
data: detail.explanation ?? "",
|
||||
),
|
||||
Row(
|
||||
spacing: 10,
|
||||
children: [
|
||||
Icon(RemixIcons.menu_2_line),
|
||||
Text(
|
||||
"Detected Ingredients",
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
Wrap(
|
||||
spacing: 10,
|
||||
runSpacing: 10,
|
||||
children: detail.ingredientsList!.map((item) {
|
||||
return Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerLow,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(item, style: Theme.of(context).textTheme.labelMedium),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
53
lib/page/record/detail/widget/health_recommend.dart
Normal file
53
lib/page/record/detail/widget/health_recommend.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:markdown_widget/widget/markdown.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
import '../../../../api/dto/food_scan_dto.dart';
|
||||
|
||||
class HealthRecommend extends StatelessWidget {
|
||||
final FoodScanDto detail;
|
||||
|
||||
const HealthRecommend({super.key, required this.detail});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 20),
|
||||
padding: EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Theme.of(context).colorScheme.shadow,
|
||||
blurRadius: 10,
|
||||
offset: Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
spacing: 10,
|
||||
children: [
|
||||
Icon(
|
||||
RemixIcons.heart_line,
|
||||
color: Theme.of(context).colorScheme.danger,
|
||||
),
|
||||
Text(
|
||||
"Health Recommendations",
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
MarkdownWidget(
|
||||
shrinkWrap: true,
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
data: detail.suggestions ?? "",
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
96
lib/page/record/detail/widget/result_chip.dart
Normal file
96
lib/page/record/detail/widget/result_chip.dart
Normal file
@@ -0,0 +1,96 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/food_scan_dto.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
class ResultChip extends StatelessWidget {
|
||||
final FoodScanDto detail;
|
||||
|
||||
const ResultChip({super.key, required this.detail});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Color currentColor = Colors.transparent; //设置主体颜色
|
||||
IconData iconData = Icons.check; //图标
|
||||
String title = "";
|
||||
String desc = "";
|
||||
|
||||
if (detail.foodType == 1) {
|
||||
currentColor = Theme.of(context).colorScheme.success;
|
||||
iconData = RemixIcons.shield_check_line;
|
||||
title = "Safe to Eat";
|
||||
desc = "This food appears safe for your health profile";
|
||||
} else if (detail.foodType == 2) {
|
||||
currentColor = Theme.of(context).colorScheme.warning;
|
||||
iconData = RemixIcons.error_warning_fill;
|
||||
title = "Proceed with Caution";
|
||||
desc = "This food may have some concerns for your health profile";
|
||||
} else if (detail.foodType == 3) {
|
||||
currentColor = Theme.of(context).colorScheme.danger;
|
||||
iconData = RemixIcons.close_circle_line;
|
||||
title = "Avoid This Food";
|
||||
desc = "This food is not recommended for your health profile";
|
||||
}
|
||||
return Container(
|
||||
padding: EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
color: currentColor.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: currentColor, width: 1),
|
||||
),
|
||||
child: Column(
|
||||
spacing: 20,
|
||||
children: [
|
||||
Container(
|
||||
width: 70,
|
||||
height: 70,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: currentColor,
|
||||
),
|
||||
child: Icon(
|
||||
iconData,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
Text(
|
||||
desc,
|
||||
style: TextStyle(color: currentColor, fontSize: 14),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
SizedBox(
|
||||
width: 150,
|
||||
height: 150,
|
||||
child: Image.network(
|
||||
detail.imageUrl!,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(color: Theme.of(context).colorScheme.surfaceContainer),
|
||||
child: Icon(RemixIcons.error_warning_fill),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
Text(
|
||||
detail.foodName ?? "",
|
||||
style: Theme.of(context).textTheme.titleSmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 20),
|
||||
child: Text(
|
||||
detail.foodDesc ?? "",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
78
lib/page/record/list/record_list_page.dart
Normal file
78
lib/page/record/list/record_list_page.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/food_scan_dto.dart';
|
||||
import 'package:food_health/api/endpoints/food_api.dart';
|
||||
|
||||
import 'widget/record_list_card.dart';
|
||||
|
||||
class RecordListPage extends StatefulWidget {
|
||||
const RecordListPage({super.key});
|
||||
|
||||
@override
|
||||
State<RecordListPage> createState() => _RecordListPageState();
|
||||
}
|
||||
|
||||
class _RecordListPageState extends State<RecordListPage> with TickerProviderStateMixin {
|
||||
bool _loading = true;
|
||||
|
||||
//tab
|
||||
late TabController _tabController;
|
||||
final tabs = ["All", "Safe", "Warning", "Danger"];
|
||||
|
||||
//列表数据
|
||||
List<FoodScanDto> _record = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: tabs.length, vsync: this);
|
||||
_loadData();
|
||||
}
|
||||
|
||||
Future<void> _loadData() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
});
|
||||
var res = await foodScanListApi();
|
||||
setState(() {
|
||||
_record = res;
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Food Check History"),
|
||||
bottom: TabBar(
|
||||
controller: _tabController,
|
||||
dividerColor: Colors.transparent,
|
||||
tabs: tabs.map((item) {
|
||||
return Tab(text: item);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
body: TabBarView(
|
||||
controller: _tabController,
|
||||
children: tabs.asMap().entries.map((entry) {
|
||||
var index = entry.key;
|
||||
var filterList = _record.where((item) {
|
||||
if (index == 0) return true;
|
||||
return item.foodType == index;
|
||||
}).toList();
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () => _loadData(),
|
||||
child: RecordListCard(
|
||||
loading: _loading,
|
||||
records: filterList,
|
||||
onRefresh: () {
|
||||
_loadData();
|
||||
},
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
157
lib/page/record/list/widget/record_list_card.dart
Normal file
157
lib/page/record/list/widget/record_list_card.dart
Normal file
@@ -0,0 +1,157 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_health/api/dto/food_scan_dto.dart';
|
||||
import 'package:food_health/config/theme/custom_colors.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
import 'package:food_health/widgets/common/async_image.dart';
|
||||
import 'package:food_health/widgets/ui_kit/empty/index.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
|
||||
class RecordListCard extends StatelessWidget {
|
||||
final bool loading;
|
||||
final List<FoodScanDto> records;
|
||||
final Function() onRefresh;
|
||||
|
||||
const RecordListCard({
|
||||
super.key,
|
||||
required this.records,
|
||||
required this.loading,
|
||||
required this.onRefresh,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (loading) {
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
return Visibility(
|
||||
visible: records.isNotEmpty,
|
||||
replacement: Empty(
|
||||
child: ElevatedButton(
|
||||
onPressed: onRefresh,
|
||||
child: Text("Refresh"),
|
||||
),
|
||||
),
|
||||
child: ListView.separated(
|
||||
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
|
||||
itemBuilder: (context, index) {
|
||||
var item = records[index];
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
context.push(RoutePaths.detail, extra: item);
|
||||
},
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Theme.of(context).colorScheme.shadow,
|
||||
blurRadius: 4,
|
||||
offset: Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
spacing: 15,
|
||||
children: [
|
||||
Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 80,
|
||||
height: 80,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: AsyncImage(
|
||||
url: item.imageUrl!,
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
right: -5,
|
||||
top: -5,
|
||||
child: StatusWidget(
|
||||
type: item.foodType!,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
spacing: 10,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
item.foodName ?? "",
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.titleSmall,
|
||||
),
|
||||
Text(
|
||||
item.foodDesc ?? "",
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (context, index) {
|
||||
return SizedBox(height: 15);
|
||||
},
|
||||
itemCount: records.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class StatusWidget extends StatelessWidget {
|
||||
final int type;
|
||||
|
||||
const StatusWidget({super.key, required this.type});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
IconData? iconData;
|
||||
Color? color;
|
||||
switch (type) {
|
||||
case 1:
|
||||
iconData = RemixIcons.shield_check_line;
|
||||
color = Theme.of(context).colorScheme.success;
|
||||
break;
|
||||
case 2:
|
||||
iconData = RemixIcons.error_warning_fill;
|
||||
color = Theme.of(context).colorScheme.warning;
|
||||
break;
|
||||
case 3:
|
||||
iconData = RemixIcons.close_circle_line;
|
||||
color = Theme.of(context).colorScheme.danger;
|
||||
break;
|
||||
}
|
||||
if (iconData == null) {
|
||||
return SizedBox();
|
||||
} else {
|
||||
return Container(
|
||||
padding: EdgeInsets.all(5),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: color,
|
||||
),
|
||||
child: Icon(
|
||||
iconData,
|
||||
color: Colors.white,
|
||||
size: 15,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
lib/page/system/agree/agree_page.dart
Normal file
23
lib/page/system/agree/agree_page.dart
Normal 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)),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
115
lib/page/system/login/login_code_page.dart
Normal file
115
lib/page/system/login/login_code_page.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'package:food_health/api/endpoints/user_api.dart';
|
||||
import 'package:food_health/api/network/safe.dart';
|
||||
import 'package:food_health/page/system/login/widget/widget.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
import 'package:food_health/widgets/ui_kit/button/custom_button.dart';
|
||||
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 '../../../providers/app_store.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,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
293
lib/page/system/login/login_page.dart
Normal file
293
lib/page/system/login/login_page.dart
Normal file
@@ -0,0 +1,293 @@
|
||||
import 'package:food_health/api/endpoints/user_api.dart';
|
||||
import 'package:food_health/data/models/other_login_type.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
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:provider/provider.dart';
|
||||
import 'package:remixicon/remixicon.dart';
|
||||
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
|
||||
import '../../../providers/app_store.dart';
|
||||
import '../../../utils/common.dart';
|
||||
import '../../../widgets/common/app_backend.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-u703jdn6imu33rthp94dg7cl5vmrc7rf.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: AppBackend(
|
||||
child: Scaffold(
|
||||
resizeToAvoidBottomInset: false,
|
||||
body: SafeArea(
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.only(
|
||||
top: 0.07.sh,
|
||||
left: 20,
|
||||
right: 20,
|
||||
),
|
||||
child: Column(
|
||||
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();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
bottom: 20,
|
||||
child: AgreementBox(
|
||||
checked: _agree,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_agree = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
66
lib/page/system/login/widget/agreement_box.dart
Normal file
66
lib/page/system/login/widget/agreement_box.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.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(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 25,
|
||||
child: Transform.scale(
|
||||
scale: 0.8,
|
||||
child: Checkbox(
|
||||
value: checked,
|
||||
shape: CircleBorder(),
|
||||
onChanged: (value) {
|
||||
onChanged(value!);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
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/derma/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/derma/privacy_policy.html"},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
166
lib/page/system/login/widget/widget.dart
Normal file
166
lib/page/system/login/widget/widget.dart
Normal 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(
|
||||
"Enter your email to sign up for this app",
|
||||
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,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
65
lib/page/system/splash/splash_page.dart
Normal file
65
lib/page/system/splash/splash_page.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:food_health/providers/app_store.dart';
|
||||
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 '../../../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,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
46
lib/providers/app_store.dart
Normal file
46
lib/providers/app_store.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:food_health/api/dto/login_dto.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../data/local/storage.dart';
|
||||
|
||||
class AppStore with ChangeNotifier {
|
||||
///用户信息
|
||||
UserInfo? userInfo;
|
||||
|
||||
///token
|
||||
String token = '';
|
||||
|
||||
//初始化
|
||||
Future<void> init() async {
|
||||
token = await getToken();
|
||||
var userInfoStorage = await Storage.get('userInfo');
|
||||
if (userInfoStorage != null) {
|
||||
userInfo = UserInfo.fromJson(await Storage.get('userInfo'));
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
///设置用户数据
|
||||
Future<void> setInfo(LoginDto data) async {
|
||||
token = data.accessToken!;
|
||||
userInfo = data.userInfo;
|
||||
|
||||
await Storage.set('userInfo', userInfo?.toJson());
|
||||
await Storage.set('token', token);
|
||||
}
|
||||
|
||||
///获取token
|
||||
static Future<String> getToken() async {
|
||||
return await Storage.get("token") ?? '';
|
||||
}
|
||||
|
||||
///退出登录
|
||||
Future<void> logout() async {
|
||||
await Storage.remove('token');
|
||||
await Storage.remove('userInfo');
|
||||
token = '';
|
||||
userInfo = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
24
lib/router/config/route_paths.dart
Normal file
24
lib/router/config/route_paths.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
class RoutePaths {
|
||||
RoutePaths._();
|
||||
|
||||
///闪烁页
|
||||
static const splash = "/";
|
||||
|
||||
///协议页
|
||||
static const agreement = "/agreement";
|
||||
|
||||
///登录
|
||||
static const login = "/login";
|
||||
|
||||
///登陆验证码
|
||||
static const loginCode = "/loginCode";
|
||||
|
||||
///首页
|
||||
static const layout = "/layout";
|
||||
|
||||
///预约详情
|
||||
static const detail = "/detail";
|
||||
|
||||
///编辑我的
|
||||
static const myEdit = "/my_edit";
|
||||
}
|
||||
16
lib/router/config/route_type.dart
Normal file
16
lib/router/config/route_type.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class RouteType {
|
||||
String path;
|
||||
FutureOr<String?> Function(BuildContext, GoRouterState)? redirect;
|
||||
Widget Function(GoRouterState) child;
|
||||
|
||||
RouteType({
|
||||
required this.path,
|
||||
required this.child,
|
||||
this.redirect,
|
||||
});
|
||||
}
|
||||
49
lib/router/modules/base.dart
Normal file
49
lib/router/modules/base.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'package:food_health/layout/layout_page.dart';
|
||||
import 'package:food_health/page/system/login/login_code_page.dart';
|
||||
import 'package:food_health/page/system/splash/splash_page.dart';
|
||||
import 'package:food_health/router/config/route_paths.dart';
|
||||
|
||||
import '../../page/system/agree/agree_page.dart';
|
||||
import '../../page/system/login/login_page.dart';
|
||||
import '../config/route_type.dart';
|
||||
|
||||
List<RouteType> baseRoutes = [
|
||||
RouteType(
|
||||
path: RoutePaths.splash,
|
||||
child: (state) {
|
||||
return SplashPage();
|
||||
},
|
||||
),
|
||||
RouteType(
|
||||
path: RoutePaths.agreement,
|
||||
child: (state) {
|
||||
final extra = state.extra as Map<String, String>;
|
||||
return AgreePage(
|
||||
title: extra['title'] ?? "",
|
||||
url: extra['url'] ?? "",
|
||||
);
|
||||
},
|
||||
),
|
||||
RouteType(
|
||||
path: RoutePaths.login,
|
||||
child: (state) {
|
||||
return LoginPage();
|
||||
},
|
||||
),
|
||||
RouteType(
|
||||
path: RoutePaths.loginCode,
|
||||
child: (state) {
|
||||
final args = state.extra as Map;
|
||||
return LoginCodePage(
|
||||
email: args['email'],
|
||||
password: args['password'],
|
||||
);
|
||||
},
|
||||
),
|
||||
RouteType(
|
||||
path: RoutePaths.layout,
|
||||
child: (state) {
|
||||
return LayoutPage();
|
||||
},
|
||||
),
|
||||
];
|
||||
27
lib/router/modules/serve.dart
Normal file
27
lib/router/modules/serve.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:food_health/page/profile/edit/my_edit_page.dart';
|
||||
|
||||
import '../../page/record/detail/record_detail_page.dart';
|
||||
import '../config/route_paths.dart';
|
||||
import '../config/route_type.dart';
|
||||
|
||||
List<RouteType> serverRoutes = [
|
||||
RouteType(
|
||||
path: RoutePaths.myEdit,
|
||||
child: (state) {
|
||||
var extra = state.extra as dynamic;
|
||||
return MyEditPage(
|
||||
userProfile: extra,
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
RouteType(
|
||||
path: RoutePaths.detail,
|
||||
child: (state) {
|
||||
var extra = state.extra as dynamic;
|
||||
return RecordDetailPage(
|
||||
detail: extra,
|
||||
);
|
||||
},
|
||||
),
|
||||
];
|
||||
28
lib/router/routes.dart
Normal file
28
lib/router/routes.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'modules/base.dart';
|
||||
import 'config/route_paths.dart';
|
||||
import 'config/route_type.dart';
|
||||
import 'modules/serve.dart';
|
||||
|
||||
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
List<RouteType> routeConfigs = [...baseRoutes, ...serverRoutes];
|
||||
|
||||
//for循环遍历
|
||||
List<RouteBase> routes = routeConfigs.map((item) {
|
||||
return GoRoute(
|
||||
path: item.path,
|
||||
builder: (context, state) {
|
||||
return item.child(state);
|
||||
},
|
||||
);
|
||||
}).toList();
|
||||
|
||||
//变量命名
|
||||
GoRouter goRouter = GoRouter(
|
||||
initialLocation: RoutePaths.splash,
|
||||
routes: routes,
|
||||
navigatorKey: navigatorKey,
|
||||
);
|
||||
14
lib/utils/common.dart
Normal file
14
lib/utils/common.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'dart:io';
|
||||
|
||||
///判断是否是安卓
|
||||
bool isAndroid() {
|
||||
return Platform.isAndroid;
|
||||
}
|
||||
|
||||
/// 获取非空的值
|
||||
String? getNotEmpty(String? value) {
|
||||
if (value != null && value.isNotEmpty) {
|
||||
return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
35
lib/utils/format.dart
Normal file
35
lib/utils/format.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
/// 格式化日期时间
|
||||
String formatDateUS(dynamic date, [String format = 'MM/DD/YYYY hh:mm:ss a']) {
|
||||
DateTime dateTime;
|
||||
|
||||
if (date is String) {
|
||||
dateTime = DateTime.tryParse(date) ?? DateTime.now();
|
||||
} else if (date is DateTime) {
|
||||
dateTime = date;
|
||||
} else {
|
||||
dateTime = DateTime.now();
|
||||
}
|
||||
|
||||
final yyyy = dateTime.year.toString();
|
||||
final MM = dateTime.month.toString().padLeft(2, '0');
|
||||
final dd = dateTime.day.toString().padLeft(2, '0');
|
||||
|
||||
// 12小时制
|
||||
final hour12 = (dateTime.hour % 12 == 0 ? 12 : dateTime.hour % 12).toString().padLeft(2, '0');
|
||||
final HH = dateTime.hour.toString().padLeft(2, '0'); // 24小时制备用
|
||||
final mm = dateTime.minute.toString().padLeft(2, '0');
|
||||
final ss = dateTime.second.toString().padLeft(2, '0');
|
||||
final ampm = dateTime.hour >= 12 ? 'PM' : 'AM';
|
||||
|
||||
String result = format
|
||||
.replaceFirst(RegExp('YYYY'), yyyy)
|
||||
.replaceFirst(RegExp('MM'), MM)
|
||||
.replaceFirst(RegExp('DD'), dd)
|
||||
.replaceFirst(RegExp('hh'), hour12)
|
||||
.replaceFirst(RegExp('HH'), HH)
|
||||
.replaceFirst(RegExp('mm'), mm)
|
||||
.replaceFirst(RegExp('ss'), ss)
|
||||
.replaceFirst(RegExp('a'), ampm);
|
||||
|
||||
return result;
|
||||
}
|
||||
27
lib/widgets/common/app_backend.dart
Normal file
27
lib/widgets/common/app_backend.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AppBackend extends StatelessWidget {
|
||||
final Widget child;
|
||||
|
||||
const AppBackend({super.key, required this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [
|
||||
Color(0xffdff8fb),
|
||||
Color(0xffffffff),
|
||||
Color(0xffdff8fb),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: SafeArea(child: child),
|
||||
);
|
||||
}
|
||||
}
|
||||
43
lib/widgets/common/app_header.dart
Normal file
43
lib/widgets/common/app_header.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AppHeader extends StatefulWidget {
|
||||
const AppHeader({super.key});
|
||||
|
||||
@override
|
||||
State<AppHeader> createState() => _AppHeaderState();
|
||||
}
|
||||
|
||||
class _AppHeaderState extends State<AppHeader> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SafeArea(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
spacing: 10,
|
||||
children: [
|
||||
Image.asset(
|
||||
"assets/image/logo.png",
|
||||
width: 44,
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"FoodSafe",
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
"AI Health Guardian",
|
||||
style: Theme.of(context).textTheme.labelSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
45
lib/widgets/common/async_image.dart
Normal file
45
lib/widgets/common/async_image.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AsyncImage extends StatelessWidget {
|
||||
final String url;
|
||||
final double? width;
|
||||
|
||||
const AsyncImage({
|
||||
super.key,
|
||||
required this.url,
|
||||
this.width,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SizedBox(
|
||||
width: width,
|
||||
child: Image.network(
|
||||
url,
|
||||
fit: BoxFit.cover,
|
||||
// 加载中的样式
|
||||
loadingBuilder: (context, child, loadingProgress) {
|
||||
if (loadingProgress == null) {
|
||||
return child; // 加载完成,直接返回图片
|
||||
}
|
||||
return Container(
|
||||
color: Colors.grey[200],
|
||||
alignment: Alignment.center,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
);
|
||||
},
|
||||
// 加载失败的样式
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return Container(
|
||||
color: Colors.grey[200],
|
||||
child: const Icon(
|
||||
Icons.broken_image,
|
||||
size: 30,
|
||||
color: Colors.grey,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
84
lib/widgets/ui_kit/button/custom_button.dart
Normal file
84
lib/widgets/ui_kit/button/custom_button.dart
Normal file
@@ -0,0 +1,84 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
///自定义按钮
|
||||
///包括loading功能
|
||||
class CustomButton extends StatelessWidget {
|
||||
final Widget child;
|
||||
final VoidCallback? onPressed;
|
||||
final bool loading;
|
||||
final bool round;
|
||||
final bool disabled;
|
||||
|
||||
const CustomButton({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.onPressed,
|
||||
this.loading = false,
|
||||
this.round = true,
|
||||
this.disabled = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
///自定义颜色
|
||||
// switch (size) {
|
||||
// case ButtonSize.small:
|
||||
// height = 28;
|
||||
// loadingSize = 16;
|
||||
// fontSize = 12;
|
||||
// padding = const EdgeInsets.symmetric(horizontal: 12);
|
||||
// break;
|
||||
// case ButtonSize.large:
|
||||
// height = 48;
|
||||
// loadingSize = 24;
|
||||
// fontSize = 18;
|
||||
// padding = const EdgeInsets.symmetric(horizontal: 20);
|
||||
// break;
|
||||
// case ButtonSize.medium:
|
||||
// height = 45;
|
||||
// loadingSize = 15;
|
||||
// fontSize = 16;
|
||||
// padding = const EdgeInsets.symmetric(horizontal: 16);
|
||||
// break;
|
||||
// }
|
||||
|
||||
void handClick() {
|
||||
if (!loading && !disabled) {
|
||||
onPressed?.call();
|
||||
}
|
||||
}
|
||||
|
||||
return Opacity(
|
||||
opacity: disabled ? 0.5 : 1,
|
||||
child: ElevatedButton(
|
||||
onPressed: handClick,
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: round
|
||||
? const StadiumBorder()
|
||||
: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Visibility(
|
||||
visible: loading,
|
||||
child: Container(
|
||||
margin: EdgeInsets.only(right: 8),
|
||||
child: SizedBox.square(
|
||||
dimension: 15,
|
||||
child: CircularProgressIndicator(
|
||||
color: Colors.white,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
child,
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
34
lib/widgets/ui_kit/empty/index.dart
Normal file
34
lib/widgets/ui_kit/empty/index.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../config/app_context.dart';
|
||||
|
||||
class Empty extends StatelessWidget {
|
||||
final String? title;
|
||||
final Widget? child;
|
||||
|
||||
const Empty({super.key, this.title, this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SizedBox.expand(
|
||||
child: Align(
|
||||
alignment: const FractionalOffset(0.5, 0.2),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
FractionallySizedBox(
|
||||
widthFactor: 0.5,
|
||||
child: Image.asset("assets/image/empty_data.png"),
|
||||
),
|
||||
if (title != null)
|
||||
Text(
|
||||
title!,
|
||||
style: AppContext.textTheme.labelMedium,
|
||||
),
|
||||
if (child != null) child!,
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user