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';
|
|
|
|
import 'package:profile/src/widgets/item_builder/item_builder_options.dart';
|
|
|
|
|
|
|
|
import 'package:profile/src/widgets/profile/profile_style.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> {
|
2022-08-26 15:31:42 +02:00
|
|
|
late List<Widget> profileItems;
|
2022-08-26 16:28:40 +02:00
|
|
|
List<Widget> defaultItems = [];
|
|
|
|
|
2022-08-26 15:31:42 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
profileItems = widget.user.profileData!.buildItems(
|
|
|
|
widget.user.profileData!.toMap(),
|
2022-08-26 16:28:40 +02:00
|
|
|
widget.user.profileData!.mapWidget(() {
|
|
|
|
widget.rebuild();
|
|
|
|
}),
|
|
|
|
widget.style.betweenDefaultItemPadding,
|
2022-08-26 15:31:42 +02:00
|
|
|
(key, value) {
|
2022-08-26 16:28:40 +02:00
|
|
|
widget.service.editProfile(widget.user, key, value);
|
2022-08-26 15:31:42 +02:00
|
|
|
},
|
|
|
|
itemBuilder: widget.itemBuilder,
|
|
|
|
itemBuilderOptions: widget.itemBuilderOptions,
|
|
|
|
);
|
2022-08-26 16:28:40 +02:00
|
|
|
if (widget.itemBuilder == null) {
|
|
|
|
ItemBuilder builder = ItemBuilder(
|
|
|
|
options: widget.itemBuilderOptions ?? const ItemBuilderOptions(),
|
|
|
|
);
|
|
|
|
defaultItems.add(builder.build(widget.user.firstName, null, (v) {
|
|
|
|
widget.user.firstName = v;
|
|
|
|
}));
|
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
defaultItems.add(builder.build(widget.user.lastName, null, (v) {
|
|
|
|
widget.user.lastName = v;
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
defaultItems
|
|
|
|
.add(widget.itemBuilder!.build(widget.user.firstName, null, (v) {
|
|
|
|
widget.user.firstName = v;
|
|
|
|
}));
|
|
|
|
defaultItems.add(
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
defaultItems
|
|
|
|
.add(widget.itemBuilder!.build(widget.user.lastName, null, (v) {
|
|
|
|
widget.user.lastName = v;
|
|
|
|
}));
|
|
|
|
}
|
2022-08-26 15:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Material(
|
|
|
|
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(
|
|
|
|
onTap: () {
|
|
|
|
widget.service.uploadImage();
|
|
|
|
},
|
|
|
|
child: Avatar(
|
|
|
|
name: '${widget.user.firstName} ${widget.user.lastName}',
|
|
|
|
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-08-26 15:31:42 +02:00
|
|
|
...profileItems,
|
2022-08-26 16:28:40 +02:00
|
|
|
if (widget.showDeleteProfile)
|
|
|
|
SizedBox(
|
|
|
|
height: widget.style.betweenDefaultItemPadding,
|
|
|
|
),
|
|
|
|
if (widget.showDeleteProfile)
|
|
|
|
InkWell(
|
|
|
|
onTap: () {
|
|
|
|
widget.service.deleteProfile();
|
|
|
|
},
|
|
|
|
child: const Text('Profiel verwijderen'),
|
|
|
|
),
|
2022-08-26 15:31:42 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|