From d75cf34419d51312626f0f7d7fc56b5f6cae4350 Mon Sep 17 00:00:00 2001 From: zhaolong <928343994@qq.com> Date: Tue, 8 Dec 2020 20:49:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E8=AE=B2=20=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E6=9D=83=E9=99=90=E7=94=B3=E8=AF=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- android/app/src/main/AndroidManifest.xml | 1 + ios/Flutter/Debug.xcconfig | 1 + ios/Flutter/Release.xcconfig | 1 + ios/Runner.xcodeproj/project.pbxproj | 68 ++++++++++ .../contents.xcworkspacedata | 3 + lib/src/index_page.dart | 120 +++++++++++++++++- pubspec.lock | 22 ++++ pubspec.yaml | 4 + 9 files changed, 220 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4aee0d3..50d021f 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,6 @@ Flutter 脚手架 快速开发模版 ## Getting Started * 2020 -12-05 第一天 [Flutter应用开发从0到1基础架构搭建效果预览](https://www.bilibili.com/video/BV1rZ4y1G7cK/) -* 2020 -12-06 第二天[Flutter项目创建、Android Studio的基本断点调试 应用图标更换、应用名称修改](https://www.bilibili.com/video/BV1pt4y1Y7F8/) -* 2020 -12-07 第三天[Flutter从0到1构建应用启动页面、闪屏页面 600行字幕 带大家轻松玩转-正在发布]() +* 2020 -12-06 第二天 [Flutter项目创建、Android Studio的基本断点调试 应用图标更换、应用名称修改](https://www.bilibili.com/video/BV1pt4y1Y7F8/) +* 2020 -12-07 第三天 [Flutter从0到1构建应用启动页面、闪屏页面 600行字幕 带大家轻松玩转-正在发布](https://www.bilibili.com/video/BV1VA41147WG/) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 21d3470..5a669b1 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ + diff --git a/ios/Flutter/Debug.xcconfig b/ios/Flutter/Debug.xcconfig index 592ceee..e8efba1 100644 --- a/ios/Flutter/Debug.xcconfig +++ b/ios/Flutter/Debug.xcconfig @@ -1 +1,2 @@ +#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" #include "Generated.xcconfig" diff --git a/ios/Flutter/Release.xcconfig b/ios/Flutter/Release.xcconfig index 592ceee..399e934 100644 --- a/ios/Flutter/Release.xcconfig +++ b/ios/Flutter/Release.xcconfig @@ -1 +1,2 @@ +#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" #include "Generated.xcconfig" diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 55faf58..1898450 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; }; + D782D52B85D2EE8FD8B57779 /* libPods-Runner.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2628FE28A1CFF29338E92A1E /* libPods-Runner.a */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -32,7 +33,9 @@ /* Begin PBXFileReference section */ 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; + 2628FE28A1CFF29338E92A1E /* libPods-Runner.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Runner.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; + 454518B0410689CAE89D4EE7 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = ""; }; 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; 7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; @@ -44,6 +47,8 @@ 97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + A3FB15E79C3F6EDC42B44C04 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = ""; }; + B7EB0D19D84D8B9BEF688A5F /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -51,12 +56,32 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + D782D52B85D2EE8FD8B57779 /* libPods-Runner.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 6E29A3D1E4EEA938EB5F7DA2 /* Pods */ = { + isa = PBXGroup; + children = ( + A3FB15E79C3F6EDC42B44C04 /* Pods-Runner.debug.xcconfig */, + 454518B0410689CAE89D4EE7 /* Pods-Runner.release.xcconfig */, + B7EB0D19D84D8B9BEF688A5F /* Pods-Runner.profile.xcconfig */, + ); + name = Pods; + path = Pods; + sourceTree = ""; + }; + 8B5F970AAD82DE761B462407 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 2628FE28A1CFF29338E92A1E /* libPods-Runner.a */, + ); + name = Frameworks; + sourceTree = ""; + }; 9740EEB11CF90186004384FC /* Flutter */ = { isa = PBXGroup; children = ( @@ -74,6 +99,8 @@ 9740EEB11CF90186004384FC /* Flutter */, 97C146F01CF9000F007C117D /* Runner */, 97C146EF1CF9000F007C117D /* Products */, + 6E29A3D1E4EEA938EB5F7DA2 /* Pods */, + 8B5F970AAD82DE761B462407 /* Frameworks */, ); sourceTree = ""; }; @@ -116,12 +143,14 @@ isa = PBXNativeTarget; buildConfigurationList = 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */; buildPhases = ( + 588F13F58ADFE109D5350F29 /* [CP] Check Pods Manifest.lock */, 9740EEB61CF901F6004384FC /* Run Script */, 97C146EA1CF9000F007C117D /* Sources */, 97C146EB1CF9000F007C117D /* Frameworks */, 97C146EC1CF9000F007C117D /* Resources */, 9705A1C41CF9048500538489 /* Embed Frameworks */, 3B06AD1E1E4923F5004D2608 /* Thin Binary */, + 950CCAC0D8C70D8D43960F4B /* [CP] Embed Pods Frameworks */, ); buildRules = ( ); @@ -193,6 +222,45 @@ shellPath = /bin/sh; shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin"; }; + 588F13F58ADFE109D5350F29 /* [CP] Check Pods Manifest.lock */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + "${PODS_PODFILE_DIR_PATH}/Podfile.lock", + "${PODS_ROOT}/Manifest.lock", + ); + name = "[CP] Check Pods Manifest.lock"; + outputFileListPaths = ( + ); + outputPaths = ( + "$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; + showEnvVarsInLog = 0; + }; + 950CCAC0D8C70D8D43960F4B /* [CP] Embed Pods Frameworks */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist", + ); + name = "[CP] Embed Pods Frameworks"; + outputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; + showEnvVarsInLog = 0; + }; 9740EEB61CF901F6004384FC /* Run Script */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; diff --git a/ios/Runner.xcworkspace/contents.xcworkspacedata b/ios/Runner.xcworkspace/contents.xcworkspacedata index 1d526a1..21a3cc1 100644 --- a/ios/Runner.xcworkspace/contents.xcworkspacedata +++ b/ios/Runner.xcworkspace/contents.xcworkspacedata @@ -4,4 +4,7 @@ + + diff --git a/lib/src/index_page.dart b/lib/src/index_page.dart index e6be73c..7e1122c 100644 --- a/lib/src/index_page.dart +++ b/lib/src/index_page.dart @@ -1,5 +1,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:permission_handler/permission_handler.dart'; /// 创建人: Created by zhaolong /// 创建时间:Created by on 2020/12/7. @@ -7,7 +9,7 @@ import 'package:flutter/material.dart'; /// 可关注公众号:我的大前端生涯 获取最新技术分享 /// 可关注网易云课堂:https://study.163.com/instructor/1021406098.htm /// 可关注博客:https://blog.csdn.net/zl18603543572 -/// +/// 启动默认显示的页面 /// 代码清单 class IndexPage extends StatefulWidget { @@ -17,7 +19,34 @@ class IndexPage extends StatefulWidget { } } -class _IndexPageState extends State { +class _IndexPageState extends State with WidgetsBindingObserver { + //页面的初始化函数 + @override + void initState() { + super.initState(); + + checkPermisson(); + + //注册观察者 + WidgetsBinding.instance.addObserver(this); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + super.didChangeAppLifecycleState(state); + if (state == AppLifecycleState.resumed && _isGoSetting) { + checkPermisson(); + } + } + + @override + void dispose() { + //注销观察者 + WidgetsBinding.instance.removeObserver(this); + + super.dispose(); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -30,4 +59,91 @@ class _IndexPageState extends State { ), ); } + + void checkPermisson({PermissionStatus status}) async { + //权限 + Permission permission = Permission.storage; + + if (status == null) { + //权限状态 + status = await permission.status; + } + + if (status.isUndetermined) { + //第一次申请 + showPermissonAlert(_list[0], "同意", permission); + } else if (status.isDenied) { + //用户第一次申请拒绝 + showPermissonAlert(_list[1], "重试", permission); + } else if (status.isPermanentlyDenied) { + //第二次申请 用户拒绝 + showPermissonAlert(_list[2], "去设置中心", permission, isSetting: true); + } else { + //通过 + } + } + + List _list = [ + "为您更好的体验应用,所以需要获取您的手机文件存储权限,以保存您的一些偏好设置", + "您已拒绝权限,所以无法保存您的一些偏好设置,将无法使用APP", + "您已拒绝权限,请在设置中心中同意APP的权限请求", + "其他错误" + ]; + + //是否去设置中心 + bool _isGoSetting = false; + + void showPermissonAlert( + String message, String rightString, Permission permission, + {bool isSetting = false}) { + showCupertinoDialog( + builder: (BuildContext context) { + return CupertinoAlertDialog( + title: Text("温馨提示"), + content: Container( + padding: EdgeInsets.all(12), + child: Text(message), + ), + actions: [ + //左边的按钮 + CupertinoDialogAction( + child: Text("退出应用"), + onPressed: () { + closeApp(); + }, + ), + //右边的按钮 + CupertinoDialogAction( + child: Text("$rightString"), + onPressed: () { + //关闭弹框 + Navigator.of(context).pop(); + if (isSetting) { + _isGoSetting = true; + //去设置中心 + openAppSettings(); + } else { + //申请权限 + requestPermiss(permission); + } + }, + ) + ], + ); + }, + context: context); + } + + void requestPermiss(Permission permission) async { + //发起权限申请 + PermissionStatus status = await permission.request(); + + //校验 + checkPermisson(); + } + + void closeApp() { + //关闭应用的方法 + SystemChannels.platform.invokeMethod("SystemNavigator.pop"); + } } diff --git a/pubspec.lock b/pubspec.lock index fd17e98..41f3389 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -88,6 +88,27 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.8.0-nullsafety.1" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.0.1+1" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.0.1" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.0.3" sky_engine: dependency: transitive description: flutter @@ -151,3 +172,4 @@ packages: version: "2.1.0-nullsafety.3" sdks: dart: ">=2.10.0-110 <=2.11.0-213.0.dev" + flutter: ">=1.12.8 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 36d88f7..3d613d5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,10 +16,14 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.0 + # 权限请求 + permission_handler: ^5.0.1+1 + dev_dependencies: flutter_test: sdk: flutter + # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec