feat:增加朋友圈表单功能
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/utils/sp_utils.dart';
|
||||
import 'package:flutter_common/utils/toast_util.dart';
|
||||
import 'package:flutter_common/widget/loading_widget.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
import 'package:food_hub_app/apis/session.dart';
|
||||
import 'package:food_hub_app/models/session.dart';
|
||||
@@ -70,12 +71,20 @@ class _LoginPage extends State<LoginPage> {
|
||||
// 表单校验通过才会继续执行
|
||||
if ((_formKey.currentState as FormState).validate()) {
|
||||
(_formKey.currentState as FormState).save();
|
||||
Session session = await loginApi(_username, _password);
|
||||
ToastUtil.success('登录成功');
|
||||
|
||||
handleRememberState();
|
||||
handleTokenState(session.saToken.tokenValue);
|
||||
Navigator.pushNamed(context, '/home');
|
||||
try {
|
||||
LoadingDialog.show(context, message: '登录中');
|
||||
Session session = await loginApi(_username, _password);
|
||||
ToastUtil.success('登录成功');
|
||||
handleRememberState();
|
||||
handleTokenState(session.saToken.tokenValue);
|
||||
|
||||
LoadingDialog.hide(context);
|
||||
Navigator.pushNamed(context, '/home');
|
||||
} catch (e) {
|
||||
LoadingDialog.hide(context);
|
||||
ToastUtil.error(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
|
||||
@@ -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)),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -294,6 +294,13 @@ class _RecordFormPageState extends State<RecordFormPage> {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
LoadingDialog.hide(context);
|
||||
if (provider.isEditing) {
|
||||
showErrorTip(context, '更新记录失败');
|
||||
} else {
|
||||
showErrorTip(context, '新增记录失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/widget/chart.dart';
|
||||
import 'package:flutter_common/widget/common_widget.dart';
|
||||
import 'package:food_hub_app/provider/food_provider.dart';
|
||||
import 'package:food_hub_app/widgets/stats/card.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class StatsPage extends StatefulWidget {
|
||||
@@ -35,32 +34,28 @@ class _StatsPage extends State<StatsPage> {
|
||||
mainAxisSpacing: 8,
|
||||
childAspectRatio: 2,
|
||||
children: [
|
||||
StatisticCard(
|
||||
StatsCard(
|
||||
icon: Icons.restaurant_menu,
|
||||
color: Colors.blue,
|
||||
title: '菜谱总数',
|
||||
value: provider.summaryStats.recipeCount,
|
||||
value: provider.summaryStats.recipeCount.toString(),
|
||||
unit: '个',
|
||||
),
|
||||
StatisticCard(
|
||||
StatsCard(
|
||||
icon: Icons.grid_view_rounded,
|
||||
color: Colors.green,
|
||||
title: '菜谱类别',
|
||||
value: provider.summaryStats.categoryCount,
|
||||
value: provider.summaryStats.categoryCount.toString(),
|
||||
unit: '种',
|
||||
),
|
||||
StatisticCard(
|
||||
StatsCard(
|
||||
icon: Icons.flag,
|
||||
color: Colors.orange,
|
||||
title: '做菜次数',
|
||||
value: provider.summaryStats.workCount,
|
||||
value: provider.summaryStats.workCount.toString(),
|
||||
unit: '个',
|
||||
),
|
||||
StatisticCard(
|
||||
StatsCard(
|
||||
icon: Icons.show_chart,
|
||||
color: Colors.red,
|
||||
title: '平均次数',
|
||||
value: double.parse(provider.averageRecordCount.toStringAsFixed(2)),
|
||||
value: provider.averageRecordCount.toStringAsFixed(2),
|
||||
unit: '次/月',
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user