aboutsummaryrefslogtreecommitdiff
path: root/lib/widgets/chat_bubble.dart
blob: 7c2e16cf5e99699d3e6f697c6c3f38b4adeca43d (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import 'package:custom_clippers/custom_clippers.dart';
import 'package:flutter/material.dart';
import 'package:linkchat/models/message.dart';
import 'package:url_launcher/link.dart';

enum ChatBubbleAlignment { start, end }

class ChatBubble extends StatelessWidget {
  final Message message;
  final ChatBubbleAlignment alignment;

  const ChatBubble(
    this.message, {
    super.key,
    required this.alignment,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        alignment == ChatBubbleAlignment.start
            ? BubbleLeft(message)
            : BubbleRight(message),
      ],
    );
  }
}

class BubbleLeft extends StatelessWidget {
  final Message message;

  const BubbleLeft(this.message, {super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(right: 77),
      child: ClipPath(
        clipper: UpperNipMessageClipper(MessageType.receive),
        child: Container(
          padding: const EdgeInsets.all(20),
          decoration: const BoxDecoration(
            color: Color(0xFFE1E1E2),
          ),
          child: Column(
            children: [
              LinkPreview(message),
              Link(
                uri: Uri.parse(message.messageText),
                builder: (context, followLink) => TextButton(
                  onPressed: followLink,
                  child: Text(
                    message.messageText,
                    style: const TextStyle(color: Colors.blue),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class BubbleRight extends StatelessWidget {
  final Message message;

  const BubbleRight(this.message, {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.centerRight,
      child: Padding(
        padding: const EdgeInsets.only(top: 20, left: 77),
        child: ClipPath(
          clipper: LowerNipMessageClipper(MessageType.send),
          child: Container(
            padding:
                const EdgeInsets.only(left: 20, top: 10, bottom: 20, right: 20),
            decoration: const BoxDecoration(
              color: Color(0xFF113753),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                LinkPreview(message),
                Link(
                  uri: Uri.parse(message.messageText),
                  builder: (context, followLink) => TextButton(
                    onPressed: followLink,
                    child: Text(
                      message.messageText,
                      style: const TextStyle(color: Colors.blue),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class LinkPreview extends StatelessWidget {
  final Message message;
  const LinkPreview(this.message, {super.key});

  @override
  Widget build(BuildContext context) {
    return (message.linkTitle != null ||
            message.linkDescription != null ||
            message.linkPhotoURL != null)
        ? Padding(
            padding: const EdgeInsets.only(bottom: 8.0),
            child: Card(
              clipBehavior: Clip.antiAliasWithSaveLayer,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  message.linkPhotoURL != null
                      ? FadeInImage(
                          placeholder: const AssetImage('assets/loading.gif'),
                          image: NetworkImage(message.linkPhotoURL!),
                          imageErrorBuilder: (context, error, stackTrace) =>
                              const SizedBox.shrink(),
                          fit: BoxFit.fill,
                        )
                      : const SizedBox.shrink(),
                  message.linkTitle != null
                      ? Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Text(
                            message.linkTitle!,
                            style: Theme.of(context)
                                .textTheme
                                .bodyLarge!
                                .copyWith(fontWeight: FontWeight.bold),
                          ),
                        )
                      : const SizedBox.shrink(),
                  message.linkDescription != null
                      ? Padding(
                          padding:
                              const EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 8.0),
                          child: Text(message.linkDescription!),
                        )
                      : const SizedBox.shrink(),
                ],
              ),
            ),
          )
        : const SizedBox.shrink();
  }
}