前言
Tinypng是一个在设计和技术界十分流行的图片压缩网站,但是它只有网页版,没有GUI。幸好的是它支持通过apikey直接运行api接口压缩图片,虽然业内已经有很多版本的GUI,Window,Mac都有,但是这几天学习Flutter Deskstop,正好可以用来实战。目前的版本已打包了macos版本及window版本。
代码过程
实现选择文件
选择文件这块的实现,由于我本身是做iOS开发的,macOS原生开发其实也大同小异,但是为了兼容多端,我也懒得一个个写插件了,搜了下现成支持deskstop的插件发现file_picker这个插件完美支持我的想法,不管是window,mac,还是linux通通都支持。目前只支持选择jpg,png的文件,貌似webp和h265都是支持的,后期我可以加上。
void _pickFiles() async {
if (await controller.checkHaveApiKey() == false) {
_showSettingBottomSheet();
showToast("Please enter your TinyPNG Apikey",
textPadding: EdgeInsets.all(15));
return;
}
FilePickerResult? result =
await FilePicker.platform.pickFiles(allowMultiple: true);
if (result != null) {
List<File> files = result.paths.map((path) => File(path ?? "")).toList();
List<File> chooseFiles = [];
files.forEach((element) {
if (element.path.toLowerCase().endsWith("jpg") ||
element.path.toLowerCase().endsWit