feat:更新美食记录表单
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/utils/log_utils.dart';
|
||||
import 'package:flutter_common/utils/minio_utils.dart';
|
||||
import 'package:flutter_common/utils/toast_util.dart';
|
||||
import 'package:flutter_common/widget/common_widget.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
import 'package:food_hub_app/config/app_config.dart';
|
||||
import 'package:food_hub_app/provider/food_provider.dart';
|
||||
import 'package:food_hub_app/utils/minio_utils.dart';
|
||||
import 'package:food_hub_app/widgets/common/form.dart';
|
||||
import 'package:food_hub_app/widgets/common/image.dart';
|
||||
import 'package:food_hub_app/widgets/common/index.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class MomentFormPage extends StatefulWidget {
|
||||
@@ -22,7 +25,9 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
static const int maxContentLength = 200;
|
||||
static const int maxImageCount = 9; // 最大图片数量
|
||||
|
||||
Widget _buildContentField() {
|
||||
Widget _buildContentField(FoodProvider provider) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return FormBuilderTextField(
|
||||
name: _contentField,
|
||||
focusNode: _focusNode,
|
||||
@@ -38,16 +43,13 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
return Text(
|
||||
'$currentLength/$maxContentLength',
|
||||
style: TextStyle(
|
||||
color:
|
||||
currentLength > maxLength!
|
||||
? Colors.red
|
||||
: Theme.of(context).colorScheme.primary,
|
||||
color: currentLength > maxLength! ? Colors.red : colors.primary,
|
||||
),
|
||||
);
|
||||
},
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
// provider.momentFormItem.content = value!;
|
||||
provider.momentFormItem.content = value!;
|
||||
});
|
||||
},
|
||||
decoration: buildInputDecoration(context: context, hintText: '请输入朋友圈内容'),
|
||||
@@ -56,39 +58,41 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
return '请输入朋友圈内容';
|
||||
}
|
||||
if (value.length > maxContentLength) {
|
||||
// 修正为使用 maxContentLength
|
||||
return '内容不能超过${maxContentLength}字';
|
||||
return '内容不能超过$maxContentLength字';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildImageGrid(FoodProvider provider) {
|
||||
Widget _buildImageField(FoodProvider provider) {
|
||||
final imageUrls = provider.momentFormItem.imageList;
|
||||
final totalItems =
|
||||
imageUrls.length + (imageUrls.length < maxImageCount ? 1 : 0);
|
||||
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
...imageUrls.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final imageUrl = entry.value;
|
||||
return Stack(
|
||||
children: [
|
||||
buildImagePreviewItem(
|
||||
context: context,
|
||||
imageUrls: imageUrls,
|
||||
index: index,
|
||||
onRemoveImage: () => _removeImage(provider, index),
|
||||
)
|
||||
],
|
||||
return GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3,
|
||||
crossAxisSpacing: 8,
|
||||
mainAxisSpacing: 8,
|
||||
childAspectRatio: 1,
|
||||
),
|
||||
itemCount: totalItems,
|
||||
itemBuilder: (context, index) {
|
||||
if (index < imageUrls.length) {
|
||||
return ImagePreview(
|
||||
imageUrls: imageUrls,
|
||||
index: index,
|
||||
onRemoveImage: () => _removeImage(provider, index),
|
||||
);
|
||||
}),
|
||||
// 上传按钮(如果还有剩余位置)
|
||||
if (imageUrls.length < maxImageCount)
|
||||
buildImageUploadButton(onPickImage: () => _pickImages(provider)),
|
||||
],
|
||||
}
|
||||
// 上传按钮(最后一个)
|
||||
else {
|
||||
return buildImageUploadButton(onTap: () => _pickImages(provider));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -102,14 +106,14 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
children: [
|
||||
buildFormLabel('朋友圈内容', required: true),
|
||||
const SizedBox(height: 8),
|
||||
_buildContentField(),
|
||||
const SizedBox(height: 20),
|
||||
_buildContentField(provider),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
buildFormLabel('上传图片', required: true),
|
||||
const SizedBox(height: 10),
|
||||
_buildImageGrid(provider),
|
||||
|
||||
const SizedBox(height: 20),
|
||||
const SizedBox(height: 8),
|
||||
_buildImageField(provider),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
buildFormButtonGroup(
|
||||
context: context,
|
||||
onConfirm: () => _submitForm(provider),
|
||||
@@ -125,7 +129,7 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
final remainingCount = maxImageCount - currentCount;
|
||||
|
||||
if (remainingCount <= 0) {
|
||||
showErrorToast('最多只能上传$maxImageCount张图片');
|
||||
ToastUtil.error('最多只能上传$maxImageCount张图片');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -147,7 +151,11 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
|
||||
// 逐个上传图片
|
||||
for (final file in filesToUpload) {
|
||||
final fileName = await MinIOHelper().uploadFile(file: file);
|
||||
final fileName = await MinIOHelper().uploadFile(
|
||||
bucketName: AppConfig.bucketName,
|
||||
file: file,
|
||||
);
|
||||
logger.i('图片名称:$fileName');
|
||||
newImageUrls.add(fileName);
|
||||
}
|
||||
|
||||
@@ -156,9 +164,9 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
provider.momentFormItem.imageList.addAll(newImageUrls);
|
||||
});
|
||||
|
||||
showSuccessToast('图片上传成功');
|
||||
ToastUtil.success('图片上传成功');
|
||||
} catch (e) {
|
||||
showErrorToast('图片上传失败');
|
||||
ToastUtil.error('图片上传失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -199,7 +207,7 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
body: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: BuildCard(child: _buildFormBuilder()),
|
||||
child: CommonCard(child: _buildFormBuilder()),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user