feat: 项目框架,全局空间处理
This commit is contained in:
1 parent
4df6be7411
commit
d90d023e85
36 files changed
+1166
-118
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
/// AppScreen class
|
||||
class AppScreen {
|
||||
static double get screenWidth => Get.width;
|
||||
static double get screenHeight => Get.height;
|
||||
// static double get screenWidth => MediaQuery.of(Get.context!).size.width;
|
||||
// static double get screenHeight => MediaQuery.of(Get.context!).size.height;
|
||||
|
||||
static Size size(BuildContext context) {
|
||||
return MediaQuery.of(context).size;
|
||||
}
|
||||
|
||||
static bool get isDebug=> kDebugMode;
|
||||
|
||||
static double get statusBarHeight => Get.statusBarHeight / Get.pixelRatio;
|
||||
|
||||
static double get bottomBarHeight => Get.bottomBarHeight / Get.pixelRatio;
|
||||
|
||||
// static double get tabBarHeight => kToolbarHeight;
|
||||
|
||||
static double get navBarHeight => kToolbarHeight + statusBarHeight;
|
||||
|
||||
static bool get isDark => Get.isDarkMode;
|
||||
|
||||
static bool get isAndroid {
|
||||
return GetPlatform.isAndroid;
|
||||
}
|
||||
|
||||
static bool get isIos {
|
||||
return GetPlatform.isIOS;
|
||||
}
|
||||
|
||||
static double aRatioWidth({double? h, double aspectRatio = 1}) {
|
||||
h ??= screenHeight;
|
||||
return screenHeight * 1 / aspectRatio;
|
||||
}
|
||||
|
||||
|
||||
static double get keyboardHeight => Get.mediaQuery.viewInsets.bottom;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
|
||||
// 用法: AppSp().setString("key", "value");
|
||||
class AppSp {
|
||||
static SharedPreferences? _instance;
|
||||
static final String _basePrefix = "omiyo.";
|
||||
static Future<SharedPreferences>? _instanceFuture;
|
||||
|
||||
static Future<SharedPreferences> getInstance() {
|
||||
if (_instance != null) {
|
||||
return Future.value(_instance);
|
||||
}
|
||||
|
||||
// 确保并发情况下只初始化一次
|
||||
_instanceFuture ??= SharedPreferences.getInstance().then((prefs) {
|
||||
_instance = prefs;
|
||||
return prefs;
|
||||
});
|
||||
|
||||
return _instanceFuture!;
|
||||
}
|
||||
|
||||
void setString(String key, String value) async {
|
||||
final prefs = await getInstance();
|
||||
prefs.setString("$_basePrefix$key", value);
|
||||
}
|
||||
|
||||
Future<String> getString(String key) async {
|
||||
final prefs = await getInstance();
|
||||
return prefs.getString("$_basePrefix$key") ?? '';
|
||||
}
|
||||
|
||||
void setBool(String key, bool value) async {
|
||||
final prefs = await getInstance();
|
||||
prefs.setBool("$_basePrefix$key", value);
|
||||
}
|
||||
|
||||
Future<bool> getBool(String key) async {
|
||||
final prefs = await getInstance();
|
||||
return prefs.getBool("$_basePrefix$key") ?? false;
|
||||
}
|
||||
|
||||
void setInt(String key, int value) async {
|
||||
final prefs = await getInstance();
|
||||
prefs.setInt("$_basePrefix$key", value);
|
||||
}
|
||||
|
||||
Future<int> getInt(String key) async {
|
||||
final prefs = await getInstance();
|
||||
return prefs.getInt("$_basePrefix$key") ?? 0;
|
||||
}
|
||||
|
||||
void deleteKey(String key) async {
|
||||
final prefs = await getInstance();
|
||||
prefs.remove("$_basePrefix$key");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import 'package:flutter/material.dart';
|
||||
// import 'package:lottie/lottie.dart';
|
||||
// import 'package:flutter_svg/flutter_svg.dart';
|
||||
|
||||
class ConstImg {
|
||||
ConstImg._();
|
||||
|
||||
static const String bottomBar = "assets/common/bottom_bar.png";
|
||||
static const String tabHomeSelected = "assets/common/tab_home_selected.png";
|
||||
static const String tabHomeUnselect = "assets/common/tab_home_unselect.png";
|
||||
static const String tabSearchSelected =
|
||||
"assets/common/tab_search_selected.png";
|
||||
static const String tabSearchUnselect =
|
||||
"assets/common/tab_search_unselect.png";
|
||||
static const String tabLikeSelected = "assets/common/tab_like_selected.png";
|
||||
static const String tabLikeUnselect = "assets/common/tab_like_unselect.png";
|
||||
static const String tabMineSelected = "assets/common/tab_mine_selected.png";
|
||||
static const String tabMineUnselect = "assets/common/tab_mine_unselect.png";
|
||||
static const String homeBg = "assets/imgs/home_bg.png";
|
||||
|
||||
// nodata
|
||||
static const String noData = "assets/images/nodata.png";
|
||||
|
||||
static const String loadingJson = "assets/bagua.json";
|
||||
// static get appLoading => Lottie.asset(loadingJson, width: 60, height: 60);
|
||||
}
|
||||
|
||||
class HelpImg {
|
||||
/// load local
|
||||
static Widget asset(
|
||||
String path, {
|
||||
BoxFit? fit,
|
||||
double? width,
|
||||
double? height,
|
||||
Color? color,
|
||||
bool isDartData = false,
|
||||
String? dartPath,
|
||||
}) {
|
||||
String assets = path;
|
||||
|
||||
return Image.asset(
|
||||
assets,
|
||||
fit: fit,
|
||||
width: width,
|
||||
height: height,
|
||||
color: color,
|
||||
);
|
||||
}
|
||||
|
||||
static Widget networkRadius(
|
||||
String path, {
|
||||
BoxFit? fit,
|
||||
double? width,
|
||||
double? height,
|
||||
BoxShape? shape,
|
||||
BorderRadius? borderRadius,
|
||||
Widget? errorWidget,
|
||||
}) {
|
||||
return ClipRRect(
|
||||
borderRadius: borderRadius ?? BorderRadius.circular(3),
|
||||
child: network(
|
||||
path,
|
||||
fit: fit,
|
||||
width: width,
|
||||
height: height,
|
||||
shape: shape,
|
||||
errorWidget: errorWidget,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// load network
|
||||
static Widget network(
|
||||
String path, {
|
||||
BoxFit? fit,
|
||||
double? width,
|
||||
double? height,
|
||||
Color? color,
|
||||
BoxShape? shape,
|
||||
BorderRadius? borderRadius,
|
||||
Widget? errorWidget,
|
||||
}) {
|
||||
// bool isSvg = path.toLowerCase().endsWith('.svg');
|
||||
// if (isSvg) {
|
||||
// return SvgPicture.network(
|
||||
// path,
|
||||
// width: width,
|
||||
// height: height,
|
||||
// // fit: fit ?? BoxFit.fill,
|
||||
// colorFilter: color != null
|
||||
// ? ColorFilter.mode(color, BlendMode.srcIn)
|
||||
// : null,
|
||||
// placeholderBuilder: (context) => Container(
|
||||
// width: width,
|
||||
// height: height,
|
||||
// color: Colors.grey[200],
|
||||
// child: Center(child: CircularProgressIndicator()),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
return Image.network(
|
||||
path,
|
||||
width: width,
|
||||
height: height,
|
||||
fit: fit,
|
||||
loadingBuilder: (context, child, loadingProgress) {
|
||||
if (loadingProgress == null) return child;
|
||||
return Center(
|
||||
//CircularProgressIndicator
|
||||
child: Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
padding: const EdgeInsets.all(5),
|
||||
child: CircularProgressIndicator(
|
||||
color: Colors.white,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
// ✅ 关键:处理加载失败的情况
|
||||
return errorWidget ??
|
||||
Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
color: Colors.grey[300],
|
||||
child: const Icon(
|
||||
Icons.broken_image,
|
||||
color: Colors.grey,
|
||||
size: 40,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user