feat: provide image as url + refactors

This commit is contained in:
Stein Milder 2022-10-21 10:42:32 +02:00
parent 9aa4231de7
commit 8e81b1110a
6 changed files with 77 additions and 83 deletions

View file

@ -37,12 +37,12 @@ class _ProfileExampleState extends State<ProfileExample> {
void initState() {
super.initState();
_user = User(
'Firstname',
'Lastname',
Uint8List.fromList(
firstName: 'Firstname',
lastName: 'Lastname',
image: Uint8List.fromList(
[],
),
profileData,
profileData: profileData,
);
}
@ -53,10 +53,10 @@ class _ProfileExampleState extends State<ProfileExample> {
bottomActionText: 'Log out',
itemBuilderOptions: ItemBuilderOptions(
inputDecorationField: {
'firstName': const InputDecoration(
'first_name': const InputDecoration(
label: Text('First name'),
),
'lastName': const InputDecoration(
'last_name': const InputDecoration(
label: Text('Last name'),
),
'email': const InputDecoration(
@ -64,13 +64,13 @@ class _ProfileExampleState extends State<ProfileExample> {
),
},
validators: {
'firstName': (String? value) {
'first_name': (String? value) {
if (value == null || value.isEmpty) {
return 'Field empty';
}
return null;
},
'lastName': (String? value) {
'last_name': (String? value) {
if (value == null || value.isEmpty) {
return 'Field empty';
}

View file

@ -2,7 +2,7 @@ library flutter_profile;
export 'src/widgets/profile/profile_page.dart';
export 'src/widgets/profile/profile_style.dart';
export 'src/widgets/avatar/avatar.dart';
export 'src/widgets/avatar/avatar_wrapper.dart';
export 'src/widgets/avatar/avatar_style.dart';
export 'src/services/profile_service.dart';
export 'src/widgets/item_builder/item_builder.dart';

View file

@ -2,39 +2,42 @@ import 'dart:typed_data';
import 'package:flutter/material.dart';
/// User is used to contain all user data. It consists of three standard fields: firstName, lastName and image.
/// User is used to contain all user data. It consists of three standard fields: firstName, lastName and image/imageUrl.
///
/// For additional data profileData can be used.
class User {
String? firstName;
String? lastName;
Uint8List? image;
String? imageUrl;
ProfileData? profileData;
User(
User({
this.firstName,
this.lastName,
this.image,
this.imageUrl,
this.profileData,
);
});
factory User.fromMap(Map<String, dynamic> data) {
return User(
data['firstName'],
data['lastName'],
data['image'],
data['profileData'],
);
}
String get displayName => '${firstName ?? ''} ${lastName ?? ''}';
String get initials => '${firstName?[0] ?? ''}${lastName?[0] ?? ''}';
Map<String, dynamic> toMap() {
return {
'firstName': firstName,
'lastName': lastName,
'image': image,
'profileData': profileData,
};
}
factory User.fromMap(Map<String, dynamic> data) => User(
firstName: data['first_name'],
lastName: data['last_name'],
image: data['image'],
imageUrl: data['image_url'],
profileData: data['profile_data'],
);
Map<String, dynamic> toMap() => {
'first_name': firstName,
'last_name': lastName,
'image': image,
'image_url': image,
'profile_data': profileData,
};
}
/// ProfileData is used to store custom/addintional data for a user.

View file

@ -1,79 +1,76 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_profile/src/models/user.dart';
import 'package:flutter_profile/src/widgets/avatar/avatar_style.dart';
class Avatar extends StatelessWidget {
const Avatar({
class AvaterWrapper extends StatelessWidget {
const AvaterWrapper({
Key? key,
this.image,
this.firstName,
this.lastName,
required this.user,
this.avatar,
this.style = const AvatarStyle(),
}) : super(key: key);
final Uint8List? image;
final String? firstName;
final String? lastName;
final User user;
final Widget? avatar;
final AvatarStyle style;
@override
Widget build(BuildContext context) {
return Column(
children: [
_avatar(),
const SizedBox(
height: 16,
),
if (firstName != null || lastName != null)
Text(
'${firstName ?? ''} ${lastName ?? ''}',
style: style.displayNameStyle,
)
],
);
Widget build(BuildContext context) => Column(
children: [
avatar ?? _generateAvatar(),
const SizedBox(
height: 16,
),
if (user.firstName != null || user.firstName != null)
Text(
user.displayName,
style: style.displayNameStyle,
)
],
);
ImageProvider? _getImageProvider() {
if (user.image != null) {
return MemoryImage(user.image!);
} else if (user.imageUrl != null) {
return NetworkImage(user.imageUrl!);
}
return null;
}
Widget _avatar() {
if (avatar != null) {
return avatar!;
}
if (image != null && image!.isNotEmpty) {
Widget _generateAvatar() {
var imageProvider = _getImageProvider();
if (imageProvider != null) {
return Container(
width: style.width,
height: style.height,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: MemoryImage(image!),
image: imageProvider,
fit: BoxFit.cover,
),
),
);
} else if (firstName != null || lastName != null) {
} else if (user.firstName != null || user.lastName != null) {
return Container(
width: style.width,
height: style.height,
decoration: BoxDecoration(
color: _generateColorWithIntials(firstName, lastName),
color: _generateColorWithIntials(user.firstName, user.lastName),
shape: BoxShape.circle,
),
child: Center(
child: Text(
style: const TextStyle(fontSize: 40),
_getInitials(firstName, lastName),
user.initials,
),
),
);
} else {
return Container();
}
}
String _getInitials(String? firstName, String? lastName) {
return (firstName?[0] ?? '') + (lastName?[0] ?? '');
return Container();
}
Color _generateColorWithIntials(String? firstName, String? lastName) {

View file

@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
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/avatar/avatar_wrapper.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';
@ -143,12 +143,10 @@ class _ProfileWrapperState extends State<ProfileWrapper> {
onTap: () async {
await widget.service.uploadImage(context);
},
child: Avatar(
firstName: widget.user.firstName,
lastName: widget.user.lastName,
child: AvaterWrapper(
user: widget.user,
style: widget.style.avatarStyle,
avatar: widget.customAvatar,
image: widget.user.image,
),
),
SizedBox(

View file

@ -13,10 +13,10 @@ void main() {
home: Material(
child: ProfilePage(
user: User(
'Firstname',
'Lastname',
Uint8List.fromList([]),
TestProfileData(email: 'test@email.com'),
firstName: 'Firstname',
lastName: 'Lastname',
image: Uint8List.fromList([]),
profileData: TestProfileData(email: 'test@email.com'),
),
service: TestProfileService(),
),
@ -39,10 +39,7 @@ void main() {
home: Material(
child: ProfilePage(
user: User(
null,
null,
null,
TestProfileData(email: null),
profileData: TestProfileData(email: null),
),
service: TestProfileService(),
),
@ -58,10 +55,9 @@ void main() {
home: Material(
child: ProfilePage(
user: User(
'Firstname',
'Lastname',
null,
TestProfileData(email: 'test@email.com'),
firstName: 'Firstname',
lastName: 'Lastname',
profileData: TestProfileData(email: 'test@email.com'),
),
service: TestProfileService(),
),