comment_input_bar_widget.dart 13 KB

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