comment_input_bar_widget.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import 'dart:io';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_riverpod/flutter_riverpod.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:image_picker/image_picker.dart';
  7. import 'package:news_app/constant/color_res.dart';
  8. import 'package:news_app/constant/size_res.dart';
  9. import 'package:news_app/http/http_util.dart';
  10. import 'package:news_app/widget/my_txt.dart';
  11. import '../../widget/auth_gesture_detector.dart';
  12. /// @author: bo.zeng
  13. /// @email: cnhbwds@gmail.com
  14. /// @date: 2025 2025/4/17 12:22
  15. /// @description:
  16. /// 图片上传状态
  17. enum UploadStatus {
  18. uploading, // 上传中
  19. success, // 上传成功
  20. failed, // 上传失败
  21. }
  22. /// 图片数据模型
  23. class ImageItem {
  24. final String localPath; // 本地路径
  25. String? remoteUrl; // 远程URL
  26. UploadStatus status; // 上传状态
  27. ImageItem({
  28. required this.localPath,
  29. this.remoteUrl,
  30. this.status = UploadStatus.uploading,
  31. });
  32. ImageItem copyWith({
  33. String? localPath,
  34. String? remoteUrl,
  35. UploadStatus? status,
  36. }) {
  37. return ImageItem(
  38. localPath: localPath ?? this.localPath,
  39. remoteUrl: remoteUrl ?? this.remoteUrl,
  40. status: status ?? this.status,
  41. );
  42. }
  43. }
  44. class CommentInputBarWidget extends ConsumerStatefulWidget {
  45. final FocusNode focusNode;
  46. final String id;
  47. final Function(String, List<String>)? onSend; // 改为传递URL列表
  48. const CommentInputBarWidget(
  49. this.focusNode,
  50. this.onSend,
  51. this.id, {
  52. super.key,
  53. });
  54. @override
  55. ConsumerState<ConsumerStatefulWidget> createState() =>
  56. _CommentInputBarState();
  57. }
  58. class _CommentInputBarState extends ConsumerState<CommentInputBarWidget> {
  59. final TextEditingController _controller = TextEditingController();
  60. final List<ImageItem> _imageItems = [];
  61. static const int maxImageCount = 9;
  62. Future<void> _pickImage() async {
  63. final List<XFile> pickedFiles = await ImagePicker().pickMultiImage(
  64. imageQuality: 80,
  65. );
  66. if (pickedFiles.isNotEmpty) {
  67. // 计算还能添加多少张图片
  68. final remainingSlots = maxImageCount - _imageItems.length;
  69. if (remainingSlots <= 0) {
  70. // 已达到最大数量,不添加
  71. return;
  72. }
  73. // 添加新图片,最多添加到9张
  74. final imagesToAdd = pickedFiles.take(remainingSlots).toList();
  75. setState(() {
  76. for (var file in imagesToAdd) {
  77. _imageItems.add(ImageItem(localPath: file.path));
  78. }
  79. });
  80. // 立即上传图片
  81. _uploadImages(imagesToAdd.map((e) => e.path).toList());
  82. }
  83. }
  84. /// 上传图片
  85. Future<void> _uploadImages(List<String> filePaths) async {
  86. // 批量上传接口
  87. final urls = await HttpUtil().uploadCommentImages(filePaths);
  88. setState(() {
  89. // 更新上传状态
  90. int urlIndex = 0;
  91. for (int i = 0; i < _imageItems.length; i++) {
  92. if (_imageItems[i].status == UploadStatus.uploading &&
  93. filePaths.contains(_imageItems[i].localPath)) {
  94. if (urlIndex < urls.length) {
  95. _imageItems[i] = _imageItems[i].copyWith(
  96. remoteUrl: urls[urlIndex],
  97. status: UploadStatus.success,
  98. );
  99. urlIndex++;
  100. } else {
  101. _imageItems[i] = _imageItems[i].copyWith(
  102. status: UploadStatus.failed,
  103. );
  104. }
  105. }
  106. }
  107. });
  108. }
  109. /// 重试上传失败的图片
  110. Future<void> _retryUpload(int index) async {
  111. final item = _imageItems[index];
  112. setState(() {
  113. _imageItems[index] = item.copyWith(status: UploadStatus.uploading);
  114. });
  115. final url = await HttpUtil().uploadCommentImage(item.localPath);
  116. setState(() {
  117. if (url != null) {
  118. _imageItems[index] = item.copyWith(
  119. remoteUrl: url,
  120. status: UploadStatus.success,
  121. );
  122. } else {
  123. _imageItems[index] = item.copyWith(status: UploadStatus.failed);
  124. }
  125. });
  126. }
  127. void _removeImage(int index) {
  128. setState(() {
  129. _imageItems.removeAt(index);
  130. });
  131. }
  132. void _clearImages() {
  133. setState(() {
  134. _imageItems.clear();
  135. });
  136. }
  137. @override
  138. Widget build(BuildContext context) {
  139. double size = MediaQuery.of(context).padding.bottom;
  140. return Container(
  141. decoration: BoxDecoration(
  142. color: Colors.white,
  143. border: Border(top: BorderSide(color: colorE5E5E5, width: 0.5)),
  144. ),
  145. padding: EdgeInsets.only(
  146. top: horizontalPadding,
  147. bottom: size > 0 ? size : horizontalPadding,
  148. ),
  149. child: Column(
  150. crossAxisAlignment: CrossAxisAlignment.end,
  151. mainAxisSize: MainAxisSize.min,
  152. children: [
  153. // 图片预览区域 - 支持多张图片
  154. if (_imageItems.isNotEmpty)
  155. Container(
  156. height: 60.h,
  157. margin: EdgeInsets.only(bottom: 10.w),
  158. child: SingleChildScrollView(
  159. scrollDirection: Axis.horizontal,
  160. child: Row(
  161. children: List.generate(_imageItems.length, (index) {
  162. final item = _imageItems[index];
  163. return Container(
  164. width: 60.w,
  165. height: 60.h,
  166. margin: EdgeInsets.only(right: 8.w),
  167. child: Stack(
  168. alignment: Alignment.topRight,
  169. children: [
  170. // 显示图片(本地或远程)
  171. ClipRRect(
  172. borderRadius: BorderRadius.circular(4.r),
  173. child: item.status == UploadStatus.success && item.remoteUrl != null
  174. ? CachedNetworkImage(
  175. imageUrl: item.remoteUrl!,
  176. width: 60.w,
  177. height: 60.h,
  178. fit: BoxFit.cover,
  179. placeholder: (context, url) => Image.file(
  180. File(item.localPath),
  181. width: 60.w,
  182. height: 60.h,
  183. fit: BoxFit.cover,
  184. ),
  185. errorWidget: (context, url, error) => Image.file(
  186. File(item.localPath),
  187. width: 60.w,
  188. height: 60.h,
  189. fit: BoxFit.cover,
  190. ),
  191. )
  192. : Image.file(
  193. File(item.localPath),
  194. width: 60.w,
  195. height: 60.h,
  196. fit: BoxFit.cover,
  197. ),
  198. ),
  199. // 上传中遮罩
  200. if (item.status == UploadStatus.uploading)
  201. Container(
  202. width: 60.w,
  203. height: 60.h,
  204. decoration: BoxDecoration(
  205. color: Colors.black26,
  206. borderRadius: BorderRadius.circular(4.r),
  207. ),
  208. child: Center(
  209. child: SizedBox(
  210. width: 20.w,
  211. height: 20.w,
  212. child: CircularProgressIndicator(
  213. strokeWidth: 2,
  214. valueColor:
  215. AlwaysStoppedAnimation<Color>(Colors.white),
  216. ),
  217. ),
  218. ),
  219. ),
  220. // 上传失败标记
  221. if (item.status == UploadStatus.failed)
  222. Container(
  223. width: 60.w,
  224. height: 60.h,
  225. decoration: BoxDecoration(
  226. color: Colors.black26,
  227. borderRadius: BorderRadius.circular(4.r),
  228. ),
  229. child: Center(
  230. child: GestureDetector(
  231. onTap: () => _retryUpload(index),
  232. child: Icon(
  233. Icons.refresh,
  234. color: Colors.white,
  235. size: 20.w,
  236. ),
  237. ),
  238. ),
  239. ),
  240. // 删除按钮
  241. GestureDetector(
  242. onTap: () => _removeImage(index),
  243. child: Container(
  244. margin: EdgeInsets.all(2.w),
  245. width: 16.w,
  246. height: 16.w,
  247. decoration: BoxDecoration(
  248. color: Colors.black54,
  249. shape: BoxShape.circle,
  250. ),
  251. child: Icon(
  252. Icons.close,
  253. color: Colors.white,
  254. size: 10.w,
  255. ),
  256. ),
  257. ),
  258. ],
  259. ),
  260. );
  261. }),
  262. ),
  263. ),
  264. ),
  265. Row(
  266. children: [
  267. Expanded(
  268. child: Container(
  269. height: 40.h,
  270. margin: EdgeInsets.only(right: 10.w),
  271. child: TextField(
  272. decoration: InputDecoration(
  273. fillColor: colorF5F7FA,
  274. filled: true,
  275. border: OutlineInputBorder(
  276. borderRadius: BorderRadius.circular(5.r),
  277. borderSide: BorderSide.none,
  278. ),
  279. contentPadding: EdgeInsets.symmetric(
  280. horizontal: 10.w,
  281. vertical: 20.h,
  282. ),
  283. hintText: '快来写下你的评论吧~',
  284. hintStyle: TextStyle(color: color7788A0, fontSize: 12.sp),
  285. ),
  286. focusNode: widget.focusNode,
  287. controller: _controller,
  288. ),
  289. ),
  290. ),
  291. // 图片上传按钮
  292. GestureDetector(
  293. onTap: _pickImage,
  294. child: Container(
  295. width: 40.w,
  296. height: 40.h,
  297. margin: EdgeInsets.only(right: 8.w),
  298. alignment: Alignment.center,
  299. child: Icon(
  300. Icons.image,
  301. color: _imageItems.isNotEmpty ? color188FFF : color7788A0,
  302. size: 24.w,
  303. ),
  304. ),
  305. ),
  306. AuthGestureDetector(
  307. onTap: () {
  308. // 获取所有上传成功的图片URL
  309. final successUrls = _imageItems
  310. .where((item) =>
  311. item.status == UploadStatus.success && item.remoteUrl != null)
  312. .map((item) => item.remoteUrl!)
  313. .toList();
  314. widget.onSend?.call(_controller.text, successUrls);
  315. _controller.clear();
  316. _clearImages();
  317. },
  318. child: Container(
  319. width: 50.w,
  320. padding: EdgeInsets.symmetric(vertical: 5.h),
  321. alignment: Alignment.center,
  322. decoration: BoxDecoration(
  323. color: color188FFF,
  324. borderRadius: BorderRadius.circular(5.r),
  325. ),
  326. child: myTxt(text: "发送", color: Colors.white, fontSize: 12.sp),
  327. ),
  328. ),
  329. ],
  330. ),
  331. ],
  332. ),
  333. );
  334. }
  335. }