Files
food_hub_app/lib/views/moment_form.dart

152 lines
4.5 KiB
Dart

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.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 {
const MomentFormPage({super.key});
@override
State<MomentFormPage> createState() => _MomentFormPageState();
}
class _MomentFormPageState extends State<MomentFormPage> {
final _formKey = GlobalKey<FormBuilderState>();
final _focusNode = FocusNode();
static const String _contentField = 'content';
Widget _buildFormBuilder() {
final provider = context.watch<FoodProvider>();
return FormBuilder(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
buildFormLabel('内容', required: true),
const SizedBox(height: 8),
FormBuilderTextField(
name: _contentField,
focusNode: _focusNode,
onChanged: (value) {
setState(() {
// provider.momentFormItem.content = value!;
});
},
decoration: buildInputDecoration(
context: context,
hintText: '请输入朋友圈内容',
prefixIcon: const Icon(
Icons.title,
size: 20,
color: Color(0xFF86909C),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return '请输入菜谱名称';
}
return null;
},
),
const SizedBox(height: 10),
buildFormLabel('上传图片', required: true),
const SizedBox(height: 10),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (provider.recordFormItem.imageUrl.isEmpty)
buildImageUploadButton(onPickImage: () => _pickImage(provider))
else
buildImagePreviewItem(
context: context,
imageUrls: [provider.recordFormItem.imageUrl],
index: 0,
onRemoveImage: () => _removeImage(provider),
),
],
),
const SizedBox(height: 10),
buildFormButtonGroup(
context: context,
onConfirm: () => _submitForm(provider),
),
],
),
);
}
/// 选择图片(限制单张)
Future<void> _pickImage(FoodProvider provider) async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['jpg', 'jpeg', 'png'],
allowMultiple: false,
);
if (result != null) {
PlatformFile file = result.files.first;
final fileName = await MinIOHelper().uploadFile(file: file);
setState(() {
provider.recordFormItem.imageUrl = fileName;
});
}
}
/// 移除图片
void _removeImage(FoodProvider provider) {
setState(() {
provider.recordFormItem.imageUrl = '';
});
}
/// 提交表单
void _submitForm(FoodProvider provider) {
if (_formKey.currentState?.saveAndValidate() ?? false) {
if (provider.recordFormItem.imageUrl.isEmpty) {
showErrorToast('请上传图片');
return;
}
print(provider.recordFormItem.name);
print(provider.recordFormItem.date);
print(provider.recordFormItem.imageUrl);
}
}
@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: TextStyle(color: Colors.white)),
backgroundColor: colors.primary,
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
),
backgroundColor: const Color(0xFFF5F5F5),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(5),
child: buildCard(context: context, child: _buildFormBuilder()),
),
),
);
}
}