107 lines
2.9 KiB
Dart
107 lines
2.9 KiB
Dart
import 'dart:io';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http_parser/http_parser.dart';
|
|
import 'package:image_cropper/image_cropper.dart';
|
|
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
|
|
|
import '../../apis/app.dart';
|
|
|
|
class ClipPage extends StatefulWidget {
|
|
final AssetEntity arguments;
|
|
|
|
const ClipPage({super.key, required this.arguments});
|
|
|
|
@override
|
|
State<ClipPage> createState() => _ClipPageState();
|
|
}
|
|
|
|
class _ClipPageState extends State<ClipPage> {
|
|
String? _croppedImagePath;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadImage(); // 加载图片以进行裁剪
|
|
print("进入裁剪页面");
|
|
}
|
|
|
|
Future<void> _loadImage() async {
|
|
final file = await widget.arguments.file;
|
|
if (file != null) {
|
|
// 裁剪图片
|
|
CroppedFile? croppedFile = await ImageCropper().cropImage(
|
|
sourcePath: file.path,
|
|
aspectRatio: const CropAspectRatio(ratioX: 4, ratioY: 3), // 设置裁剪比例为 4:3
|
|
uiSettings: [
|
|
AndroidUiSettings(
|
|
toolbarTitle: '裁剪图片',
|
|
toolbarColor: const Color(0xff0C0C16),
|
|
toolbarWidgetColor: Colors.white,
|
|
initAspectRatio: CropAspectRatioPreset.original,
|
|
lockAspectRatio: true, // 锁定比例
|
|
hideBottomControls: true, // 隐藏底部的刷新和缩放按钮
|
|
),
|
|
IOSUiSettings(
|
|
minimumAspectRatio: 1.0,
|
|
),
|
|
],
|
|
);
|
|
|
|
if (croppedFile != null) {
|
|
setState(() {
|
|
_croppedImagePath = croppedFile.path; // 保存裁剪后的路径
|
|
_updateUserBackground(); // 确认裁剪并调用接口
|
|
});
|
|
} else {
|
|
print("croppedFile为空");
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _updateUserBackground() async {
|
|
if (_croppedImagePath != null) {
|
|
// formData
|
|
FormData formData = FormData.fromMap({
|
|
'background': await MultipartFile.fromFile(
|
|
_croppedImagePath!,
|
|
filename: 'image.jpg',
|
|
contentType: MediaType("image", "png"),
|
|
),
|
|
'username': "18847025990", // 根据需求替换用户名
|
|
});
|
|
// 调用接口上传裁剪后的图片路径
|
|
final result = await userApi.getUpdateUserBg(formData);
|
|
print("result:$result");
|
|
if (result['code'] == 200) {
|
|
print("发送成功");
|
|
// Navigator.pushNamed(context, '/mine'); // 跳转到 mine 页面
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context,
|
|
'/tabs',
|
|
(route) => false,
|
|
arguments: {'initialIndex': 4},
|
|
);
|
|
} else {
|
|
print("发送失败");
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('裁剪图片'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.check), // 确认图标
|
|
onPressed: () {},
|
|
),
|
|
],
|
|
),
|
|
|
|
);
|
|
}
|
|
}
|