題記
—— 執劍天涯,從你的點滴積累開始,所及之處,必精益求精,即是折騰每一天。
重要消息
- flutter中網絡請求dio使用分析 視訊教程在這裡
- Flutter 從入門實踐到開發一個APP之UI基礎篇 視訊
- Flutter 從入門實踐到開發一個APP之開發實戰基礎篇
- flutter跨平台開發一點一滴分析系列文章系列文章 在這裡了
///目前進度進度百分比 目前進度/總進度 從0-1
double currentProgress =0.0;
///下載下傳檔案的網絡路徑
String apkUrl ="";
///使用dio 下載下傳檔案
void downApkFunction() async{
/// 申請寫檔案權限
bool isPermiss = await checkPermissFunction();
if(isPermiss) {
///手機儲存目錄
String savePath = await getPhoneLocalPath();
String appName = "rk.apk";
///建立DIO
Dio dio = new Dio();
///參數一 檔案的網絡儲存URL
///參數二 下載下傳的本地目錄檔案
///參數三 下載下傳監聽
Response response = await dio.download(
apkUrl, "$savePath$appName", onReceiveProgress: (received, total) {
if (total != -1) {
///目前下載下傳的百分比例
print((received / total * 100).toStringAsFixed(0) + "%");
// CircularProgressIndicator(value: currentProgress,) 進度 0-1
currentProgress = received / total;
setState(() {
});
}
});
}else{
///提示使用者請同意權限申請
}
}
Android權限目前分為三種:正常權限、危險權限、特殊權限
正常權限 直接在AndroidManifest中配置即可獲得的權限。大部分權限都歸于此。
危險權限,Android 6.0之後将部分權限定義于此。
危險權限不僅需要需要在AndroidManifest中配置,還需要在使用前check是否真正擁有權限,以動态申請。
在ios中,使用xcode打開本目錄
選中Xcode 工程中的 info.plist檔案,右鍵選擇Open As - Source Code,将權限配置的代碼copy到裡面即可,鍵值對中的内容可按項目需求相應修改。
<!-- 相冊 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>需要您的同意,APP才能通路相冊</string>
<!-- 相機 -->
<key>NSCameraUsageDescription</key>
<string>需要您的同意,APP才能通路相機</string>
<!-- 麥克風 -->
<key>NSMicrophoneUsageDescription</key>
<string>需要您的同意,APP才能通路麥克風</string>
<!-- 位置 -->
<key>NSLocationUsageDescription</key>
<string>需要您的同意, APP才能通路位置</string>
<!-- 在使用期間通路位置 -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意, APP才能在使用期間通路位置</string>
<!-- 始終通路位置 -->
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意, APP才能始終通路位置</string>
<!-- 月曆 -->
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意, APP才能通路月曆</string>
<!-- 提醒事項 -->
<key>NSRemindersUsageDescription</key>
<string>需要您的同意, APP才能通路提醒事項</string>
<!-- 運動與健身 -->
<key>NSMotionUsageDescription</key>
<string>需要您的同意, APP才能通路運動與健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>需要您的同意, APP才能通路健康更新 </string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>需要您的同意, APP才能通路健康分享</string>
<!-- 藍牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>需要您的同意, APP才能通路藍牙</string>
<!-- 媒體資料庫 -->
<key>NSAppleMusicUsageDescription</key>
<string>需要您的同意, APP才能通路媒體資料庫</string>
在 flutter 項目目錄中,我們也可以打開 info.plist 檔案配置,如下圖所示
在這裡使用的是 permission_handler 插件來申請權限的
permission_handler: ^4.3.0
申請權限代碼如下
///PermissionGroup.storage 對應的是
///android 的外部存儲 (External Storage)
///ios 的Documents` or `Downloads`
checkPermissFunction() async {
if (Theme.of(context).platform == TargetPlatform.android) {
///安卓平台中 checkPermissionStatus方法校驗是否有儲存卡的讀寫權限
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.storage);
if (permission != PermissionStatus.granted) {
///無權限那麼 調用方法 requestPermissions 申請權限
Map<PermissionGroup, PermissionStatus> permissions =
await PermissionHandler()
.requestPermissions([PermissionGroup.storage]);
///校驗使用者對權限申請的處理
if (permissions[PermissionGroup.storage] == PermissionStatus.granted) {
return true;
}
} else {
return true;
}
} else {
return true;
}
return false;
}
申請好權限後,我們需要确定下來儲存卡的路徑,在這裡使用的是 path_provider 插件
path_provider: 1.6.0
///擷取手機的存儲目錄路徑
///getExternalStorageDirectory() 擷取的是 android 的外部存儲 (External Storage)
/// getApplicationDocumentsDirectory 擷取的是 ios 的Documents` or `Downloads` 目錄
Future<String> getPhoneLocalPath() async {
final directory = Theme.of(context).platform == TargetPlatform.android
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
return directory.path;
}
完畢