fix
This commit is contained in:
+31
-3
@@ -5,6 +5,7 @@ import 'package:cloud_archiver/utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:webdav_client/webdav_client.dart' as dav;
|
||||
|
||||
Future<void> main() async {
|
||||
@@ -46,6 +47,33 @@ class MyHomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
@override
|
||||
void initState() {
|
||||
checkAndRequestStoragePermission();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Future<void> checkAndRequestStoragePermission() async {
|
||||
// 1. 检查是否已经拥有“所有文件访问权限”
|
||||
var status = await Permission.manageExternalStorage.status;
|
||||
|
||||
if (!status.isGranted) {
|
||||
print("未获得管理外部存储权限,尝试申请...");
|
||||
|
||||
// 2. 首次申请,或者已经被拒绝过
|
||||
// 在 Android 11+ 上,这行代码会自动尝试跳转到系统的“所有文件访问权限”设置列表
|
||||
var requestStatus = await Permission.manageExternalStorage.request();
|
||||
|
||||
// 3. 如果系统未能自动跳转(某些国产ROM限制),则强制通过插件打开系统设置
|
||||
if (!requestStatus.isGranted) {
|
||||
print("自动跳转失败,提示用户手动去设置开启");
|
||||
await openAppSettings();
|
||||
}
|
||||
} else {
|
||||
print("已获得所有文件访问权限,可以放心执行 listSync()");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var width = MediaQuery.sizeOf(context).width;
|
||||
@@ -171,7 +199,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
? null
|
||||
: () {
|
||||
if (syncItemConfig != null) {
|
||||
upload(syncItemConfig, cloudModifiedTimeTs == null);
|
||||
upload(context, syncItemConfig, cloudModifiedTimeTs == null);
|
||||
}
|
||||
},
|
||||
child: Icon(Icons.upload_rounded, size: 22),
|
||||
@@ -184,7 +212,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
? null
|
||||
: () {
|
||||
if (syncItemConfig != null) {
|
||||
download(syncItemConfig, cloudArchives.sorted.first);
|
||||
download(context, syncItemConfig, cloudArchives.sorted.first);
|
||||
}
|
||||
},
|
||||
onLongPress: cloudModifiedTimeTs == null
|
||||
@@ -204,7 +232,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
value: file,
|
||||
onTap: () {
|
||||
if (syncItemConfig != null) {
|
||||
download(syncItemConfig, file);
|
||||
download(context, syncItemConfig, file);
|
||||
}
|
||||
},
|
||||
child: SizedBox(
|
||||
|
||||
Reference in New Issue
Block a user