feat:增加用户信息编辑功能

This commit is contained in:
2026-07-05 14:14:43 +08:00
parent 9d3a3f2e80
commit 780526ddf4
18 changed files with 379 additions and 60 deletions

View File

@@ -33,17 +33,26 @@ class _LoginPage extends State<LoginPage> {
@override
void initState() {
super.initState();
_loadRememberState();
_loadRememberState();
}
Future<void> _loadRememberState() async {
bool? isRemember = SPUtil.getBool('isRemember');
if (isRemember) {
setState(() {
_isRemember = true;
_username = SPUtil.getString('username');
_password = SPUtil.getString('password');
});
if (isRemember == true) {
final username = SPUtil.getString('username');
final password = SPUtil.getString('password');
if (username.isNotEmpty && password.isNotEmpty) {
setState(() {
_isRemember = true;
_username = username;
_password = password;
});
WidgetsBinding.instance.addPostFrameCallback((_) {
loginClick();
});
}
}
}

View File

@@ -80,6 +80,7 @@ class _MomentPageState extends State<MomentPage> {
Widget _buildMomentList(FoodProvider provider) {
return ListView.separated(
padding: const EdgeInsets.only(bottom: 80),
controller: _scrollController,
itemCount: provider.momentList.length,
separatorBuilder: (context, index) => SizedBox(height: 8),

View File

@@ -10,8 +10,6 @@ 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 MomentForm extends StatefulWidget {
@@ -152,7 +150,7 @@ class _MomentFormState extends State<MomentForm> {
// 限制选择数量
final filesToUpload = result.files.take(remainingCount).toList();
LoadingDialog.show(context, message: '上传中');
try {
final newImageUrls = <String>[];
// 逐个上传图片
@@ -215,14 +213,12 @@ class _MomentFormState extends State<MomentForm> {
Widget build(BuildContext context) {
final provider = context.watch<FoodProvider>();
return SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: GlassCard(
useOwnLayer: true,
settings: LiquidGlassSettings(
glassColor: Theme.of(context).scaffoldBackgroundColor,
),
return AnimatedPadding(
duration: const Duration(milliseconds: 200),
padding: MediaQuery.of(context).viewInsets,
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16),
child: _buildFormBuilder(provider),
),
),

View File

@@ -33,11 +33,14 @@ class _MomentUserPageState extends State<MomentUserPage> {
}
return Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
title: Text('我的朋友圈', style: const TextStyle(color: Colors.white)),
backgroundColor: Theme.of(context).colorScheme.primary,
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
title: Text('我的朋友圈', style: Theme.of(context).textTheme.titleLarge),
centerTitle: true,
automaticallyImplyLeading: false,
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
icon: const Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context),
),
),

View File

@@ -4,6 +4,7 @@ import 'package:flutter_common/widget/common_widget.dart';
import 'package:flutter_common/widget/dialog_widget.dart';
import 'package:food_hub_app/provider/app_provider.dart';
import 'package:food_hub_app/provider/user_provider.dart';
import 'package:food_hub_app/views/profile_form.dart';
import 'package:food_hub_app/widgets/profile/basic_info.dart';
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
import 'package:liquid_glass_widgets/widgets/containers/glass_card.dart';
@@ -42,11 +43,26 @@ class ProfilePageState extends State<ProfilePage> {
);
}
void _onTapInfo(UserProvider provider) {
provider.initUserForm(provider.currentUser);
showModalBottomSheet(
context: context,
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
isScrollControlled: true,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
),
builder: (context) => ProfileForm(),
);
}
void _onTapMoment() {
Navigator.pushNamed(context, '/momentUser');
}
Widget _buildFunctionButtons(BuildContext context) {
final provider = context.watch<UserProvider>();
final colors = Theme.of(context).colorScheme;
return GlassCard(
@@ -58,10 +74,7 @@ class ProfilePageState extends State<ProfilePage> {
_buildFunctionButton(
icon: Icons.account_circle,
text: '基本资料',
onTap: () {
// 跳转到编辑资料页面
// LogUtils.i('点击编辑资料');
},
onTap: () => _onTapInfo(provider),
),
const Divider(height: 1, thickness: 0.2),
_buildFunctionButton(
@@ -155,8 +168,6 @@ class ProfilePageState extends State<ProfilePage> {
children: [
if (provider.isLoading)
buildLoadingIndicator()
else if (provider.error.isNotEmpty)
Text(provider.error)
else
SingleChildScrollView(
padding: const EdgeInsets.only(bottom: 90),

251
lib/views/profile_form.dart Normal file
View File

@@ -0,0 +1,251 @@
import 'package:flutter/material.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:flutter_input_chips/flutter_input_chips.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:food_hub_app/provider/user_provider.dart';
import 'package:food_hub_app/widgets/common/form.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
class ProfileForm extends StatefulWidget {
const ProfileForm({super.key});
@override
State<ProfileForm> createState() => _ProfileFormState();
}
class _ProfileFormState extends State<ProfileForm> {
final _formKey = GlobalKey<FormBuilderState>();
@override
void initState() {
super.initState();
}
Widget _buildFormBuilder(UserProvider provider) {
final double sizeBoxHeight = 8;
return FormBuilder(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
buildFormLabel(context: context, text: '用户名', isRequired: true),
SizedBox(height: sizeBoxHeight),
FormBuilderTextField(
name: 'username',
initialValue: provider.userFormItem.username,
onChanged: (value) {
provider.updateUserFormItem(username: value);
},
decoration: buildInputDecoration(
context: context,
hintText: '请输入用户名',
),
validator: FormBuilderValidators.required(errorText: '请输入用户名'),
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '性别', isRequired: true),
SizedBox(height: sizeBoxHeight),
FormBuilderDropdown<int>(
name: 'gender',
initialValue: provider.userFormItem.gender,
decoration: buildInputDecoration(
context: context,
hintText: '请选择性别',
),
items: const [
DropdownMenuItem(value: 1, child: Text('')),
DropdownMenuItem(value: 2, child: Text('')),
],
onChanged: (value) {
provider.updateUserFormItem(gender: value);
},
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '手机号', isRequired: true),
SizedBox(height: sizeBoxHeight),
FormBuilderTextField(
name: 'phoneNumber',
initialValue: provider.userFormItem.phoneNumber,
keyboardType: TextInputType.phone,
decoration: buildInputDecoration(
context: context,
hintText: '请输入手机号',
),
onChanged: (value) {
provider.updateUserFormItem(phoneNumber: value);
},
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(errorText: '请输入手机号'),
FormBuilderValidators.match(
RegExp(r'^1[3-9]\d{9}$'),
errorText: '请输入正确的11位手机号',
),
]),
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '邮箱', isRequired: true),
SizedBox(height: sizeBoxHeight),
FormBuilderTextField(
name: 'email',
initialValue: provider.userFormItem.email,
keyboardType: TextInputType.emailAddress,
decoration: buildInputDecoration(
context: context,
hintText: '请输入邮箱',
),
onChanged: (value) {
provider.updateUserFormItem(email: value);
},
validator: FormBuilderValidators.compose([
FormBuilderValidators.email(errorText: '邮箱格式不正确'),
]),
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '出生日期', isRequired: true),
SizedBox(height: sizeBoxHeight),
FormBuilderDateTimePicker(
name: 'birthDate',
initialValue: provider.userFormItem.birthDate,
inputType: InputType.date,
format: DateFormat('yyyy-MM-dd'),
firstDate: DateTime(1900),
lastDate: DateTime.now(),
decoration: buildInputDecoration(
context: context,
hintText: '请选择出生日期',
),
onChanged: (value) {
provider.updateUserFormItem(birthDate: value);
},
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '详细地址', isRequired: false),
SizedBox(height: sizeBoxHeight),
FormBuilderTextField(
name: 'address',
initialValue: provider.userFormItem.address,
decoration: buildInputDecoration(
context: context,
hintText: '请输入详细地址',
),
onChanged: (value) {
provider.updateUserFormItem(address: value);
},
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '职业', isRequired: false),
SizedBox(height: sizeBoxHeight),
FormBuilderTextField(
name: 'job',
initialValue: provider.userFormItem.job,
decoration: buildInputDecoration(
context: context,
hintText: '请输入职业',
),
onChanged: (value) {
provider.updateUserFormItem(job: value);
},
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '标签', isRequired: false),
SizedBox(height: sizeBoxHeight),
FormBuilderField<List<String>>(
name: 'tags',
initialValue: provider.userFormItem.tags,
validator: FormBuilderValidators.minLength(
1,
errorText: '至少添加一个标签',
),
builder: (FormFieldState<List<String>> field) {
return FlutterInputChips(
padding: EdgeInsets.all(0),
initialValue: field.value ?? [],
onChanged: (value) {
provider.updateUserFormItem(tags: value);
},
inputDecoration: buildInputDecoration(
context: context,
hintText: '请输入标签,多个标签用回车键隔开',
),
);
},
),
SizedBox(height: sizeBoxHeight),
buildFormLabel(context: context, text: '简介', isRequired: false),
SizedBox(height: sizeBoxHeight),
FormBuilderTextField(
name: 'description',
initialValue: provider.userFormItem.description,
decoration: buildInputDecoration(
context: context,
hintText: '请输入简介',
),
onChanged: (value) {
provider.updateUserFormItem(description: value);
},
),
SizedBox(height: sizeBoxHeight),
buildFormButtonGroup(
context: context,
isShowDelete: false,
onConfirm: () => _submitForm(provider),
onDelete: () => {},
),
],
),
);
}
/// 提交表单
void _submitForm(UserProvider provider) async {
if (_formKey.currentState?.saveAndValidate() ?? false) {
LoadingDialog.show(context, message: '提交中');
final result = await provider.handleUser();
if (result == true) {
LoadingDialog.hide(context);
showSuccessTip(context, '更新信息成功');
Future.delayed(Duration(milliseconds: 1500), () {
if (mounted) {
Navigator.pop(context);
}
});
} else {
LoadingDialog.hide(context);
showErrorTip(context, '更新信息失败');
}
}
}
@override
Widget build(BuildContext context) {
final provider = context.watch<UserProvider>();
return AnimatedPadding(
duration: const Duration(milliseconds: 200),
padding: MediaQuery.of(context).viewInsets,
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16),
child: _buildFormBuilder(provider),
),
),
);
}
}

View File

@@ -99,8 +99,6 @@ class ProfileUserPageState extends State<ProfileUserPage> {
children: [
if (provider.isLoading)
buildLoadingIndicator()
else if (provider.error.isNotEmpty)
Text(provider.error)
else
_buildContent(provider),
],

View File

@@ -8,8 +8,6 @@ import 'package:food_hub_app/widgets/common/index.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:flutter_common/utils/toast_util.dart';
import 'package:flutter_common/widget/loading_widget.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 RecipeForm extends StatefulWidget {
@@ -551,14 +549,12 @@ class _RecipeFormState extends State<RecipeForm> {
Widget build(BuildContext context) {
final provider = context.watch<FoodProvider>();
return SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: GlassCard(
useOwnLayer: true,
settings: LiquidGlassSettings(
glassColor: Theme.of(context).scaffoldBackgroundColor,
),
return AnimatedPadding(
duration: const Duration(milliseconds: 200),
padding: MediaQuery.of(context).viewInsets,
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
children: [
_buildStepIndicator(),

View File

@@ -88,9 +88,6 @@ class _RecordPageState extends State<RecordPage> {
showModalBottomSheet(
context: context,
constraints: BoxConstraints(
minHeight: 600,
),
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
isScrollControlled: true,
shape: const RoundedRectangleBorder(

View File

@@ -11,7 +11,6 @@ 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:intl/intl.dart';
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
import 'package:provider/provider.dart';
@@ -333,14 +332,12 @@ class _RecordFormState extends State<RecordForm> {
Widget build(BuildContext context) {
final provider = context.watch<FoodProvider>();
return SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: GlassCard(
useOwnLayer: true,
settings: LiquidGlassSettings(
glassColor: Theme.of(context).scaffoldBackgroundColor,
),
return AnimatedPadding(
duration: const Duration(milliseconds: 200),
padding: MediaQuery.of(context).viewInsets,
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16),
child: _buildFormBuilder(provider),
),
),