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.

83 lines
2.6 KiB

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