2022-09-20 14:11:38 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-21 14:37:16 +02:00
|
|
|
import 'package:flutter_profile/src/models/user.dart';
|
|
|
|
import 'package:flutter_profile/src/services/profile_service.dart';
|
|
|
|
import 'package:flutter_profile/src/widgets/avatar/avatar.dart';
|
|
|
|
import 'package:flutter_profile/src/widgets/item_builder/item_builder.dart';
|
|
|
|
import 'package:flutter_profile/src/widgets/item_builder/item_builder_options.dart';
|
|
|
|
import 'package:flutter_profile/src/widgets/item_builder/item_list.dart';
|
|
|
|
import 'package:flutter_profile/src/widgets/profile/profile_style.dart';
|
2022-09-20 14:11:38 +02:00
|
|
|
|
|
|
|
class ProfileWrapper extends StatefulWidget {
|
|
|
|
const ProfileWrapper({
|
|
|
|
Key? key,
|
|
|
|
required this.user,
|
|
|
|
required this.service,
|
|
|
|
required this.rebuild,
|
|
|
|
this.style = const ProfileStyle(),
|
|
|
|
this.customAvatar,
|
|
|
|
this.showAvatar = true,
|
|
|
|
this.itemBuilder,
|
|
|
|
this.itemBuilderOptions,
|
2022-09-21 14:08:36 +02:00
|
|
|
this.bottomActionText,
|
2022-09-20 14:11:38 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
final User user;
|
|
|
|
final ProfileService service;
|
|
|
|
final ProfileStyle style;
|
|
|
|
final Widget? customAvatar;
|
|
|
|
final bool showAvatar;
|
2022-09-21 14:08:36 +02:00
|
|
|
final String? bottomActionText;
|
2022-09-20 14:11:38 +02:00
|
|
|
final ItemBuilder? itemBuilder;
|
|
|
|
final Function rebuild;
|
|
|
|
final ItemBuilderOptions? itemBuilderOptions;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ProfileWrapper> createState() => _ProfileWrapperState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ProfileWrapperState extends State<ProfileWrapper> {
|
|
|
|
List<Widget> defaultItems = [];
|
|
|
|
|
2022-09-21 14:08:36 +02:00
|
|
|
GlobalKey<FormState> firstNameKey = GlobalKey<FormState>();
|
|
|
|
|
|
|
|
GlobalKey<FormState> lastNameKey = GlobalKey<FormState>();
|
|
|
|
|
2022-09-20 14:11:38 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
if (widget.itemBuilder == null) {
|
|
|
|
ItemBuilder builder = ItemBuilder(
|
|
|
|
options: widget.itemBuilderOptions ?? ItemBuilderOptions(),
|
|
|
|
);
|
2022-09-21 14:08:36 +02:00
|
|
|
defaultItems.add(
|
|
|
|
builder.build(
|
|
|
|
'firstName',
|
|
|
|
firstNameKey,
|
|
|
|
widget.user.firstName,
|
|
|
|
null,
|
|
|
|
(v) {
|
|
|
|
widget.user.firstName = v;
|
2022-09-20 14:11:38 +02:00
|
|
|
|
2022-09-21 14:08:36 +02:00
|
|
|
widget.service.editProfile(widget.user, 'firstName', v);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2022-09-20 14:11:38 +02:00
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
2022-09-21 14:08:36 +02:00
|
|
|
defaultItems.add(
|
|
|
|
builder.build(
|
|
|
|
'lastName',
|
|
|
|
lastNameKey,
|
|
|
|
widget.user.lastName,
|
|
|
|
null,
|
|
|
|
(v) {
|
|
|
|
widget.user.lastName = v;
|
2022-09-20 14:11:38 +02:00
|
|
|
|
2022-09-21 14:08:36 +02:00
|
|
|
widget.service.editProfile(widget.user, 'lastName', v);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2022-09-22 14:17:35 +02:00
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
2022-09-20 14:11:38 +02:00
|
|
|
} else {
|
2022-09-21 14:08:36 +02:00
|
|
|
defaultItems.add(
|
|
|
|
widget.itemBuilder!.build(
|
|
|
|
'firstName',
|
|
|
|
firstNameKey,
|
|
|
|
widget.user.firstName,
|
|
|
|
null,
|
|
|
|
(v) {
|
|
|
|
widget.user.firstName = v;
|
2022-09-20 14:11:38 +02:00
|
|
|
|
2022-09-21 14:08:36 +02:00
|
|
|
widget.service.editProfile(widget.user, 'firstname', v);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2022-09-20 14:11:38 +02:00
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
2022-09-21 14:08:36 +02:00
|
|
|
defaultItems.add(
|
|
|
|
widget.itemBuilder!.build(
|
|
|
|
'lastName',
|
|
|
|
lastNameKey,
|
|
|
|
widget.user.lastName,
|
|
|
|
null,
|
|
|
|
(v) {
|
|
|
|
widget.user.lastName = v;
|
2022-09-20 14:11:38 +02:00
|
|
|
|
2022-09-21 14:08:36 +02:00
|
|
|
widget.service.editProfile(widget.user, 'lastName', v);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2022-09-22 14:00:55 +02:00
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
2022-09-20 14:11:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
child: Padding(
|
|
|
|
padding: widget.style.pagePadding,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
if (widget.showAvatar)
|
|
|
|
InkWell(
|
|
|
|
onTap: () async {
|
|
|
|
await widget.service.uploadImage(context);
|
|
|
|
},
|
|
|
|
child: Avatar(
|
|
|
|
firstName: widget.user.firstName,
|
|
|
|
lastName: widget.user.lastName,
|
|
|
|
style: widget.style.avatarStyle,
|
|
|
|
avatar: widget.customAvatar,
|
|
|
|
image: widget.user.image,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (widget.showAvatar)
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
...defaultItems,
|
2022-09-21 14:08:36 +02:00
|
|
|
ItemList(
|
2022-09-20 14:11:38 +02:00
|
|
|
widget.user.profileData!.toMap(),
|
|
|
|
widget.user.profileData!.mapWidget(
|
|
|
|
() {
|
|
|
|
widget.rebuild();
|
|
|
|
},
|
|
|
|
context,
|
|
|
|
),
|
|
|
|
widget.style.betweenDefaultItemPadding,
|
|
|
|
(key, value) {
|
|
|
|
widget.service.editProfile(widget.user, key, value);
|
|
|
|
},
|
|
|
|
itemBuilder: widget.itemBuilder,
|
|
|
|
itemBuilderOptions: widget.itemBuilderOptions,
|
|
|
|
),
|
2022-09-21 14:08:36 +02:00
|
|
|
if (widget.bottomActionText != null)
|
2022-09-20 14:11:38 +02:00
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
const Spacer(),
|
2022-09-21 14:08:36 +02:00
|
|
|
if (widget.bottomActionText != null)
|
2022-09-20 14:11:38 +02:00
|
|
|
InkWell(
|
|
|
|
onTap: () {
|
2022-09-21 14:08:36 +02:00
|
|
|
widget.service.pageBottomAction();
|
2022-09-20 14:11:38 +02:00
|
|
|
},
|
2022-09-21 11:09:24 +02:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
2022-09-21 14:08:36 +02:00
|
|
|
child: Text(widget.bottomActionText!),
|
2022-09-21 11:09:24 +02:00
|
|
|
),
|
2022-09-20 14:11:38 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|