-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcontact_list_tile.dart
51 lines (46 loc) · 1.53 KB
/
contact_list_tile.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import 'package:flutter/material.dart';
class ContactListTile extends StatelessWidget {
const ContactListTile({
super.key,
required this.onPressed,
required this.name,
required this.email,
}) : assert(name.length > 0, 'name must be non-empty'),
assert(email.length > 0, 'email must be non-empty');
final VoidCallback? onPressed;
final String name;
final String email;
bool get _isEnabled => onPressed != null;
/// Combines the first character of the [name]'s first and last names.
String get _initials {
final charsByPart = name.split(' ').map((part) => part.split('')).toList();
final initialsBuffer = StringBuffer()..write(charsByPart[0][0]);
if (charsByPart.length > 1) {
initialsBuffer.write(charsByPart.last[0]);
}
return initialsBuffer.toString().toUpperCase();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: InkWell(
onTap: onPressed,
customBorder: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
enabled: _isEnabled,
leading: CircleAvatar(
backgroundColor: _isEnabled ? Colors.blue.shade900 : Colors.grey,
foregroundColor: Colors.white,
child: Text(_initials),
),
title: Text(name),
subtitle: Text(email),
),
),
);
}
}