// Author: fengshengxiong // Date: 2024/5/11 // Description: 设置锁屏/首页壁纸 import 'package:async_wallpaper/async_wallpaper.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:wallpaper/common/components/divider_widget.dart'; import 'package:wallpaper/common/components/easy_loading.dart'; import 'package:wallpaper/res/themes/app_colors.dart'; class WallpaperPicker { static void picker(String imgPath) { Get.bottomSheet( Container( color: Colors.white, child: IntrinsicHeight( child: Column( children: [ _bottomSheetItem('Lock Screen', () { Get.back(); setWallpaper(imgPath, AsyncWallpaper.LOCK_SCREEN); }), const DividerWidget(), _bottomSheetItem('Home Screen', () { Get.back(); setWallpaper(imgPath, AsyncWallpaper.HOME_SCREEN); }), const DividerWidget(), _bottomSheetItem('Both Screen', () { Get.back(); setWallpaper(imgPath, AsyncWallpaper.BOTH_SCREENS); }), const DividerWidget(), _bottomSheetItem('Cancel', Get.back), ], ), ), ), ); } static Widget _bottomSheetItem(String label, Function() onTap) { return SizedBox( width: 1.sw, child: TextButton( onPressed: onTap, child: Text( label, style: TextStyle( color: seedColor, fontSize: 18.sp, fontWeight: FontWeight.w500, ), ), ), ); } static Future setWallpaper(String imgPath, int wallpaperLocation) async { loading(); String result; try { result = await AsyncWallpaper.setWallpaperFromFile( filePath: imgPath, wallpaperLocation: AsyncWallpaper.LOCK_SCREEN, // toastDetails: ToastDetails.success(), // errorToastDetails: ToastDetails.error(), ) ? 'Wallpaper applied successfully' : 'Failed to get wallpaper'; } on PlatformException { result = 'Failed to get wallpaper'; } dismiss(); toast(result); } }