Commit 53a4b750 authored by netyouli's avatar netyouli

添加拍照上传功能和优化生成图片进度显示方式

parent d9f47644
...@@ -10,8 +10,13 @@ class AppColor { ...@@ -10,8 +10,13 @@ class AppColor {
static const Color gray4 = Color.fromARGB(255, 106, 106, 106); static const Color gray4 = Color.fromARGB(255, 106, 106, 106);
static const Color gray5 = Color.fromARGB(255, 40, 40, 40); static const Color gray5 = Color.fromARGB(255, 40, 40, 40);
static const Color gray6 = Color.fromARGB(255, 30, 30, 30); static const Color gray6 = Color.fromARGB(255, 30, 30, 30);
static const Color gray7 = Color.fromARGB(255, 50, 50, 50);
static const Color black1 = Color.fromARGB(255, 20, 20, 20); static const Color black1 = Color.fromARGB(255, 20, 20, 20);
static const Color gradient_back1 = Color.fromARGB(255, 15, 24, 29);
static const Color gradient_back2 = Color.fromARGB(255, 13, 30, 39);
static const Color gradient_back3 = Color.fromARGB(255, 15, 52, 63);
static const Color yellow1 = Color.fromARGB(255, 150, 100, 30); static const Color yellow1 = Color.fromARGB(255, 150, 100, 30);
......
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data'; import 'dart:typed_data';
...@@ -31,6 +32,7 @@ class AIDrawImageResultController extends GetxController { ...@@ -31,6 +32,7 @@ class AIDrawImageResultController extends GetxController {
var imageUrl = "".obs; var imageUrl = "".obs;
var progress = 0.obs; var progress = 0.obs;
var showProgressView = false.obs; var showProgressView = false.obs;
var loseTime = "0.0s".obs;
Rx<Uint8List?> imageData = Rx<Uint8List?>(null); Rx<Uint8List?> imageData = Rx<Uint8List?>(null);
Uint8List? placehoderImageData; Uint8List? placehoderImageData;
...@@ -40,6 +42,7 @@ class AIDrawImageResultController extends GetxController { ...@@ -40,6 +42,7 @@ class AIDrawImageResultController extends GetxController {
Rx<List<String>> bottomButtonTitles = Rx<List<String>>([]); Rx<List<String>> bottomButtonTitles = Rx<List<String>>([]);
var opearter = ""; var opearter = "";
var imageUrls = List.generate(0, (index) => ""); var imageUrls = List.generate(0, (index) => "");
Timer? _timer;
@override @override
void onInit() { void onInit() {
...@@ -70,6 +73,7 @@ class AIDrawImageResultController extends GetxController { ...@@ -70,6 +73,7 @@ class AIDrawImageResultController extends GetxController {
@override @override
void dispose() { void dispose() {
SSEClient.unsubscribeFromSSE(); SSEClient.unsubscribeFromSSE();
_timer?.cancel();
super.dispose(); super.dispose();
} }
...@@ -79,6 +83,16 @@ class AIDrawImageResultController extends GetxController { ...@@ -79,6 +83,16 @@ class AIDrawImageResultController extends GetxController {
} }
} }
void startTimer() {
_timer?.cancel();
const oneSec = Duration(milliseconds: 10);
var sumTime = 0.0;
_timer = Timer.periodic(oneSec, (Timer timer) {
sumTime += 0.01;
loseTime.value = "${sumTime.toStringAsFixed(2)}s";
});
}
void loadImage(String imageUrl, int progress) async { void loadImage(String imageUrl, int progress) async {
final imageData = await HttpUtil().getData(imageUrl); final imageData = await HttpUtil().getData(imageUrl);
if (imageData != null) { if (imageData != null) {
...@@ -86,6 +100,7 @@ class AIDrawImageResultController extends GetxController { ...@@ -86,6 +100,7 @@ class AIDrawImageResultController extends GetxController {
this.imageData.value = imageData; this.imageData.value = imageData;
if (progress == 100) { if (progress == 100) {
showProgressView.value = false; showProgressView.value = false;
_timer?.cancel();
} }
} }
} }
...@@ -104,6 +119,7 @@ class AIDrawImageResultController extends GetxController { ...@@ -104,6 +119,7 @@ class AIDrawImageResultController extends GetxController {
} else { } else {
EasyLoading.dismiss(); EasyLoading.dismiss();
showProgressView.value = true; showProgressView.value = true;
startTimer();
await initEventSource(res.data ?? ""); await initEventSource(res.data ?? "");
} }
// state.isLoading = false; // state.isLoading = false;
...@@ -139,6 +155,7 @@ class AIDrawImageResultController extends GetxController { ...@@ -139,6 +155,7 @@ class AIDrawImageResultController extends GetxController {
EasyLoading.showToast(res.message ?? "未知错误"); EasyLoading.showToast(res.message ?? "未知错误");
} else { } else {
showProgressView.value = true; showProgressView.value = true;
startTimer();
await initEventSource(res.data ?? ""); await initEventSource(res.data ?? "");
} }
} }
...@@ -168,6 +185,7 @@ class AIDrawImageResultController extends GetxController { ...@@ -168,6 +185,7 @@ class AIDrawImageResultController extends GetxController {
EasyLoading.showToast(res.message ?? "未知错误"); EasyLoading.showToast(res.message ?? "未知错误");
} else { } else {
showProgressView.value = true; showProgressView.value = true;
startTimer();
await initEventSource(res.data ?? ""); await initEventSource(res.data ?? "");
} }
} }
......
...@@ -14,25 +14,40 @@ class AIDrawImageResultPage extends GetView<AIDrawImageResultController> { ...@@ -14,25 +14,40 @@ class AIDrawImageResultPage extends GetView<AIDrawImageResultController> {
Widget makeProgressView() { Widget makeProgressView() {
return Container( return Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.black26 gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
AppColor.gradient_back1.withOpacity(1.0 - (controller.progress.value.toDouble() / 100.0)),
AppColor.gradient_back2.withOpacity(1.0 - (controller.progress.value.toDouble() / 100.0)),
AppColor.gradient_back3.withOpacity(1.0 - (controller.progress.value.toDouble() / 100.0))],
),
), ),
width: Get.width,
height: Get.height,
alignment: Alignment.center, alignment: Alignment.center,
child: Container( child: Container(
width: 150, width: 260,
height: 150, height: 150,
decoration: BoxDecoration( // decoration: BoxDecoration(
color: Colors.grey, // color: Colors.grey,
borderRadius: BorderRadius.circular(10) // borderRadius: BorderRadius.circular(10)
), // ),
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
CircularProgressIndicator( Text("AI生成进度:${controller.progress.value}%"),
backgroundColor: Colors.red, SizedBox(height: 10,),
valueColor: AlwaysStoppedAnimation(Colors.blue), ClipRRect(
borderRadius: BorderRadius.circular(10),
child: LinearProgressIndicator(
backgroundColor: AppColor.gray7,
valueColor: AlwaysStoppedAnimation(Colors.blue),
value: controller.progress.value.toDouble() / 100.0,
),
), ),
SizedBox(height: 10,), SizedBox(height: 10,),
Text("AI生成进度:${controller.progress.value}%") Text(controller.loseTime.value),
], ],
), ),
), ),
......
...@@ -100,19 +100,23 @@ class AIDrawImageController extends GetxController { ...@@ -100,19 +100,23 @@ class AIDrawImageController extends GetxController {
uploadImageUrls.clear(); uploadImageUrls.clear();
} }
selectImageUpload() async { selectImageUpload(ImageSource source) async {
/* List<XFile>? images;
final image = await ImagePicker().pickImage(source: ImageSource.gallery); if (source == ImageSource.camera) {
if (image == null) return null; final image = await ImagePicker().pickImage(source: ImageSource.camera);
*/ if (image == null) return null;
uploadImageUrls.clear(); images = [image];
List<XFile>? images = await ImagePicker().pickMultiImage(); } else {
images = await ImagePicker().pickMultiImage();
}
final count = images.length; final count = images.length;
if (count == 0) { return; } if (count == 0) { return; }
if (count <= 1 || count > 5) { if (count > 5) {
EasyLoading.showError("请选择2-5张图片"); EasyLoading.showError("请选择1-5张图片");
} else { } else {
assets.value = images; for (var image in images) {
assets.add(image);
}
} }
EasyLoading.show( EasyLoading.show(
status: "正在上传,请稍后", status: "正在上传,请稍后",
......
...@@ -5,21 +5,102 @@ import 'package:chart/pages/ai-draw-image/image_square_view.dart'; ...@@ -5,21 +5,102 @@ import 'package:chart/pages/ai-draw-image/image_square_view.dart';
import 'package:chart/pages/ai-draw-image/text_to_image_view.dart'; import 'package:chart/pages/ai-draw-image/text_to_image_view.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import '../../common/style/color.dart'; import '../../common/style/color.dart';
import 'controller.dart'; import 'controller.dart';
showSheetAlert() {
final controller = Get.find<AIDrawImageController>();
Get.bottomSheet(
SafeArea(
child: Container(
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(20))
),
padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
height: 138,
child: Column(
children: [
InkWell(
onTap: () {
controller.selectImageUpload(ImageSource.camera);
Get.back();
},
child: Container(
height: 44,
width: Get.width - 40,
alignment: Alignment.center,
decoration: BoxDecoration(
color: AppColor.primary,
borderRadius: BorderRadius.circular(22)
),
child: Text("拍照"),
),
),
SizedBox(height: 10,),
InkWell(
onTap: () {
controller.selectImageUpload(ImageSource.gallery);
Get.back();
},
child: Container(
height: 44,
width: Get.width - 40,
alignment: Alignment.center,
decoration: BoxDecoration(
color: AppColor.primary,
borderRadius: BorderRadius.circular(22)
),
child: Text("相册"),
),
)
],
),
)
), persistent: false
);
}
Widget makeShowAssetImageView(double width) { Widget makeShowAssetImageView(double width) {
final controller = Get.find<AIDrawImageController>(); final controller = Get.find<AIDrawImageController>();
final count = controller.assets.length < 5 ? controller.assets.length + 1 : controller.assets.length;
final children = List<Widget>.generate(count, (index) {
if (index < controller.assets.length) {
final element = controller.assets[index];
return Image.file(File(element.path), width: (width - 20) / 5.0, height: 160, fit: BoxFit.contain,);
}
return Container(
width: (width - 20) / 5.0,
height: 160,
alignment: Alignment.center,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
child: InkWell(
onTap: () {
showSheetAlert();
},
child: Container(
alignment: Alignment.center,
// decoration: BoxDecoration(
// border: Border.all(
// width: 1,
// color: AppColor.primary
// ),
// borderRadius: BorderRadius.circular(5)
// ),
child: Image(image: AssetImage("assets/images/添加图片.png", ), width: 30, height: 30,),
),
),
);
});
return Container( return Container(
child: Stack( child: Stack(
children: [ children: [
Row( Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: controller.assets.map((element) { children: children,
return Image.file(File(element.path), width: (width - 20) / 5.0, height: 160, fit: BoxFit.contain,);
}).toList(),
), ),
], ],
), ),
...@@ -71,7 +152,7 @@ import 'controller.dart'; ...@@ -71,7 +152,7 @@ import 'controller.dart';
visible: controller.assets.isEmpty, visible: controller.assets.isEmpty,
child: InkWell( child: InkWell(
onTap: () { onTap: () {
controller.selectImageUpload(); showSheetAlert();
}, },
child: Container( child: Container(
height: 160, height: 160,
......
...@@ -127,15 +127,19 @@ class AIDrawImagePage extends GetView<AIDrawImageController> { ...@@ -127,15 +127,19 @@ class AIDrawImagePage extends GetView<AIDrawImageController> {
builder: (_) => Scaffold( builder: (_) => Scaffold(
body: Stack( body: Stack(
children: [ children: [
Image( Positioned(
image: AssetImage("assets/images/top_ai_draw_image.jpeg"), top: -20,
height: 300, left: -(Get.width * 1.2 - Get.width) / 2.0,
width: Get.width, child: Image(
fit: BoxFit.fitWidth, image: AssetImage("assets/images/top_ai_draw_image.png"),
), height: 300,
SafeArea( width: Get.width * 1.2,
child: makeContentView() fit: BoxFit.cover,
), ),
),
SafeArea(
child: makeContentView()
)
], ],
), ),
) )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment