D1APP-42 edit profile (dynamic)
This commit is contained in:
parent
76ee41c3f8
commit
b3605b008c
|
|
@ -1,59 +1,169 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:didvan/models/view/app_bar_data.dart';
|
import 'package:didvan/models/view/app_bar_data.dart';
|
||||||
import 'package:didvan/pages/home/profile/edit_profile/widgets/profile_photo.dart';
|
import 'package:didvan/pages/home/profile/edit_profile/widgets/profile_photo.dart';
|
||||||
import 'package:didvan/pages/home/profile/edit_profile/widgets/switch.dart';
|
|
||||||
import 'package:didvan/pages/home/profile/widgets/menu_item.dart';
|
import 'package:didvan/pages/home/profile/widgets/menu_item.dart';
|
||||||
|
import 'package:didvan/providers/user_provider.dart';
|
||||||
|
import 'package:didvan/routes/routes.dart';
|
||||||
|
import 'package:didvan/widgets/didvan/button.dart';
|
||||||
import 'package:didvan/widgets/didvan/card.dart';
|
import 'package:didvan/widgets/didvan/card.dart';
|
||||||
import 'package:didvan/widgets/didvan/divider.dart';
|
import 'package:didvan/widgets/didvan/divider.dart';
|
||||||
import 'package:didvan/widgets/didvan/scaffold.dart';
|
import 'package:didvan/widgets/didvan/scaffold.dart';
|
||||||
import 'package:didvan/widgets/didvan/text_field.dart';
|
import 'package:didvan/widgets/didvan/text_field.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class EditProfile extends StatelessWidget {
|
class EditProfile extends StatefulWidget {
|
||||||
const EditProfile({Key? key}) : super(key: key);
|
const EditProfile({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<EditProfile> createState() => _EditProfileState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _EditProfileState extends State<EditProfile> {
|
||||||
|
Timer? _timer;
|
||||||
|
|
||||||
|
late String fullName;
|
||||||
|
String? username;
|
||||||
|
String? email;
|
||||||
|
|
||||||
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
final user = context.read<UserProvider>().user;
|
||||||
|
fullName = user.fullName;
|
||||||
|
username = user.username;
|
||||||
|
email = user.email;
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return DidvanScaffold(
|
return Consumer<UserProvider>(
|
||||||
|
builder: (context, state, child) => Material(
|
||||||
|
child: Stack(
|
||||||
|
children: [
|
||||||
|
Form(
|
||||||
|
key: _formKey,
|
||||||
|
child: DidvanScaffold(
|
||||||
|
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 92),
|
||||||
appBarData: AppBarData(title: 'ویرایش پروفایل'),
|
appBarData: AppBarData(title: 'ویرایش پروفایل'),
|
||||||
children: [
|
children: [
|
||||||
const SizedBox(height: 16),
|
|
||||||
const ProfilePhoto(),
|
const ProfilePhoto(),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
DidvanCard(
|
DidvanCard(
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
|
DidvanTextField(
|
||||||
|
title: 'نام و نام خانوادگی',
|
||||||
|
hintText: 'نام و نام خانوادگی',
|
||||||
|
initialValue: state.user.fullName,
|
||||||
|
onChanged: (value) {
|
||||||
|
_setButtonState();
|
||||||
|
fullName = value;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
DidvanTextField(
|
||||||
|
title: 'موبایل',
|
||||||
|
enabled: false,
|
||||||
|
hintText: state.user.phoneNumber,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
DidvanTextField(
|
DidvanTextField(
|
||||||
title: 'نام کاربری',
|
title: 'نام کاربری',
|
||||||
hintText: 'انتخاب نام کاربری (اختیاری)',
|
hintText: 'انتخاب نام کاربری (اختیاری)',
|
||||||
onChanged: (value) {},
|
onChanged: _onUsernameChanged,
|
||||||
|
initialValue: state.user.username,
|
||||||
|
validator: (value) async {
|
||||||
|
final result = await state.checkUsername(value);
|
||||||
|
if (result == false) {
|
||||||
|
return 'نام کاربری در دسترس نمیباشد';
|
||||||
|
}
|
||||||
|
},
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
DidvanTextField(
|
DidvanTextField(
|
||||||
title: 'ایمیل',
|
title: 'ایمیل',
|
||||||
hintText: 'مثال: example@email.com',
|
hintText: 'مثال: example@email.com',
|
||||||
onChanged: (value) {},
|
onChanged: (value) {
|
||||||
),
|
_setButtonState();
|
||||||
const SizedBox(height: 16),
|
email = value;
|
||||||
const DidvanTextField(
|
},
|
||||||
title: 'موبایل',
|
initialValue: state.user.email,
|
||||||
enabled: false,
|
validator: (value) async {
|
||||||
hintText: '09123456789',
|
bool emailValid = RegExp(
|
||||||
),
|
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
|
||||||
const SizedBox(height: 16),
|
.hasMatch(value);
|
||||||
DidvanSwitch(
|
if (!emailValid) {
|
||||||
value: true,
|
return 'ایمیل وارد شده معتبر نمیباشد';
|
||||||
title: 'ورود با اثر انگشت',
|
}
|
||||||
onChanged: (value) => {},
|
},
|
||||||
),
|
),
|
||||||
|
// const SizedBox(height: 16),
|
||||||
|
// DidvanSwitch(
|
||||||
|
// value: true,
|
||||||
|
// title: 'ورود با اثر انگشت',
|
||||||
|
// onChanged: (value) => {},
|
||||||
|
// ),
|
||||||
const DidvanDivider(),
|
const DidvanDivider(),
|
||||||
MenuItem(
|
MenuItem(
|
||||||
title: 'تغییر رمز عبور',
|
title: 'تغییر رمز عبور',
|
||||||
onTap: () {},
|
onTap: () => Navigator.of(context).pushNamed(
|
||||||
|
Routes.authenticaion,
|
||||||
|
arguments: true,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Positioned(
|
||||||
|
left: 20,
|
||||||
|
right: 20,
|
||||||
|
bottom: 20,
|
||||||
|
child: DidvanButton(
|
||||||
|
key: UniqueKey(),
|
||||||
|
title: 'ذخیره تغییرات',
|
||||||
|
onPressed: () {
|
||||||
|
if (_formKey.currentState!.validate()) {
|
||||||
|
state.editProfile(fullName, username, email);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
enabled: _buttonEnabled,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _setButtonState() {
|
||||||
|
if (!_buttonEnabled) {
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get _buttonEnabled {
|
||||||
|
final user = context.read<UserProvider>().user;
|
||||||
|
return !(user.email == email &&
|
||||||
|
user.fullName == fullName &&
|
||||||
|
user.username == username);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onUsernameChanged(String value) {
|
||||||
|
_setButtonState();
|
||||||
|
username = value;
|
||||||
|
_timer?.cancel();
|
||||||
|
if (value.length < 5) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_timer = Timer(const Duration(seconds: 1), () {
|
||||||
|
_formKey.currentState!.validate();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ import 'dart:io';
|
||||||
|
|
||||||
import 'package:didvan/models/enums.dart';
|
import 'package:didvan/models/enums.dart';
|
||||||
import 'package:didvan/models/user.dart';
|
import 'package:didvan/models/user.dart';
|
||||||
|
import 'package:didvan/models/view/alert_data.dart';
|
||||||
import 'package:didvan/providers/core_provider.dart';
|
import 'package:didvan/providers/core_provider.dart';
|
||||||
import 'package:didvan/services/network/request.dart';
|
import 'package:didvan/services/network/request.dart';
|
||||||
import 'package:didvan/services/network/request_helper.dart';
|
import 'package:didvan/services/network/request_helper.dart';
|
||||||
|
import 'package:didvan/utils/action_sheet.dart';
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
|
|
||||||
class UserProvider extends CoreProvier {
|
class UserProvider extends CoreProvier {
|
||||||
|
|
@ -34,6 +36,8 @@ class UserProvider extends CoreProvier {
|
||||||
RequestService(RequestHelper.updateUserProfile);
|
RequestService(RequestHelper.updateUserProfile);
|
||||||
await service.multipart(file);
|
await service.multipart(file);
|
||||||
if (service.isSuccess) {
|
if (service.isSuccess) {
|
||||||
|
user =
|
||||||
|
user.copyWith(photo: RequestHelper.baseUrl + service.result['photo']);
|
||||||
appState = AppState.idle;
|
appState = AppState.idle;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -41,15 +45,55 @@ class UserProvider extends CoreProvier {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> setUsername(String username) async {
|
Future<bool?> checkUsername(String username) async {
|
||||||
appState = AppState.isolatedBusy;
|
if (user.username == username) return true;
|
||||||
final RequestService service = RequestService(
|
final RequestService service = RequestService(
|
||||||
RequestHelper.updateUsername,
|
RequestHelper.checkUsername,
|
||||||
body: {'username': username},
|
body: {'username': username},
|
||||||
);
|
);
|
||||||
|
await service.post();
|
||||||
|
if (service.isSuccess) {
|
||||||
|
return service.result['available'];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> editProfile(
|
||||||
|
String fullName,
|
||||||
|
String? username,
|
||||||
|
String? email,
|
||||||
|
) async {
|
||||||
|
appState = AppState.isolatedBusy;
|
||||||
|
final service = RequestService(
|
||||||
|
RequestHelper.updateProfile,
|
||||||
|
body: {
|
||||||
|
'fullName': fullName,
|
||||||
|
'email': email,
|
||||||
|
'username': username,
|
||||||
|
},
|
||||||
|
);
|
||||||
await service.put();
|
await service.put();
|
||||||
if (service.isSuccess) {
|
if (service.isSuccess) {
|
||||||
// appState = app
|
user = user.copyWith(
|
||||||
}
|
fullName: fullName,
|
||||||
|
email: email,
|
||||||
|
username: username,
|
||||||
|
);
|
||||||
|
appState = AppState.idle;
|
||||||
|
ActionSheetUtils.pop();
|
||||||
|
ActionSheetUtils.showAlert(
|
||||||
|
AlertData(
|
||||||
|
message: 'پروفایل با موفقیت ویرایش شد',
|
||||||
|
aLertType: ALertType.success,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
appState = AppState.idle;
|
||||||
|
ActionSheetUtils.showAlert(
|
||||||
|
AlertData(
|
||||||
|
message: service.errorMessage,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue