Skip to content

Commit

Permalink
第四讲 动态权限申请
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaolongs committed Dec 8, 2020
1 parent d7fbd50 commit d75cf34
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)

1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.studyyoun.flutter_ho">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:label="flutter Ho"
android:icon="@mipmap/app_icon">
Expand Down
1 change: 1 addition & 0 deletions ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
68 changes: 68 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -32,7 +33,9 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand All @@ -44,19 +47,41 @@
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
97C146EB1CF9000F007C117D /* Frameworks */ = {
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 = "<group>";
};
8B5F970AAD82DE761B462407 /* Frameworks */ = {
isa = PBXGroup;
children = (
2628FE28A1CFF29338E92A1E /* libPods-Runner.a */,
);
name = Frameworks;
sourceTree = "<group>";
};
9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup;
children = (
Expand All @@ -74,6 +99,8 @@
9740EEB11CF90186004384FC /* Flutter */,
97C146F01CF9000F007C117D /* Runner */,
97C146EF1CF9000F007C117D /* Products */,
6E29A3D1E4EEA938EB5F7DA2 /* Pods */,
8B5F970AAD82DE761B462407 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -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 = (
);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions ios/Runner.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 118 additions & 2 deletions lib/src/index_page.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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.
///
/// 可关注公众号:我的大前端生涯 获取最新技术分享
/// 可关注网易云课堂:https://study.163.com/instructor/1021406098.htm
/// 可关注博客:https://blog.csdn.net/zl18603543572
///
/// 启动默认显示的页面
/// 代码清单
class IndexPage extends StatefulWidget {
Expand All @@ -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(
Expand All @@ -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<String> _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");
}
}
22 changes: 22 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d75cf34

Please sign in to comment.