2022-08-26 15:31:42 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:profile/profile.dart';
|
|
|
|
import 'package:profile/src/widgets/avatar/avatar.dart';
|
|
|
|
|
|
|
|
class ProfilePage extends StatefulWidget {
|
|
|
|
const ProfilePage({
|
|
|
|
Key? key,
|
|
|
|
required this.user,
|
2022-08-26 16:28:40 +02:00
|
|
|
required this.service,
|
2022-08-26 15:31:42 +02:00
|
|
|
this.style = const ProfileStyle(),
|
|
|
|
this.customAvatar,
|
|
|
|
this.showAvatar = true,
|
|
|
|
this.itemBuilder,
|
|
|
|
this.itemBuilderOptions,
|
2022-08-26 16:28:40 +02:00
|
|
|
this.showDeleteProfile = true,
|
2022-08-26 15:31:42 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
final User user;
|
|
|
|
final ProfileService service;
|
|
|
|
final ProfileStyle style;
|
|
|
|
final Widget? customAvatar;
|
|
|
|
final bool showAvatar;
|
2022-08-26 16:28:40 +02:00
|
|
|
final bool showDeleteProfile;
|
2022-08-26 15:31:42 +02:00
|
|
|
final ItemBuilder? itemBuilder;
|
2022-08-26 16:28:40 +02:00
|
|
|
|
2022-08-26 15:31:42 +02:00
|
|
|
final ItemBuilderOptions? itemBuilderOptions;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ProfilePage> createState() => _ProfilePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ProfilePageState extends State<ProfilePage> {
|
2022-08-26 16:28:40 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return ProfileWrapper(
|
|
|
|
service: widget.service,
|
|
|
|
user: widget.user,
|
|
|
|
rebuild: () {
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
style: widget.style,
|
|
|
|
customAvatar: widget.customAvatar,
|
|
|
|
showAvatar: widget.showAvatar,
|
|
|
|
showDeleteProfile: widget.showDeleteProfile,
|
|
|
|
itemBuilder: widget.itemBuilder,
|
|
|
|
itemBuilderOptions: widget.itemBuilderOptions,
|
|
|
|
key: UniqueKey(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
this.showDeleteProfile = true,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
final User user;
|
|
|
|
final ProfileService service;
|
|
|
|
final ProfileStyle style;
|
|
|
|
final Widget? customAvatar;
|
|
|
|
final bool showAvatar;
|
|
|
|
final bool showDeleteProfile;
|
|
|
|
final ItemBuilder? itemBuilder;
|
|
|
|
final Function rebuild;
|
|
|
|
final ItemBuilderOptions? itemBuilderOptions;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ProfileWrapper> createState() => _ProfileWrapperState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ProfileWrapperState extends State<ProfileWrapper> {
|
|
|
|
List<Widget> defaultItems = [];
|
|
|
|
|
2022-08-26 15:31:42 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2022-09-19 12:11:58 +02:00
|
|
|
|
2022-08-26 16:28:40 +02:00
|
|
|
if (widget.itemBuilder == null) {
|
|
|
|
ItemBuilder builder = ItemBuilder(
|
2022-09-19 12:11:58 +02:00
|
|
|
options: widget.itemBuilderOptions ?? ItemBuilderOptions(),
|
2022-08-26 16:28:40 +02:00
|
|
|
);
|
2022-09-19 12:11:58 +02:00
|
|
|
defaultItems
|
|
|
|
.add(builder.build('firstName', widget.user.firstName, null, (v) {
|
2022-08-26 16:28:40 +02:00
|
|
|
widget.user.firstName = v;
|
2022-09-19 12:11:58 +02:00
|
|
|
|
|
|
|
widget.service.editProfile(widget.user, 'firstName', v);
|
2022-08-26 16:28:40 +02:00
|
|
|
}));
|
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
2022-09-19 12:11:58 +02:00
|
|
|
defaultItems
|
|
|
|
.add(builder.build('lastName', widget.user.lastName, null, (v) {
|
2022-08-26 16:28:40 +02:00
|
|
|
widget.user.lastName = v;
|
2022-09-19 12:11:58 +02:00
|
|
|
|
|
|
|
widget.service.editProfile(widget.user, 'lastName', v);
|
2022-08-26 16:28:40 +02:00
|
|
|
}));
|
|
|
|
} else {
|
2022-09-19 12:11:58 +02:00
|
|
|
defaultItems.add(widget.itemBuilder!
|
|
|
|
.build('firstName', widget.user.firstName, null, (v) {
|
2022-08-26 16:28:40 +02:00
|
|
|
widget.user.firstName = v;
|
2022-09-19 12:11:58 +02:00
|
|
|
|
|
|
|
widget.service.editProfile(widget.user, 'firstname', v);
|
2022-08-26 16:28:40 +02:00
|
|
|
}));
|
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
2022-09-19 12:11:58 +02:00
|
|
|
defaultItems.add(widget.itemBuilder!
|
|
|
|
.build('lastName', widget.user.lastName, null, (v) {
|
2022-08-26 16:28:40 +02:00
|
|
|
widget.user.lastName = v;
|
2022-09-19 12:11:58 +02:00
|
|
|
|
|
|
|
widget.service.editProfile(widget.user, 'lastName', v);
|
2022-08-26 16:28:40 +02:00
|
|
|
}));
|
|
|
|
}
|
2022-08-26 15:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Material(
|
2022-09-19 12:11:58 +02:00
|
|
|
color: Colors.transparent,
|
2022-08-26 15:31:42 +02:00
|
|
|
child: Padding(
|
2022-08-26 16:28:40 +02:00
|
|
|
padding: widget.style.pagePadding,
|
2022-08-26 15:31:42 +02:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
if (widget.showAvatar)
|
2022-08-26 16:28:40 +02:00
|
|
|
InkWell(
|
2022-09-19 12:11:58 +02:00
|
|
|
onTap: () async {
|
|
|
|
await widget.service.uploadImage(context);
|
2022-08-26 16:28:40 +02:00
|
|
|
},
|
|
|
|
child: Avatar(
|
2022-09-19 12:11:58 +02:00
|
|
|
firstName: widget.user.firstName,
|
|
|
|
lastName: widget.user.lastName,
|
2022-08-26 16:28:40 +02:00
|
|
|
style: widget.style.avatarStyle,
|
|
|
|
avatar: widget.customAvatar,
|
|
|
|
image: widget.user.image,
|
|
|
|
),
|
2022-08-26 15:31:42 +02:00
|
|
|
),
|
2022-08-26 16:28:40 +02:00
|
|
|
if (widget.showAvatar)
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
...defaultItems,
|
2022-09-19 12:11:58 +02:00
|
|
|
...widget.user.profileData!.buildItems(
|
|
|
|
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-08-26 16:28:40 +02:00
|
|
|
if (widget.showDeleteProfile)
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
2022-09-19 12:11:58 +02:00
|
|
|
const Spacer(),
|
2022-08-26 16:28:40 +02:00
|
|
|
if (widget.showDeleteProfile)
|
|
|
|
InkWell(
|
|
|
|
onTap: () {
|
|
|
|
widget.service.deleteProfile();
|
|
|
|
},
|
|
|
|
child: const Text('Profiel verwijderen'),
|
|
|
|
),
|
2022-08-26 15:31:42 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|