mirror of
https://github.com/Iconica-Development/flutter_profile.git
synced 2025-05-18 16:53:45 +02:00
fix unit test: Profile page with preset values
This commit is contained in:
parent
8e81b1110a
commit
b30d5305ba
8 changed files with 117 additions and 90 deletions
|
@ -5,3 +5,7 @@
|
|||
## 0.0.2
|
||||
|
||||
* Add prioritizedItems option to display items at the top of the page.
|
||||
|
||||
## 0.0.3
|
||||
|
||||
* Add support for image URL (instead of in-memory image using image parameter from User model)
|
|
@ -3,6 +3,7 @@ library flutter_profile;
|
|||
export 'src/widgets/profile/profile_page.dart';
|
||||
export 'src/widgets/profile/profile_style.dart';
|
||||
export 'src/widgets/avatar/avatar_wrapper.dart';
|
||||
export 'src/widgets/avatar/avatar.dart';
|
||||
export 'src/widgets/avatar/avatar_style.dart';
|
||||
export 'src/services/profile_service.dart';
|
||||
export 'src/widgets/item_builder/item_builder.dart';
|
||||
|
|
66
lib/src/widgets/avatar/avatar.dart
Normal file
66
lib/src/widgets/avatar/avatar.dart
Normal file
|
@ -0,0 +1,66 @@
|
|||
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({
|
||||
Key? key,
|
||||
required this.user,
|
||||
this.style = const AvatarStyle(),
|
||||
}) : super(key: key);
|
||||
|
||||
final User user;
|
||||
final AvatarStyle style;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var imageProvider = _getImageProvider();
|
||||
|
||||
if (imageProvider != null) {
|
||||
return Container(
|
||||
width: style.width,
|
||||
height: style.height,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
image: DecorationImage(
|
||||
image: imageProvider,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
);
|
||||
} else if (user.firstName != null || user.lastName != null) {
|
||||
return Container(
|
||||
width: style.width,
|
||||
height: style.height,
|
||||
decoration: BoxDecoration(
|
||||
color: _generateColorWithIntials(user.firstName, user.lastName),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
style: const TextStyle(fontSize: 40),
|
||||
user.initials,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Container();
|
||||
}
|
||||
|
||||
ImageProvider? _getImageProvider() {
|
||||
if (user.image != null) {
|
||||
return MemoryImage(user.image!);
|
||||
} else if (user.imageUrl != null) {
|
||||
return NetworkImage(user.imageUrl!);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Color _generateColorWithIntials(String? firstName, String? lastName) {
|
||||
var uniqueInitialId = (firstName?.toLowerCase().codeUnitAt(0) ?? 0) +
|
||||
(lastName?.toLowerCase().codeUnitAt(0) ?? 0);
|
||||
|
||||
return Colors.primaries[uniqueInitialId % Colors.primaries.length];
|
||||
}
|
||||
}
|
|
@ -13,12 +13,16 @@ class AvatarStyle {
|
|||
const AvatarStyle({
|
||||
this.width = 100,
|
||||
this.height = 100,
|
||||
this.displayName = true,
|
||||
this.initialStyle = const TextStyle(),
|
||||
this.displayNamePadding = const EdgeInsets.only(top: 16),
|
||||
this.displayNameStyle = const TextStyle(),
|
||||
});
|
||||
|
||||
final double width;
|
||||
final double height;
|
||||
final TextStyle initialStyle;
|
||||
final bool displayName;
|
||||
final EdgeInsets displayNamePadding;
|
||||
final TextStyle displayNameStyle;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_profile/src/models/user.dart';
|
||||
import 'package:flutter_profile/src/widgets/avatar/avatar.dart';
|
||||
import 'package:flutter_profile/src/widgets/avatar/avatar_style.dart';
|
||||
|
||||
class AvaterWrapper extends StatelessWidget {
|
||||
|
@ -15,68 +16,29 @@ class AvaterWrapper extends StatelessWidget {
|
|||
final AvatarStyle style;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Column(
|
||||
children: [
|
||||
avatar ?? _generateAvatar(),
|
||||
const SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
if (user.firstName != null || user.firstName != null)
|
||||
Text(
|
||||
Widget build(BuildContext context) {
|
||||
var avatar = this.avatar ??
|
||||
Avatar(
|
||||
user: user,
|
||||
style: style,
|
||||
);
|
||||
|
||||
if (!style.displayName) {
|
||||
return avatar;
|
||||
}
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
avatar,
|
||||
if (user.firstName != null || user.firstName != null)
|
||||
Padding(
|
||||
padding: style.displayNamePadding,
|
||||
child: 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 _generateAvatar() {
|
||||
var imageProvider = _getImageProvider();
|
||||
|
||||
if (imageProvider != null) {
|
||||
return Container(
|
||||
width: style.width,
|
||||
height: style.height,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
image: DecorationImage(
|
||||
image: imageProvider,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
);
|
||||
} else if (user.firstName != null || user.lastName != null) {
|
||||
return Container(
|
||||
width: style.width,
|
||||
height: style.height,
|
||||
decoration: BoxDecoration(
|
||||
color: _generateColorWithIntials(user.firstName, user.lastName),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
style: const TextStyle(fontSize: 40),
|
||||
user.initials,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Container();
|
||||
}
|
||||
|
||||
Color _generateColorWithIntials(String? firstName, String? lastName) {
|
||||
var uniqueInitialId = (firstName?.toLowerCase().codeUnitAt(0) ?? 0) +
|
||||
(lastName?.toLowerCase().codeUnitAt(0) ?? 0);
|
||||
|
||||
return Colors.primaries[uniqueInitialId % Colors.primaries.length];
|
||||
),
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,14 +56,14 @@ class _ProfileWrapperState extends State<ProfileWrapper> {
|
|||
);
|
||||
defaultItems.add(
|
||||
builder.build(
|
||||
'firstName',
|
||||
'first_name',
|
||||
firstNameKey,
|
||||
widget.user.firstName,
|
||||
null,
|
||||
(v) {
|
||||
widget.user.firstName = v;
|
||||
|
||||
widget.service.editProfile(widget.user, 'firstName', v);
|
||||
widget.service.editProfile(widget.user, 'first_name', v);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
@ -74,14 +74,14 @@ class _ProfileWrapperState extends State<ProfileWrapper> {
|
|||
);
|
||||
defaultItems.add(
|
||||
builder.build(
|
||||
'lastName',
|
||||
'last_name',
|
||||
lastNameKey,
|
||||
widget.user.lastName,
|
||||
null,
|
||||
(v) {
|
||||
widget.user.lastName = v;
|
||||
|
||||
widget.service.editProfile(widget.user, 'lastName', v);
|
||||
widget.service.editProfile(widget.user, 'last_name', v);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
@ -93,14 +93,14 @@ class _ProfileWrapperState extends State<ProfileWrapper> {
|
|||
} else {
|
||||
defaultItems.add(
|
||||
widget.itemBuilder!.build(
|
||||
'firstName',
|
||||
'first_name',
|
||||
firstNameKey,
|
||||
widget.user.firstName,
|
||||
null,
|
||||
(v) {
|
||||
widget.user.firstName = v;
|
||||
|
||||
widget.service.editProfile(widget.user, 'firstname', v);
|
||||
widget.service.editProfile(widget.user, 'first_name', v);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
@ -111,14 +111,14 @@ class _ProfileWrapperState extends State<ProfileWrapper> {
|
|||
);
|
||||
defaultItems.add(
|
||||
widget.itemBuilder!.build(
|
||||
'lastName',
|
||||
'last_name',
|
||||
lastNameKey,
|
||||
widget.user.lastName,
|
||||
null,
|
||||
(v) {
|
||||
widget.user.lastName = v;
|
||||
|
||||
widget.service.editProfile(widget.user, 'lastName', v);
|
||||
widget.service.editProfile(widget.user, 'last_name', v);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_profile/flutter_profile.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'test_classes/test_profile_data.dart';
|
||||
import 'test_classes/test_profile_service.dart';
|
||||
|
||||
|
@ -15,7 +13,6 @@ void main() {
|
|||
user: User(
|
||||
firstName: 'Firstname',
|
||||
lastName: 'Lastname',
|
||||
image: Uint8List.fromList([]),
|
||||
profileData: TestProfileData(email: 'test@email.com'),
|
||||
),
|
||||
service: TestProfileService(),
|
||||
|
|
|
@ -12,28 +12,21 @@ class TestProfileData extends ProfileData {
|
|||
Map<String, dynamic> mapWidget(
|
||||
VoidCallback update,
|
||||
BuildContext context,
|
||||
) {
|
||||
return {
|
||||
'email': null,
|
||||
};
|
||||
}
|
||||
) =>
|
||||
{
|
||||
'email': null,
|
||||
};
|
||||
|
||||
@override
|
||||
ProfileData fromMap(Map<String, dynamic> data) {
|
||||
return TestProfileData(
|
||||
email: data['email'],
|
||||
);
|
||||
}
|
||||
ProfileData fromMap(Map<String, dynamic> data) => TestProfileData(
|
||||
email: data['email'],
|
||||
);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'email': email,
|
||||
};
|
||||
}
|
||||
Map<String, dynamic> toMap() => {
|
||||
'email': email,
|
||||
};
|
||||
|
||||
@override
|
||||
ProfileData create() {
|
||||
return TestProfileData();
|
||||
}
|
||||
ProfileData create() => TestProfileData();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue