Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB

  1. import 'package:flutter/material.dart';
  2. import '/components/custom_circle_avatar.dart';
  3. import '/components/flash_message.dart';
  4. import '/database/models/conversations.dart';
  5. import '/database/models/friends.dart';
  6. import '/services/conversations_service.dart';
  7. import '/utils/strings.dart';
  8. import '/views/main/conversation/detail.dart';
  9. class FriendListItem extends StatefulWidget{
  10. final Friend friend;
  11. const FriendListItem({
  12. Key? key,
  13. required this.friend,
  14. }) : super(key: key);
  15. @override
  16. _FriendListItemState createState() => _FriendListItemState();
  17. }
  18. class _FriendListItemState extends State<FriendListItem> {
  19. @override
  20. Widget build(BuildContext context) {
  21. return GestureDetector(
  22. behavior: HitTestBehavior.opaque,
  23. onTap: () { findOrCreateConversation(context); },
  24. child: Container(
  25. padding: const EdgeInsets.only(left: 16,right: 16,top: 0,bottom: 20),
  26. child: Row(
  27. children: <Widget>[
  28. Expanded(
  29. child: Row(
  30. children: <Widget>[
  31. CustomCircleAvatar(
  32. initials: widget.friend.username[0].toUpperCase(),
  33. ),
  34. const SizedBox(width: 16),
  35. Expanded(
  36. child: Align(
  37. alignment: Alignment.centerLeft,
  38. child: Container(
  39. color: Colors.transparent,
  40. child: Column(
  41. crossAxisAlignment: CrossAxisAlignment.start,
  42. children: <Widget>[
  43. Text(widget.friend.username, style: const TextStyle(fontSize: 16)),
  44. ],
  45. ),
  46. ),
  47. ),
  48. ),
  49. ],
  50. ),
  51. ),
  52. ],
  53. ),
  54. ),
  55. );
  56. }
  57. Future<void> findOrCreateConversation(BuildContext context) async {
  58. Conversation? conversation = await getTwoUserConversation(widget.friend.friendId);
  59. conversation ??= await createConversation(
  60. generateRandomString(32),
  61. [ widget.friend ],
  62. true,
  63. );
  64. ConversationsService.uploadConversation(conversation)
  65. .catchError((dynamic d) async {
  66. showMessage('Failed to create conversation', context);
  67. });
  68. if (!mounted) {
  69. return;
  70. }
  71. Navigator.push(context, MaterialPageRoute(builder: (context){
  72. return ConversationDetail(
  73. conversation: conversation!,
  74. );
  75. }));
  76. }
  77. }