feat: 项目框架,全局空间处理

This commit is contained in:
chengsiwen committed 2026-06-26 15:58:29 +08:00
1 parent 4df6be7411
commit d90d023e85
36 files changed
+1166 -118

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
/// keep alive
class AppKeepAlive extends StatefulWidget {
final Widget child;
final bool? wantKeepAlive;
const AppKeepAlive(
{super.key, required this.child, this.wantKeepAlive = true});
@override
State<AppKeepAlive> createState() => _CmKeepAlivePageState();
}
class _CmKeepAlivePageState extends State<AppKeepAlive>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return widget.child;
}
@override
bool get wantKeepAlive => widget.wantKeepAlive!;
}
+205
View File
@@ -0,0 +1,205 @@
import 'package:flutter/material.dart';
// 扩展Widget类以添加额外的布局和样式方法
extension WidgetExtend on Widget {
/// 在当前Widget外层添加一个Container
///
/// 可以通过此方法设置容器的颜色、大小、边距、内边距、对齐方式等
/// 如果设置了gradientColors,则会应用渐变色
/// 边框和圆角也可以通过此方法自定义
/// 添加点击事件处理函数onTap会创建一个InkWell
Widget addContainer({
Color? color,
double? height,
double? width,
EdgeInsets? margin,
EdgeInsets? padding,
double? radius,
BorderRadius? borderRadius,
Border? border,
GestureTapCallback? onTap,
AlignmentGeometry? alignment,
Gradient? gradient,
List<BoxShadow>? boxShadow,
BoxConstraints? constraints,
Matrix4? transform,
Clip clipBehavior = Clip.none,
DecorationImage? image,
BlendMode? backgroundBlendMode,
BoxShape shape = BoxShape.rectangle,
double? minWidth,
double? maxWidth,
double? minHeight,
double? maxHeight,
/// 打印 widget size 只在 debug 有效
bool isPrintSize = false,
}) {
return Container(
width: width,
height: height,
margin: margin,
padding: padding,
alignment: alignment,
clipBehavior: clipBehavior,
constraints: constraints ??
BoxConstraints(
minWidth: minWidth ?? 0.0,
maxWidth: maxWidth ?? double.infinity,
minHeight: minHeight ?? 0.0,
maxHeight: maxHeight ?? double.infinity,
),
transform: transform,
decoration: BoxDecoration(
color: color,
boxShadow: boxShadow,
backgroundBlendMode: backgroundBlendMode,
shape: shape,
image: image,
gradient: gradient,
border: border,
borderRadius:
radius != null ? BorderRadius.circular(radius) : borderRadius,
),
child: this,
).addInkWell(onTap: onTap);
}
/// 在当前Widget外层添加一个Transform.translate
///
/// 通过此方法可以设置控件的偏移量,实现位置移动的效果
Widget addOffset({Offset offset = const Offset(0, 0)}) {
return Transform.translate(offset: offset, child: this);
}
/// 在当前Widget外层添加一个Padding
///
/// 通过此方法可以设置控件的内边距
Widget addPadding({EdgeInsets? padding}) {
return Padding(padding: padding ?? EdgeInsets.zero, child: this);
}
/// 在当前Widget外层添加一个Align
///
/// 通过此方法可以设置控件的对齐方式
Widget addAlign({AlignmentGeometry alignment = Alignment.centerLeft}) {
return Align(
alignment: alignment,
child: this,
);
}
Widget addCenter() {
return Center(child: this);
}
/// 在当前Widget外层添加一个GestureDetector
///
/// 通过此方法可以为控件添加点击事件处理函数
Widget addGestureDetector({
GestureTapCallback? onTap,
GestureTapCallback? onDoubleTap,
GestureLongPressCallback? onLongPress,
}) {
return GestureDetector(
onTap: onTap,
onDoubleTap: onDoubleTap,
onLongPress: onLongPress,
child: this,
);
// return GestureDetectorDebounced(
// onTap: onTap,
// child: this,
// );
}
/// 在当前Widget外层添加一个Visibility
///
/// 通过此方法可以控制控件的显示和隐藏
Widget addVisibility({bool visible = true}) {
return Visibility(
visible: visible,
child: this,
);
}
/// 在当前Widget外层添加一个Transform.rotate
///
/// 通过此方法可以设置控件的旋转角度
Widget addRotate({double angle = 0}) {
return Transform.rotate(
angle: angle,
child: this,
);
}
/// 在当前Widget外层添加一个InkWell
///
/// 通过此方法可以为控件添加点击事件处理函数
Widget addInkWell({GestureTapCallback? onTap}) {
if (onTap == null) {
return this;
}
return InkWell(
onTap: onTap,
child: this,
);
}
/// 在当前Widget外层添加一个SizedBox
///
/// 通过此方法可以设置控件的宽高
Widget addSize({double? width, double? height}) {
return SizedBox(
width: width,
height: height,
child: this,
);
}
/// 在当前Widget外层添加一个Opacity
///
/// 通过此方法可以设置控件的透明度
Widget addOpacity({double opacity = 1}) {
return Opacity(
opacity: opacity,
child: this,
);
}
}
extension OnRenderObjectWidget on RenderObjectWidget {
SliverPadding addPaddingSliver({EdgeInsets? padding}) {
return SliverPadding(
padding: padding ?? EdgeInsets.zero,
sliver: this,
);
}
DecoratedSliver addDecoratedSliver({
Color? color,
BorderRadius? borderRadius,
Border? border,
BoxShape? shape,
Gradient? gradient,
List<BoxShadow>? boxShadow,
EdgeInsets? padding,
}) {
return DecoratedSliver(
decoration: BoxDecoration(
color: color ?? Colors.white,
border: border,
borderRadius: borderRadius,
boxShadow: boxShadow,
gradient: gradient,
shape: shape ?? BoxShape.rectangle,
),
sliver: this.addPaddingSliver(padding: padding),
);
}
}
+44
View File
@@ -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;
}
+59
View File
@@ -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");
}
}
+137
View File
@@ -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,
),
);
},
);
}
}