feat: 项目框架,全局空间处理
This commit is contained in:
1 parent
4df6be7411
commit
d90d023e85
36 files changed
+1166
-118
No files matched your search
@@ -3,7 +3,7 @@ import 'dart:ui';
|
||||
class CommonColor {
|
||||
|
||||
// 主题色 #653CFA
|
||||
static const Color primacy = Color(0xFF653CFA);
|
||||
static const Color primacyColor = Color(0xFF653CFA);
|
||||
static const Color black = Color(0xFF030510);
|
||||
// grey
|
||||
static const Color grey = Color(0xFF8e8e93);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../../utils/tools/app_sp.dart';
|
||||
import '../const/common_string.dart';
|
||||
|
||||
class AppLanguage extends Translations {
|
||||
|
||||
static List<Locale> supportedLocales = [
|
||||
Locale('en', 'US'),
|
||||
Locale('zh', 'CN'),
|
||||
Get.deviceLocale!
|
||||
];
|
||||
|
||||
///切换语言
|
||||
static void changeLanguage(int languageIndex) {
|
||||
Locale? appLocale;
|
||||
switch (languageIndex) {
|
||||
case 0: //English
|
||||
appLocale = const Locale('en', 'US');
|
||||
break;
|
||||
case 1: //简体中文
|
||||
appLocale = const Locale('zh', 'CN');
|
||||
break;
|
||||
case 2: //跟隨系統
|
||||
appLocale = Get.deviceLocale;
|
||||
break;
|
||||
default:
|
||||
appLocale = Get.deviceLocale;
|
||||
break;
|
||||
}
|
||||
//保存到本地
|
||||
// AppBox.shared.language = languageIndex;
|
||||
// AppSp().setString(CommonString.language, appLocale!.languageCode);
|
||||
// AppSp().setInt(CommonString.languageIndex, languageIndex);
|
||||
Get.updateLocale(appLocale!);
|
||||
}
|
||||
|
||||
static Future<Locale?> getLocale() async {
|
||||
int lint = await AppSp().getInt(CommonString.languageIndex);
|
||||
return supportedLocales[lint];
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Map<String, String>> get keys => {
|
||||
'en_US': {
|
||||
|
||||
},
|
||||
'zh_CN': {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class AppLocale {
|
||||
static String get languageCode =>
|
||||
(Get.locale?.languageCode ?? 'en').toLowerCase();
|
||||
|
||||
static bool get isZh => languageCode.startsWith('zh');
|
||||
|
||||
static String pick({
|
||||
required String zh,
|
||||
required String en,
|
||||
}) {
|
||||
final String first = isZh ? zh : en;
|
||||
final String second = isZh ? en : zh;
|
||||
if (first.trim().isNotEmpty) return first;
|
||||
if (second.trim().isNotEmpty) return second;
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
class LocalString {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class AppRequest {
|
||||
|
||||
static const Duration defaultTimeout = Duration(seconds: 15);
|
||||
static final AppRequest instance = AppRequest();
|
||||
final Dio dio;
|
||||
|
||||
AppRequest({
|
||||
Dio? dio,
|
||||
String baseUrl = '',
|
||||
Duration connectTimeout = defaultTimeout,
|
||||
Duration sendTimeout = defaultTimeout,
|
||||
Duration receiveTimeout = defaultTimeout,
|
||||
}) : dio =
|
||||
dio ??
|
||||
Dio(
|
||||
BaseOptions(
|
||||
baseUrl: baseUrl,
|
||||
connectTimeout: connectTimeout,
|
||||
sendTimeout: sendTimeout,
|
||||
receiveTimeout: receiveTimeout,
|
||||
responseType: ResponseType.json,
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
|
||||
void setBaseUrl(String baseUrl) {
|
||||
dio.options.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
void setHeader(String key, Object? value) {
|
||||
dio.options.headers[key] = value;
|
||||
}
|
||||
|
||||
void removeHeader(String key) {
|
||||
dio.options.headers.remove(key);
|
||||
}
|
||||
|
||||
void setToken(String token, {String prefix = 'Bearer'}) {
|
||||
final value = prefix.isEmpty ? token : '$prefix $token';
|
||||
setHeader('Authorization', value);
|
||||
}
|
||||
|
||||
void clearToken() {
|
||||
removeHeader('Authorization');
|
||||
}
|
||||
|
||||
Future<T?> get<T>(
|
||||
String path, {
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) {
|
||||
return request<T>(
|
||||
path,
|
||||
method: 'GET',
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
}
|
||||
|
||||
Future<T?> post<T>(
|
||||
String path, {
|
||||
Object? data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) {
|
||||
return request<T>(
|
||||
path,
|
||||
method: 'POST',
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
}
|
||||
|
||||
Future<T?> put<T>(
|
||||
String path, {
|
||||
Object? data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) {
|
||||
return request<T>(
|
||||
path,
|
||||
method: 'PUT',
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
}
|
||||
|
||||
Future<T?> delete<T>(
|
||||
String path, {
|
||||
Object? data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
}) {
|
||||
return request<T>(
|
||||
path,
|
||||
method: 'DELETE',
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
);
|
||||
}
|
||||
|
||||
Future<T?> request<T>(
|
||||
String path, {
|
||||
required String method,
|
||||
Object? data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) async {
|
||||
final response = await dio.request<T>(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: _withMethod(options, method),
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
Options _withMethod(Options? options, String method) {
|
||||
return options == null
|
||||
? Options(method: method)
|
||||
: options.copyWith(method: method);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user