pikapika/lib/basic/config/ChooserRoot.dart

78 lines
2.0 KiB
Dart
Raw Normal View History

2021-10-07 03:47:28 +00:00
/// 文件夹选择器的根路径
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
2021-10-07 03:47:28 +00:00
import '../Common.dart';
import '../Method.dart';
2023-04-12 07:53:37 +00:00
import 'Platform.dart';
2021-10-07 03:47:28 +00:00
const _propertyName = "chooserRoot";
late String _chooserRoot;
2021-11-04 05:56:25 +00:00
Future<dynamic> initChooserRoot() async {
2021-10-07 03:47:28 +00:00
_chooserRoot = await method.loadProperty(_propertyName, "");
2023-03-30 14:38:28 +00:00
if (_chooserRoot.isEmpty) {
if (Platform.isAndroid) {
try {
_chooserRoot = await method.androidStorageRoot();
} catch (e) {
_chooserRoot = "/sdcard";
}
} else if (Platform.isMacOS || Platform.isLinux) {
_chooserRoot = await method.getHomeDir();
} else if (Platform.isWindows) {
_chooserRoot = "/";
2021-10-07 03:47:28 +00:00
}
}
}
Future<String> currentChooserRoot() async {
if (Platform.isAndroid) {
2023-04-12 07:53:37 +00:00
late bool g;
if (androidVersion < 30) {
g = await Permission.storage.request().isGranted;
}else{
g = await Permission.manageExternalStorage.request().isGranted;
}
if (!g) {
throw Exception("申请权限被拒绝");
}
}
2023-03-30 14:38:28 +00:00
return _chooserRoot;
}
2021-11-04 05:56:25 +00:00
Future<dynamic> _inputChooserRoot(BuildContext context) async {
2021-10-07 03:47:28 +00:00
String? input = await displayTextInputDialog(
context,
2021-12-15 04:22:40 +00:00
src: _chooserRoot,
title: '文件夹选择器根路径',
hint: '请输入文件夹选择器根路径',
desc: "导出时选择目录的默认路径, 同时也是根路径, 不能正常导出时也可以尝试设置此选项。",
2021-10-07 03:47:28 +00:00
);
if (input != null) {
await method.saveProperty(_propertyName, input);
_chooserRoot = input;
}
}
2021-11-04 03:45:22 +00:00
Widget chooserRootSetting() {
2022-05-14 13:45:00 +00:00
if (Platform.isIOS) {
return Container();
}
2021-11-04 03:45:22 +00:00
return StatefulBuilder(
builder: (BuildContext context, void Function(void Function()) setState) {
return ListTile(
2022-03-19 04:12:27 +00:00
title: const Text("文件夹选择器默认路径"),
2023-03-30 14:38:28 +00:00
subtitle: Text(_chooserRoot),
2021-11-04 03:45:22 +00:00
onTap: () async {
2021-11-04 05:56:25 +00:00
await _inputChooserRoot(context);
2021-11-04 03:45:22 +00:00
setState(() {});
},
);
},
);
}