mirror of
https://github.com/Iconica-Development/flutter_profile.git
synced 2025-05-19 01:03: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
|
## 0.0.2
|
||||||
|
|
||||||
* Add prioritizedItems option to display items at the top of the page.
|
* 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_page.dart';
|
||||||
export 'src/widgets/profile/profile_style.dart';
|
export 'src/widgets/profile/profile_style.dart';
|
||||||
export 'src/widgets/avatar/avatar_wrapper.dart';
|
export 'src/widgets/avatar/avatar_wrapper.dart';
|
||||||
|
export 'src/widgets/avatar/avatar.dart';
|
||||||
export 'src/widgets/avatar/avatar_style.dart';
|
export 'src/widgets/avatar/avatar_style.dart';
|
||||||
export 'src/services/profile_service.dart';
|
export 'src/services/profile_service.dart';
|
||||||
export 'src/widgets/item_builder/item_builder.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({
|
const AvatarStyle({
|
||||||
this.width = 100,
|
this.width = 100,
|
||||||
this.height = 100,
|
this.height = 100,
|
||||||
|
this.displayName = true,
|
||||||
this.initialStyle = const TextStyle(),
|
this.initialStyle = const TextStyle(),
|
||||||
|
this.displayNamePadding = const EdgeInsets.only(top: 16),
|
||||||
this.displayNameStyle = const TextStyle(),
|
this.displayNameStyle = const TextStyle(),
|
||||||
});
|
});
|
||||||
|
|
||||||
final double width;
|
final double width;
|
||||||
final double height;
|
final double height;
|
||||||
final TextStyle initialStyle;
|
final TextStyle initialStyle;
|
||||||
|
final bool displayName;
|
||||||
|
final EdgeInsets displayNamePadding;
|
||||||
final TextStyle displayNameStyle;
|
final TextStyle displayNameStyle;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_profile/src/models/user.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';
|
import 'package:flutter_profile/src/widgets/avatar/avatar_style.dart';
|
||||||
|
|
||||||
class AvaterWrapper extends StatelessWidget {
|
class AvaterWrapper extends StatelessWidget {
|
||||||
|
@ -15,68 +16,29 @@ class AvaterWrapper extends StatelessWidget {
|
||||||
final AvatarStyle style;
|
final AvatarStyle style;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => Column(
|
Widget build(BuildContext context) {
|
||||||
children: [
|
var avatar = this.avatar ??
|
||||||
avatar ?? _generateAvatar(),
|
Avatar(
|
||||||
const SizedBox(
|
user: user,
|
||||||
height: 16,
|
style: style,
|
||||||
),
|
);
|
||||||
if (user.firstName != null || user.firstName != null)
|
|
||||||
Text(
|
if (!style.displayName) {
|
||||||
|
return avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
avatar,
|
||||||
|
if (user.firstName != null || user.firstName != null)
|
||||||
|
Padding(
|
||||||
|
padding: style.displayNamePadding,
|
||||||
|
child: Text(
|
||||||
user.displayName,
|
user.displayName,
|
||||||
style: style.displayNameStyle,
|
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(
|
defaultItems.add(
|
||||||
builder.build(
|
builder.build(
|
||||||
'firstName',
|
'first_name',
|
||||||
firstNameKey,
|
firstNameKey,
|
||||||
widget.user.firstName,
|
widget.user.firstName,
|
||||||
null,
|
null,
|
||||||
(v) {
|
(v) {
|
||||||
widget.user.firstName = 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(
|
defaultItems.add(
|
||||||
builder.build(
|
builder.build(
|
||||||
'lastName',
|
'last_name',
|
||||||
lastNameKey,
|
lastNameKey,
|
||||||
widget.user.lastName,
|
widget.user.lastName,
|
||||||
null,
|
null,
|
||||||
(v) {
|
(v) {
|
||||||
widget.user.lastName = 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 {
|
} else {
|
||||||
defaultItems.add(
|
defaultItems.add(
|
||||||
widget.itemBuilder!.build(
|
widget.itemBuilder!.build(
|
||||||
'firstName',
|
'first_name',
|
||||||
firstNameKey,
|
firstNameKey,
|
||||||
widget.user.firstName,
|
widget.user.firstName,
|
||||||
null,
|
null,
|
||||||
(v) {
|
(v) {
|
||||||
widget.user.firstName = 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(
|
defaultItems.add(
|
||||||
widget.itemBuilder!.build(
|
widget.itemBuilder!.build(
|
||||||
'lastName',
|
'last_name',
|
||||||
lastNameKey,
|
lastNameKey,
|
||||||
widget.user.lastName,
|
widget.user.lastName,
|
||||||
null,
|
null,
|
||||||
(v) {
|
(v) {
|
||||||
widget.user.lastName = 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/material.dart';
|
||||||
import 'package:flutter_profile/flutter_profile.dart';
|
import 'package:flutter_profile/flutter_profile.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
import 'test_classes/test_profile_data.dart';
|
import 'test_classes/test_profile_data.dart';
|
||||||
import 'test_classes/test_profile_service.dart';
|
import 'test_classes/test_profile_service.dart';
|
||||||
|
|
||||||
|
@ -15,7 +13,6 @@ void main() {
|
||||||
user: User(
|
user: User(
|
||||||
firstName: 'Firstname',
|
firstName: 'Firstname',
|
||||||
lastName: 'Lastname',
|
lastName: 'Lastname',
|
||||||
image: Uint8List.fromList([]),
|
|
||||||
profileData: TestProfileData(email: 'test@email.com'),
|
profileData: TestProfileData(email: 'test@email.com'),
|
||||||
),
|
),
|
||||||
service: TestProfileService(),
|
service: TestProfileService(),
|
||||||
|
|
|
@ -12,28 +12,21 @@ class TestProfileData extends ProfileData {
|
||||||
Map<String, dynamic> mapWidget(
|
Map<String, dynamic> mapWidget(
|
||||||
VoidCallback update,
|
VoidCallback update,
|
||||||
BuildContext context,
|
BuildContext context,
|
||||||
) {
|
) =>
|
||||||
return {
|
{
|
||||||
'email': null,
|
'email': null,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ProfileData fromMap(Map<String, dynamic> data) {
|
ProfileData fromMap(Map<String, dynamic> data) => TestProfileData(
|
||||||
return TestProfileData(
|
email: data['email'],
|
||||||
email: data['email'],
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toMap() {
|
Map<String, dynamic> toMap() => {
|
||||||
return {
|
'email': email,
|
||||||
'email': email,
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ProfileData create() {
|
ProfileData create() => TestProfileData();
|
||||||
return TestProfileData();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue