From e34234da09472a7581206eb5001ae4cef82e6c37 Mon Sep 17 00:00:00 2001 From: "DESKTOP-I3JPKHK\\wy" <1111> Date: Thu, 11 Sep 2025 17:15:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=8F=90=E4=BA=A4=E5=A4=84?= =?UTF-8?q?=E7=90=86BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flutter_build_apk_onwindows.bat | 109 ++++++++++++++++++ .../controllers/app_state_controller.dart | 33 ++++++ .../utils/permission_describe_util.dart | 25 ++-- 3 files changed, 151 insertions(+), 16 deletions(-) create mode 100644 making_school_asignment_app/flutter_build_apk_onwindows.bat create mode 100644 making_school_asignment_app/lib/common/controllers/app_state_controller.dart diff --git a/making_school_asignment_app/flutter_build_apk_onwindows.bat b/making_school_asignment_app/flutter_build_apk_onwindows.bat new file mode 100644 index 0000000..97db05f --- /dev/null +++ b/making_school_asignment_app/flutter_build_apk_onwindows.bat @@ -0,0 +1,109 @@ +@echo off +SETLOCAL EnableDelayedExpansion + +:: 设置 UTF-8 代码页以支持中文显示 +chcp 65001 > nul + +:: 获取项目根目录 +for %%I in ("%~dp0.") do set "PROJECT_DIR=%%~fI" + +:: 1. 强制终止所有Java进程(Gradle相关) +echo Terminating processes... +taskkill /f /im java.exe 2>nul +taskkill /f /im gradlew.exe 2>nul +timeout /t 2 /nobreak >nul + +:: 修复后的Flutter构建脚本 +echo Running flutter clean... +call flutter clean +if !ERRORLEVEL! neq 0 ( + echo flutter clean failed with error: !ERRORLEVEL! + pause + exit /b 1 +) + +:: 3. 强制删除构建目录(带重试机制) +echo Removing residual files +call :ForceDelete "build" +call :ForceDelete ".dart_tool" +call :ForceDelete "pubspec.lock" + +echo Running flutter pub get... +call flutter pub get +if !ERRORLEVEL! neq 0 ( + echo flutter pub get failed with error: !ERRORLEVEL! + pause + exit /b 1 +) + +echo Running build_runner... +call flutter packages pub run build_runner build --delete-conflicting-outputs +if !ERRORLEVEL! neq 0 ( + echo build_runner failed with error: !ERRORLEVEL! + pause + exit /b 1 +) + +echo Building release APK... +call flutter build apk --release +if !ERRORLEVEL! neq 0 ( + echo APK build failed with error: !ERRORLEVEL! + pause + exit /b 1 +) + +:: 新增:打开APK所在文件夹 +echo Opening APK directory +if exist "build\app\outputs\flutter-apk\" ( + explorer "build\app\outputs\flutter-apk\" +) else ( + echo APK directory does not exist +) + +echo. +echo All steps completed successfully! +echo APK file generated at: %PROJECT_DIR%\build\app\outputs\flutter-apk\ +echo. +timeout /t 3 /nobreak >nul +exit /b 0 + +:: 函数区 ------------------------------------------------- +:ForceDelete +setlocal +set "target=%~1" +set "max_retries=3" +set "retry_delay=1" + +:: 检查目标是否存在 +if not exist "%target%" ( + endlocal + goto :eof +) + +:: 尝试多次删除 +for /l %%i in (1,1,%max_retries%) do ( + echo Attempting to delete %target% (try %%i of %max_retries%) + + :: 先尝试删除文件 + del /f /q /s "%target%\*" 2>nul + :: 再尝试删除目录 + rmdir /s /q "%target%" 2>nul + + :: 检查是否删除成功 + if not exist "%target%" ( + echo Successfully deleted %target% + endlocal + goto :eof + ) + + :: 如果未成功,等待后重试 + if %%i lss %max_retries% ( + timeout /t %retry_delay% /nobreak >nul + set /a retry_delay*=2 :: 指数退避策略 + ) +) + +:: 如果所有尝试都失败 +echo Warning: Could not completely delete %target%, some files may be in use +endlocal +goto :eof \ No newline at end of file diff --git a/making_school_asignment_app/lib/common/controllers/app_state_controller.dart b/making_school_asignment_app/lib/common/controllers/app_state_controller.dart new file mode 100644 index 0000000..e974de8 --- /dev/null +++ b/making_school_asignment_app/lib/common/controllers/app_state_controller.dart @@ -0,0 +1,33 @@ +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; + +/// 全局监听应用是否进入后台 或者 前台 +class AppStateController extends GetxController with WidgetsBindingObserver { + // 响应式变量,存储当前生命周期状态 + final appLifecycleState = AppLifecycleState.resumed.obs; + + // 便捷方法:判断应用是否在前台 + bool get isAppInForeground => appLifecycleState.value == AppLifecycleState.resumed; + + @override + void onInit() { + super.onInit(); + WidgetsBinding.instance.addObserver(this); // 注册生命周期监听 + } + + @override + void onClose() { + WidgetsBinding.instance.removeObserver(this); // 移除监听 + super.onClose(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + appLifecycleState.value = state; // 更新状态 + // if (isAppInForeground) { + // WidgetsBinding.instance.addPostFrameCallback((_) { + // Get.find().initiateVersionCheck(Get.context!); + // }); + // } + } +} diff --git a/making_school_asignment_app/lib/common/utils/permission_describe_util.dart b/making_school_asignment_app/lib/common/utils/permission_describe_util.dart index 4c90103..3fd2028 100644 --- a/making_school_asignment_app/lib/common/utils/permission_describe_util.dart +++ b/making_school_asignment_app/lib/common/utils/permission_describe_util.dart @@ -12,18 +12,13 @@ class PermissionDescribePage extends StatefulWidget { final String describe; final List permissions; - const PermissionDescribePage( - {required this.title, - required this.describe, - required this.permissions, - super.key}); + const PermissionDescribePage({required this.title, required this.describe, required this.permissions, super.key}); @override State createState() => _PermissionDescribePageState(); } -class _PermissionDescribePageState extends State - with WidgetsBindingObserver { +class _PermissionDescribePageState extends State with WidgetsBindingObserver { Timer? _timerPermission; bool theOpenAppSettings = false; int theExecutionFrequency = 0; @@ -40,7 +35,7 @@ class _PermissionDescribePageState extends State @override void didChangeAppLifecycleState(AppLifecycleState state) { - print("-didChangeAppLifecycleState-" + state.toString()); + print("-didChangeAppLifecycleState-$state"); switch (state) { case AppLifecycleState.inactive: // 处于这种状态的应用程序应该假设它们可能在任何时候暂停。 break; @@ -97,7 +92,7 @@ class _PermissionDescribePageState extends State ); } permissions.add(p); - } else if (await status.isPermanentlyDenied) { + } else if (status.isPermanentlyDenied) { permanentRefusal.add(p); } } @@ -137,8 +132,7 @@ class _PermissionDescribePageState extends State /// 权限发起请求 /// @param List permissions 权限集合 - Future> getStoragePermission( - BuildContext context, List permissions, + Future> getStoragePermission(BuildContext context, List permissions, [int executionFrequency = 0]) async { Map statusRes = await permissions.request(); @@ -180,8 +174,7 @@ class _PermissionDescribePageState extends State executionFrequency++; } if (executionFrequency <= 1) { - var res = - await getStoragePermission(context, [key], executionFrequency); + var res = await getStoragePermission(context, [key], executionFrequency); permanentRefusal.addAll(res); } else { permanentRefusal.add(key); @@ -215,7 +208,7 @@ class _PermissionDescribePageState extends State color: Colors.black.withOpacity(0.5), // 阴影颜色 spreadRadius: 2, // 水平和垂直方向上扩展范围 blurRadius: 4, // 模糊半径 - offset: Offset(0, 3), // X 和 Y 的偏移量 + offset: const Offset(0, 3), // X 和 Y 的偏移量 ), ], ), @@ -239,7 +232,7 @@ class PermissionDescribeUtil { PermissionDescribeUtil._internal(); - static get instance => _instance ??= PermissionDescribeUtil._internal(); + static PermissionDescribeUtil get instance => _instance ??= PermissionDescribeUtil._internal(); /// 发起权限请求 Future toLaunchPermissionRequest( @@ -257,7 +250,7 @@ class PermissionDescribeUtil { } } if (isGranted) { - Navigator.of(Get.context ?? context).push(PageRouteBuilder( + await Navigator.of(Get.context ?? context).push(PageRouteBuilder( opaque: false, pageBuilder: (context, animation, secondaryAnimation) { return PermissionDescribePage(