mirror of
https://github.com/foss42/apidash.git
synced 2025-05-30 21:41:50 +08:00
Add saveToDownloads
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:apidash/utils/utils.dart';
|
||||
import 'package:apidash/widgets/widgets.dart';
|
||||
|
||||
@ -20,3 +21,30 @@ Future<void> saveCollection(
|
||||
sm.hideCurrentSnackBar();
|
||||
sm.showSnackBar(getSnackBar(message, small: false));
|
||||
}
|
||||
|
||||
Future<void> saveToDownloads(
|
||||
ScaffoldMessengerState sm, {
|
||||
Uint8List? content,
|
||||
String? mimeType,
|
||||
String? ext,
|
||||
String? name,
|
||||
}) async {
|
||||
var message = "";
|
||||
var path = await getFileDownloadpath(
|
||||
name,
|
||||
ext ?? getFileExtension(mimeType),
|
||||
);
|
||||
if (path != null) {
|
||||
try {
|
||||
await saveFile(path, content!);
|
||||
var sp = getShortPath(path);
|
||||
message = 'Saved to $sp';
|
||||
} catch (e) {
|
||||
message = "An error occurred while saving file.";
|
||||
}
|
||||
} else {
|
||||
message = "Unable to determine the download path.";
|
||||
}
|
||||
sm.hideCurrentSnackBar();
|
||||
sm.showSnackBar(getSnackBar(message, small: false));
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:apidash/utils/utils.dart';
|
||||
import 'package:apidash/consts.dart';
|
||||
import "snackbars.dart";
|
||||
|
||||
class SaveInDownloadsButton extends StatelessWidget {
|
||||
const SaveInDownloadsButton({
|
||||
@ -29,26 +28,13 @@ class SaveInDownloadsButton extends StatelessWidget {
|
||||
);
|
||||
const label = kLabelDownload;
|
||||
final onPressed = (content != null)
|
||||
? () async {
|
||||
var message = "";
|
||||
var path = await getFileDownloadpath(
|
||||
name,
|
||||
ext ?? getFileExtension(mimeType),
|
||||
);
|
||||
if (path != null) {
|
||||
try {
|
||||
await saveFile(path, content!);
|
||||
var sp = getShortPath(path);
|
||||
message = 'Saved to $sp';
|
||||
} catch (e) {
|
||||
message = "An error occurred while saving file.";
|
||||
}
|
||||
} else {
|
||||
message = "Unable to determine the download path.";
|
||||
}
|
||||
sm.hideCurrentSnackBar();
|
||||
sm.showSnackBar(getSnackBar(message, small: false));
|
||||
}
|
||||
? () => saveToDownloads(
|
||||
sm,
|
||||
content: content,
|
||||
mimeType: mimeType,
|
||||
ext: ext,
|
||||
name: name,
|
||||
)
|
||||
: null;
|
||||
|
||||
return showLabel
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
import 'package:apidash/main.dart';
|
||||
import 'package:apidash/app.dart';
|
||||
import 'package:apidash/common/utils.dart';
|
||||
import 'package:apidash/screens/screens.dart';
|
||||
|
||||
void main() {}
|
||||
|
Reference in New Issue
Block a user