feat:增加朋友圈表单功能
This commit is contained in:
@@ -4,6 +4,8 @@ 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_common/widget/dialog_widget.dart';
|
||||
import 'package:flutter_common/widget/loading_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';
|
||||
@@ -48,15 +50,9 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
);
|
||||
},
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
provider.momentFormItem.content = value!;
|
||||
});
|
||||
provider.updateMomentFormItem(content: value);
|
||||
},
|
||||
decoration: buildInputDecoration(
|
||||
context: context,
|
||||
hintText: '请输入朋友圈内容',
|
||||
prefixIcon: Icons.article,
|
||||
),
|
||||
decoration: buildInputDecoration(context: context, hintText: '请输入朋友圈内容'),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '请输入朋友圈内容';
|
||||
@@ -81,7 +77,7 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
crossAxisCount: 3,
|
||||
crossAxisSpacing: 8,
|
||||
mainAxisSpacing: 8,
|
||||
childAspectRatio: 1,
|
||||
childAspectRatio: 4/3,
|
||||
),
|
||||
itemCount: totalItems,
|
||||
itemBuilder: (context, index) {
|
||||
@@ -103,9 +99,7 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFormBuilder() {
|
||||
final provider = context.watch<FoodProvider>();
|
||||
|
||||
Widget _buildFormBuilder(FoodProvider provider) {
|
||||
return FormBuilder(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
@@ -116,7 +110,7 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
_buildContentField(provider),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
buildFormLabel(context: context, text: '上传图片', isRequired: true),
|
||||
buildFormLabel(context: context, text: '上传图片', isRequired: false),
|
||||
const SizedBox(height: 8),
|
||||
_buildImageField(provider),
|
||||
const SizedBox(height: 8),
|
||||
@@ -152,47 +146,59 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
// 限制选择数量
|
||||
final filesToUpload = result.files.take(remainingCount).toList();
|
||||
|
||||
// 显示加载提示
|
||||
// showLoadingToast('正在上传图片...');
|
||||
LoadingDialog.show(context, message: '上传中');
|
||||
|
||||
try {
|
||||
final newImageUrls = <String>[];
|
||||
final newImageUrls = <String>[];
|
||||
|
||||
// 逐个上传图片
|
||||
for (final file in filesToUpload) {
|
||||
final fileName = await MinIOHelper().uploadFile(
|
||||
bucketName: AppConfig.bucketName,
|
||||
file: file,
|
||||
);
|
||||
logger.i('图片名称:$fileName');
|
||||
newImageUrls.add(fileName);
|
||||
}
|
||||
|
||||
// 更新图片列表
|
||||
setState(() {
|
||||
provider.momentFormItem.imageList.addAll(newImageUrls);
|
||||
});
|
||||
|
||||
ToastUtil.success('图片上传成功');
|
||||
} catch (e) {
|
||||
ToastUtil.error('图片上传失败');
|
||||
// 逐个上传图片
|
||||
for (final file in filesToUpload) {
|
||||
final fileName = await MinIOHelper().uploadFile(
|
||||
bucketName: AppConfig.bucketName,
|
||||
file: file,
|
||||
);
|
||||
logger.i('图片名称:$fileName');
|
||||
newImageUrls.add(fileName);
|
||||
}
|
||||
|
||||
// 更新图片列表
|
||||
LoadingDialog.hide(context);
|
||||
provider.addMomentFormImage(newImageUrls);
|
||||
}
|
||||
}
|
||||
|
||||
/// 移除单张图片
|
||||
void _removeImage(FoodProvider provider, int index) {
|
||||
setState(() {
|
||||
provider.momentFormItem.imageList.removeAt(index);
|
||||
});
|
||||
provider.removeMomentFormImage(index);
|
||||
}
|
||||
|
||||
/// 提交表单
|
||||
void _submitForm(FoodProvider provider) {
|
||||
void _submitForm(FoodProvider provider) async{
|
||||
if (_formKey.currentState?.saveAndValidate() ?? false) {
|
||||
// 这里处理表单提交逻辑
|
||||
// print('提交内容: ${_formKey.currentState?.value[_contentField]}');
|
||||
// print('图片列表: ${provider.recordFormItem.imageUrls}');
|
||||
LoadingDialog.show(context, message: '提交中');
|
||||
final result = await provider.handleMoment();
|
||||
if (result == true) {
|
||||
await provider.refreshMomentList();
|
||||
LoadingDialog.hide(context);
|
||||
|
||||
if (provider.isEditing) {
|
||||
showSuccessTip(context, '更新朋友圈成功');
|
||||
} else {
|
||||
showSuccessTip(context, '新增朋友圈成功');
|
||||
}
|
||||
|
||||
Future.delayed(Duration(milliseconds: 1500), () {
|
||||
if (mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
LoadingDialog.hide(context);
|
||||
if (provider.isEditing) {
|
||||
showErrorTip(context, '更新朋友圈失败');
|
||||
} else {
|
||||
showErrorTip(context, '新增朋友圈失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,7 +222,7 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
body: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: CommonCard(child: _buildFormBuilder()),
|
||||
child: CommonCard(child: _buildFormBuilder(provider)),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user