feat:重构模块
This commit is contained in:
@@ -1,26 +1,27 @@
|
||||
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_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';
|
||||
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:liquid_glass_widgets/liquid_glass_widgets.dart';
|
||||
import 'package:liquid_glass_widgets/widgets/containers/glass_card.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class MomentFormPage extends StatefulWidget {
|
||||
const MomentFormPage({super.key});
|
||||
class MomentForm extends StatefulWidget {
|
||||
const MomentForm({super.key});
|
||||
|
||||
@override
|
||||
State<MomentFormPage> createState() => _MomentFormPageState();
|
||||
State<MomentForm> createState() => _MomentFormState();
|
||||
}
|
||||
|
||||
class _MomentFormPageState extends State<MomentFormPage> {
|
||||
class _MomentFormState extends State<MomentForm> {
|
||||
final _formKey = GlobalKey<FormBuilderState>();
|
||||
final _focusNode = FocusNode();
|
||||
static const String _contentField = 'content';
|
||||
@@ -34,8 +35,8 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
name: _contentField,
|
||||
initialValue: provider.momentFormItem.content,
|
||||
focusNode: _focusNode,
|
||||
maxLines: 5,
|
||||
minLines: 3,
|
||||
maxLines: 8,
|
||||
minLines: 5,
|
||||
maxLength: maxContentLength,
|
||||
buildCounter: (
|
||||
context, {
|
||||
@@ -75,10 +76,10 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3,
|
||||
crossAxisCount: 2,
|
||||
crossAxisSpacing: 8,
|
||||
mainAxisSpacing: 8,
|
||||
childAspectRatio: 4/3,
|
||||
childAspectRatio: 4 / 3,
|
||||
),
|
||||
itemCount: totalItems,
|
||||
itemBuilder: (context, index) {
|
||||
@@ -111,7 +112,11 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
_buildContentField(provider),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
buildFormLabel(context: context, text: '上传图片(最多9张)', isRequired: false),
|
||||
buildFormLabel(
|
||||
context: context,
|
||||
text: '上传图片(最多9张)',
|
||||
isRequired: false,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_buildImageField(provider),
|
||||
const SizedBox(height: 8),
|
||||
@@ -146,24 +151,27 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
if (result != null) {
|
||||
// 限制选择数量
|
||||
final filesToUpload = result.files.take(remainingCount).toList();
|
||||
|
||||
LoadingDialog.show(context, message: '上传中');
|
||||
|
||||
try {
|
||||
final newImageUrls = <String>[];
|
||||
// 逐个上传图片
|
||||
for (final file in filesToUpload) {
|
||||
final fileName = await MinIOHelper().uploadFile(
|
||||
bucketName: AppConfig.bucketName,
|
||||
file: file,
|
||||
);
|
||||
logger.i('图片名称:$fileName');
|
||||
newImageUrls.add(fileName);
|
||||
}
|
||||
|
||||
final newImageUrls = <String>[];
|
||||
|
||||
// 逐个上传图片
|
||||
for (final file in filesToUpload) {
|
||||
final fileName = await MinIOHelper().uploadFile(
|
||||
bucketName: AppConfig.bucketName,
|
||||
file: file,
|
||||
);
|
||||
logger.i('图片名称:$fileName');
|
||||
newImageUrls.add(fileName);
|
||||
// 更新图片列表
|
||||
provider.addMomentFormImage(newImageUrls);
|
||||
} catch (e) {
|
||||
ToastUtil.error(e.toString());
|
||||
} finally {
|
||||
LoadingDialog.hide(context);
|
||||
}
|
||||
|
||||
// 更新图片列表
|
||||
LoadingDialog.hide(context);
|
||||
provider.addMomentFormImage(newImageUrls);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,7 +181,7 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
}
|
||||
|
||||
/// 提交表单
|
||||
void _submitForm(FoodProvider provider) async{
|
||||
void _submitForm(FoodProvider provider) async {
|
||||
if (_formKey.currentState?.saveAndValidate() ?? false) {
|
||||
LoadingDialog.show(context, message: '提交中');
|
||||
final result = await provider.handleMoment();
|
||||
@@ -205,24 +213,17 @@ class _MomentFormPageState extends State<MomentFormPage> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final provider = context.watch<FoodProvider>();
|
||||
|
||||
final String title = provider.isEditing ? '编辑朋友圈' : '新增朋友圈';
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(title, style: const TextStyle(color: Colors.white)),
|
||||
backgroundColor: colors.primary,
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: CommonCard(child: _buildFormBuilder(provider)),
|
||||
return SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: GlassCard(
|
||||
useOwnLayer: true,
|
||||
settings: LiquidGlassSettings(
|
||||
glassColor: Theme.of(context).scaffoldBackgroundColor,
|
||||
),
|
||||
child: _buildFormBuilder(provider),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user