mirror of
https://github.com/lakaola/chat-flutter.git
synced 2026-05-31 10:50:19 +08:00
88 lines
2.1 KiB
Dart
88 lines
2.1 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'package:demo/config/app_config.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:demo/tools/tools_perms.dart';
|
|
import 'package:demo/component/component_bottom.dart';
|
|
import 'package:demo/component/component_common.dart';
|
|
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
|
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
|
|
|
|
// 上传组件
|
|
class ComponentAvatar {
|
|
// 拍摄/图片
|
|
static Future<void> image({
|
|
required Function(String value) onTap,
|
|
}) async {
|
|
ComponentBottom([
|
|
BottomModel(
|
|
'图片',
|
|
onTap: () async {
|
|
// 关闭
|
|
Get.back();
|
|
// 权限
|
|
bool result = await ToolsPerms.photos();
|
|
if (!result) {
|
|
return;
|
|
}
|
|
// 选取
|
|
String portrait = await _image();
|
|
// 修改
|
|
onTap.call(portrait);
|
|
},
|
|
),
|
|
BottomModel(
|
|
'拍照',
|
|
onTap: () async {
|
|
// 关闭
|
|
Get.back();
|
|
// 权限
|
|
bool result = await ToolsPerms.camera();
|
|
if (!result) {
|
|
return;
|
|
}
|
|
// 拍照
|
|
String portrait = await _image(camera: true);
|
|
// 修改
|
|
onTap.call(portrait);
|
|
},
|
|
),
|
|
]);
|
|
}
|
|
|
|
// 拍摄/图片
|
|
static Future<String> _image({
|
|
bool camera = false,
|
|
}) async {
|
|
// 对象
|
|
AssetEntity? entity;
|
|
// 拍照
|
|
if (camera) {
|
|
entity = await CameraPicker.pickFromCamera(
|
|
AppConfig.navigatorKey.currentContext!,
|
|
);
|
|
}
|
|
// 本地选取
|
|
else {
|
|
// 选取
|
|
List<AssetEntity>? dataList = await AssetPicker.pickAssets(
|
|
AppConfig.navigatorKey.currentContext!,
|
|
pickerConfig: AssetPickerConfig(
|
|
maxAssets: 1,
|
|
requestType: RequestType.image,
|
|
pathNameBuilder: (AssetPathEntity path) {
|
|
return ComponentCommon.pathName(path);
|
|
},
|
|
),
|
|
);
|
|
entity = dataList!.first;
|
|
}
|
|
// 判断
|
|
if (entity == null) {
|
|
return '';
|
|
}
|
|
File? file = await entity.file;
|
|
return file?.path ?? '';
|
|
}
|
|
}
|