|
...
|
...
|
@@ -8,21 +8,27 @@ import 'package:dio/dio.dart'; |
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_picker/Picker.dart';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
import 'package:one_key/provider/user_model.dart';
|
|
|
|
import 'package:one_key/res/colors.dart';
|
|
|
|
import 'package:one_key/widgets/round_button.dart';
|
|
|
|
import 'package:one_key/widgets/system_app_bar.dart';
|
|
|
|
import 'package:one_key/widgets/v_empty_view.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
import '../../../../app.dart';
|
|
|
|
import '../../../../net/dio_util.dart';
|
|
|
|
import '../../../../utils/dialog_util.dart';
|
|
|
|
import '../../../../utils/screen_util.dart';
|
|
|
|
import '../../../../utils/utils.dart';
|
|
|
|
import '../../../../widgets/h_empty_view.dart';
|
|
|
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
|
|
|
import 'package:android_path_provider/android_path_provider.dart';
|
|
|
|
// import 'package:flutter_downloader/flutter_downloader.dart';
|
|
|
|
// import 'package:android_path_provider/android_path_provider.dart';
|
|
|
|
import 'package:open_file/open_file.dart';
|
|
|
|
import 'package:excel/excel.dart' as excelUtil;
|
|
|
|
|
|
|
|
import '../../../../widgets/loading.dart';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 导出预订数据
|
|
...
|
...
|
@@ -43,11 +49,11 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
|
|
|
|
List<String> data = ['到店时间', '下单时间'];
|
|
|
|
int timeIndex;
|
|
|
|
ReceivePort _port = ReceivePort();
|
|
|
|
// ReceivePort _port = ReceivePort();
|
|
|
|
String _localPath;
|
|
|
|
|
|
|
|
String taskId;
|
|
|
|
DownloadTaskStatus status;
|
|
|
|
// DownloadTaskStatus status;
|
|
|
|
int progress;
|
|
|
|
|
|
|
|
@override
|
|
...
|
...
|
@@ -181,21 +187,21 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@pragma('vm:entry-point')
|
|
|
|
static void downloadCallback(
|
|
|
|
String id,
|
|
|
|
int status,
|
|
|
|
int progress,
|
|
|
|
) {
|
|
|
|
final SendPort send =
|
|
|
|
IsolateNameServer.lookupPortByName('downloader_send_port');
|
|
|
|
send.send([id, status, progress]);
|
|
|
|
}
|
|
|
|
// @pragma('vm:entry-point')
|
|
|
|
// static void downloadCallback(
|
|
|
|
// String id,
|
|
|
|
// int status,
|
|
|
|
// int progress,
|
|
|
|
// ) {
|
|
|
|
// final SendPort send =
|
|
|
|
// IsolateNameServer.lookupPortByName('downloader_send_port');
|
|
|
|
// send.send([id, status, progress]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 根据taskId打开下载文件
|
|
|
|
Future<bool> _openDownloadedFile(taskId) {
|
|
|
|
return FlutterDownloader.open(taskId: taskId);
|
|
|
|
}
|
|
|
|
// // 根据taskId打开下载文件
|
|
|
|
// Future<bool> _openDownloadedFile(taskId) {
|
|
|
|
// return FlutterDownloader.open(taskId: taskId);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// void _bindBackgroundIsolate() {
|
|
|
|
// final isSuccess = IsolateNameServer.registerPortWithName(
|
|
...
|
...
|
@@ -270,17 +276,17 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
Future<void> _delete() async {
|
|
|
|
final task = await FlutterDownloader.loadTasks();
|
|
|
|
if (task != null) {
|
|
|
|
task.forEach((element) {
|
|
|
|
FlutterDownloader.remove(
|
|
|
|
taskId: element.taskId,
|
|
|
|
shouldDeleteContent: true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Future<void> _delete() async {
|
|
|
|
// final task = await FlutterDownloader.loadTasks();
|
|
|
|
// if (task != null) {
|
|
|
|
// task.forEach((element) {
|
|
|
|
// FlutterDownloader.remove(
|
|
|
|
// taskId: element.taskId,
|
|
|
|
// shouldDeleteContent: true,
|
|
|
|
// );
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
Future<void> _prepareSaveDir() async {
|
|
|
|
_localPath = (await _getSavedDir());
|
|
...
|
...
|
@@ -299,9 +305,10 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
// } catch (err, st) {
|
|
|
|
// print('failed to get downloads path: $err, $st');
|
|
|
|
|
|
|
|
// if()/
|
|
|
|
|
|
|
|
final directory = await getExternalStorageDirectory();
|
|
|
|
externalStorageDirPath = directory?.path;
|
|
|
|
// }
|
|
|
|
} else if (Platform.isIOS) {
|
|
|
|
externalStorageDirPath =
|
|
|
|
(await getApplicationDocumentsDirectory()).absolute.path;
|
|
...
|
...
|
@@ -325,6 +332,23 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
// try {
|
|
|
|
// externalStorageDirPath = await AndroidPathProvider.downloadsPath;
|
|
|
|
// } catch (err, st) {
|
|
|
|
// print('failed to get downloads path: $err, $st');
|
|
|
|
|
|
|
|
// if()/
|
|
|
|
PermissionStatus permissionStatus = await Permission.storage.status;
|
|
|
|
|
|
|
|
if (permissionStatus != PermissionStatus.permanentlyDenied) {
|
|
|
|
//全局提示权限使用目的
|
|
|
|
BuildContext context = App.navigatorKey.currentState.overlay.context;
|
|
|
|
DialogUtil.showAuthorityDestination(
|
|
|
|
context, 'h_storage', '存储权限使用说明', '用于存储下载的预订表。');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final result = await Permission.storage.request();
|
|
|
|
return result == PermissionStatus.granted;
|
|
|
|
}
|
|
...
|
...
|
@@ -332,9 +356,9 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
throw StateError('unknown platform');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _unbindBackgroundIsolate() {
|
|
|
|
IsolateNameServer.removePortNameMapping('downloader_send_port');
|
|
|
|
}
|
|
|
|
// void _unbindBackgroundIsolate() {
|
|
|
|
// IsolateNameServer.removePortNameMapping('downloader_send_port');
|
|
|
|
// }
|
|
|
|
|
|
|
|
///时间选择
|
|
|
|
Widget _select(String title, String hintText, GestureTapCallback onTap) {
|
|
...
|
...
|
@@ -418,6 +442,86 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _download() async {
|
|
|
|
Loading.showLoading(
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
Stopwatch stopwatch = new Stopwatch()..start();
|
|
|
|
|
|
|
|
excelUtil.Excel excel = excelUtil.Excel.createExcel();
|
|
|
|
excelUtil.Sheet sh = excel['Sheet1'];
|
|
|
|
//表头
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
sh
|
|
|
|
.cell(
|
|
|
|
excelUtil.CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i))
|
|
|
|
.value = 'Column $i';
|
|
|
|
//sh.cell(CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i)).cellStyle =CellStyle(bold: true);
|
|
|
|
}
|
|
|
|
for (int row = 1; row < 9; row++) {
|
|
|
|
for (int column = 0; column < 8; column++) {
|
|
|
|
sh
|
|
|
|
.cell(excelUtil.CellIndex.indexByColumnRow(
|
|
|
|
rowIndex: row, columnIndex: column))
|
|
|
|
.value = '$row$column value';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print('Generating executed in ${stopwatch.elapsed}');
|
|
|
|
stopwatch.reset();
|
|
|
|
var fileBytes = excel.encode();
|
|
|
|
|
|
|
|
print('Encoding executed in ${stopwatch.elapsed}');
|
|
|
|
stopwatch.reset();
|
|
|
|
String sellername =
|
|
|
|
Provider.of<UserModel>(context, listen: false).user.sellername;
|
|
|
|
String starttime = fromDate.replaceAll('-', '');
|
|
|
|
String endtime = endDate.replaceAll('-', '');
|
|
|
|
if (fileBytes != null) {
|
|
|
|
File file =
|
|
|
|
File(_localPath + "/${sellername}${starttime}至${endtime}预订.xlsx")
|
|
|
|
..createSync(recursive: true)
|
|
|
|
..writeAsBytesSync(fileBytes);
|
|
|
|
file.copy('/storage/emulated/0/Download/dongle.xlsx');
|
|
|
|
Loading.hideLoading(context);
|
|
|
|
DialogUtil.cupertinoAlertDialog(context,
|
|
|
|
title: '提示',
|
|
|
|
content: '文件下载完成,是否打开?',
|
|
|
|
lTitle: '取消',
|
|
|
|
rTitle: '打开', lCallback: () {
|
|
|
|
progress = null;
|
|
|
|
setState(() {});
|
|
|
|
}, rCallback: () async {
|
|
|
|
// Navigator.pop(context);
|
|
|
|
|
|
|
|
// print(_localPath);
|
|
|
|
|
|
|
|
// final tasks = await FlutterDownloader.loadTasks();
|
|
|
|
// String str = '';
|
|
|
|
// tasks.forEach((element) {
|
|
|
|
// str = element.savedDir + '/' + element.filename;
|
|
|
|
// });
|
|
|
|
// print(str);
|
|
|
|
|
|
|
|
// if (str != '') {
|
|
|
|
// print(savePath);
|
|
|
|
// progress = null;
|
|
|
|
// setState(() {});
|
|
|
|
OpenFile.open(
|
|
|
|
_localPath + "/${sellername}${starttime}至${endtime}预订.xlsx");
|
|
|
|
// _delete();
|
|
|
|
// }
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Loading.hideLoading(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
print('Downloaded executed in ${stopwatch.elapsed}');
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> openSetting() {
|
|
|
|
return openAppSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
...
|
...
|
@@ -554,87 +658,98 @@ class _ExportBookDataPageState extends State<ExportBookDataPage> { |
|
|
|
fontSize: 32,
|
|
|
|
isBold: true,
|
|
|
|
onPressed: () async {
|
|
|
|
if (progress != null) {
|
|
|
|
// if (progress != null) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (timeType == '点击选择') {
|
|
|
|
Utils.showToast('请选择时间类型');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fromDate == '点击选择') {
|
|
|
|
Utils.showToast('请选择开始日期');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (endDate == '点击选择') {
|
|
|
|
Utils.showToast('请选择结束日期');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _permissionReady = await _checkPermission();
|
|
|
|
if (_permissionReady) {
|
|
|
|
await _prepareSaveDir();
|
|
|
|
}
|
|
|
|
// _delete();
|
|
|
|
String savePath = _localPath + "/fff.pdf";
|
|
|
|
Directory directory = new Directory(savePath);
|
|
|
|
if (directory.existsSync()) {
|
|
|
|
//删除历史
|
|
|
|
final List<FileSystemEntity> files = directory.listSync();
|
|
|
|
for (FileSystemEntity file in files) {
|
|
|
|
file.deleteSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Response response = await DioUtil().download(
|
|
|
|
'http://englishonlineclub.com/pdf/iOS%20Programming%20-%20The%20Big%20Nerd%20Ranch%20Guide%20(6th%20Edition)%20[EnglishOnlineClub.com].pdf',
|
|
|
|
savePath, onProgress: (received, total) {
|
|
|
|
if (progress != 100) {
|
|
|
|
progress = ((received / total) * 100).round();
|
|
|
|
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
if (received == total) {
|
|
|
|
//下载完成
|
|
|
|
print('xaizaiwancheng');
|
|
|
|
|
|
|
|
DialogUtil.cupertinoAlertDialog(context,
|
|
|
|
title: '提示',
|
|
|
|
content: '文件下载完成,是否打开?',
|
|
|
|
lTitle: '取消',
|
|
|
|
rTitle: '打开', lCallback: () {
|
|
|
|
progress = null;
|
|
|
|
setState(() {});
|
|
|
|
}, rCallback: () async {
|
|
|
|
// Navigator.pop(context);
|
|
|
|
|
|
|
|
// print(_localPath);
|
|
|
|
|
|
|
|
// final tasks = await FlutterDownloader.loadTasks();
|
|
|
|
// String str = '';
|
|
|
|
// tasks.forEach((element) {
|
|
|
|
// str = element.savedDir + '/' + element.filename;
|
|
|
|
// });
|
|
|
|
// print(str);
|
|
|
|
|
|
|
|
// if (str != '') {
|
|
|
|
print(savePath);
|
|
|
|
progress = null;
|
|
|
|
setState(() {});
|
|
|
|
OpenFile.open(savePath);
|
|
|
|
// _delete();
|
|
|
|
// }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_download();
|
|
|
|
} else {
|
|
|
|
String title = Platform.isAndroid ? '存储' : '相册';
|
|
|
|
|
|
|
|
//用户选择不在弹出权限提示框
|
|
|
|
print('never');
|
|
|
|
// Navigator.pop(context);
|
|
|
|
DialogUtil.openAlertDialog(context, '未获得${title}权限',
|
|
|
|
"请在手机的权限管理里,将${title}权限开启", '去设置', 340.0, onTap: () {
|
|
|
|
openSetting();
|
|
|
|
Navigator.pop(context, '');
|
|
|
|
});
|
|
|
|
print(response);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
Utils.showToast('下载异常,请稍后重试');
|
|
|
|
}
|
|
|
|
|
|
|
|
// taskId = await FlutterDownloader.enqueue(
|
|
|
|
// url:
|
|
|
|
// _delete();
|
|
|
|
// String savePath = _localPath + "/fff.pdf";
|
|
|
|
// Directory directory = new Directory(savePath);
|
|
|
|
// if (directory.existsSync()) {
|
|
|
|
// //删除历史
|
|
|
|
// final List<FileSystemEntity> files = directory.listSync();
|
|
|
|
// for (FileSystemEntity file in files) {
|
|
|
|
// file.deleteSync();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// try {
|
|
|
|
// Response response = await DioUtil().download(
|
|
|
|
// 'http://englishonlineclub.com/pdf/iOS%20Programming%20-%20The%20Big%20Nerd%20Ranch%20Guide%20(6th%20Edition)%20[EnglishOnlineClub.com].pdf',
|
|
|
|
// headers: {
|
|
|
|
// // 'auth': 'test_for_sql_encoding'
|
|
|
|
// }, // optional: header send with url (auth token etc)
|
|
|
|
// savedDir: _localPath,
|
|
|
|
// saveInPublicStorage: true,
|
|
|
|
// // fileName:
|
|
|
|
// // // showNotification:
|
|
|
|
// // // true, // show download progress in status bar (for Android)
|
|
|
|
// // openFileFromNotification:
|
|
|
|
// // true, // click on notification to open downloaded file (for Android)
|
|
|
|
// );
|
|
|
|
// savePath, onProgress: (received, total) {
|
|
|
|
// if (progress != 100) {
|
|
|
|
// progress = ((received / total) * 100).round();
|
|
|
|
|
|
|
|
// setState(() {});
|
|
|
|
// }
|
|
|
|
// if (received == total) {
|
|
|
|
// //下载完成
|
|
|
|
// print('xaizaiwancheng');
|
|
|
|
|
|
|
|
// DialogUtil.cupertinoAlertDialog(context,
|
|
|
|
// title: '提示',
|
|
|
|
// content: '文件下载完成,是否打开?',
|
|
|
|
// lTitle: '取消',
|
|
|
|
// rTitle: '打开', lCallback: () {
|
|
|
|
// progress = null;
|
|
|
|
// setState(() {});
|
|
|
|
// }, rCallback: () async {
|
|
|
|
// // Navigator.pop(context);
|
|
|
|
|
|
|
|
// // print(_localPath);
|
|
|
|
|
|
|
|
// // final tasks = await FlutterDownloader.loadTasks();
|
|
|
|
// // String str = '';
|
|
|
|
// // tasks.forEach((element) {
|
|
|
|
// // str = element.savedDir + '/' + element.filename;
|
|
|
|
// // });
|
|
|
|
// // print(str);
|
|
|
|
|
|
|
|
// // if (str != '') {
|
|
|
|
// print(savePath);
|
|
|
|
// progress = null;
|
|
|
|
// setState(() {});
|
|
|
|
// OpenFile.open(savePath);
|
|
|
|
// // _delete();
|
|
|
|
// // }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// print(response);
|
|
|
|
// } catch (e) {
|
|
|
|
// print(e);
|
|
|
|
// Utils.showToast('下载异常,请稍后重试');
|
|
|
|
// }
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
...
|
...
|
|