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.

78 lines
2.4 KiB

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