File size: 6,697 Bytes
5acd9c3
2e813e6
5acd9c3
 
2e813e6
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
718ff66
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
2e813e6
5acd9c3
 
 
 
 
2e813e6
5acd9c3
 
 
 
 
 
 
2e813e6
5acd9c3
 
 
 
2e813e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
2e813e6
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e15d9f5
 
f26b729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
f26b729
5acd9c3
f26b729
 
5acd9c3
 
f26b729
e15d9f5
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e15d9f5
 
 
 
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import 'package:flutter/material.dart';
import 'dart:convert';
import '../theme/colors.dart';
import '../models/video_result.dart';
import './video_player/index.dart';

class VideoCard extends StatelessWidget {
  final VideoResult video;

  const VideoCard({
    super.key,
    required this.video,
  });

  Widget _buildThumbnail() {
    if (video.thumbnailUrl.isEmpty) {
      return Container(
        color: AiTubeColors.surfaceVariant,
        child: const Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                Icons.movie_creation,
                color: AiTubeColors.onSurfaceVariant,
                size: 32,
              ),
              SizedBox(height: 8),
              Text(
                '(TODO: thumbnails)',
                style: TextStyle(
                  color: AiTubeColors.onSurfaceVariant,
                  fontSize: 12,
                ),
              ),
            ],
          ),
        ),
      );
    }

    try {
      // Handle image thumbnails
      if (video.thumbnailUrl.startsWith('data:image')) {
        final uri = Uri.parse(video.thumbnailUrl);
        final base64Data = uri.data?.contentAsBytes();
        
        if (base64Data == null) {
          debugPrint('Invalid image data in thumbnailUrl');
          throw Exception('Invalid image data');
        }

        return Image.memory(
          base64Data,
          fit: BoxFit.cover,
          errorBuilder: (context, error, stackTrace) {
            debugPrint('Error loading image thumbnail: $error');
            return _buildErrorThumbnail();
          },
        );
      }
      // Handle video thumbnails
      else if (video.thumbnailUrl.startsWith('data:video')) {
        return NanoVideoPlayer(
          video: video,
          autoPlay: true,
          muted: true,
          loop: true,
          borderRadius: 0,
          showLoadingIndicator: true,
          playbackSpeed: 0.7,
        );
      }
      // Regular URL thumbnail
      else if (video.thumbnailUrl.isNotEmpty) {
        return Image.network(
          video.thumbnailUrl,
          fit: BoxFit.cover,
          errorBuilder: (context, error, stackTrace) {
            debugPrint('Error loading network thumbnail: $error');
            return _buildErrorThumbnail();
          },
        );
      } else {
        return _buildErrorThumbnail();
      }
    } catch (e) {
      debugPrint('Unexpected error in thumbnail rendering: $e');
      return _buildErrorThumbnail();
    }
  }

  Widget _buildErrorThumbnail() {
    return Container(
      color: AiTubeColors.surfaceVariant,
      child: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.broken_image,
              color: AiTubeColors.onSurfaceVariant,
              size: 32,
            ),
            SizedBox(height: 8),
            Text(
              'Preview unavailable',
              style: TextStyle(
                color: AiTubeColors.onSurfaceVariant,
                fontSize: 12,
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      clipBehavior: Clip.antiAlias,
      margin: EdgeInsets.zero,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AspectRatio(
            aspectRatio: 16 / 9,
            child: Stack(
              fit: StackFit.expand,
              children: [
                _buildThumbnail(),
                /*
                Will be used in the future release
                Positioned(
                  right: 8,
                  top: 8,
                  child: Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 8,
                      vertical: 4,
                    ),
                    decoration: BoxDecoration(
                      color: Colors.black.withOpacity(0.7),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: const Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          'LTX Video',
                          style: TextStyle(
                            color: AiTubeColors.onBackground,
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                */
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.all(12),
            color: AiTubeColors.surface,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const CircleAvatar(
                  radius: 16,
                  backgroundColor: AiTubeColors.surfaceVariant,
                  child: Icon(
                    Icons.play_arrow,
                    color: AiTubeColors.onSurfaceVariant,
                    size: 20,
                  ),
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        video.title,
                        style: const TextStyle(
                          color: AiTubeColors.onBackground,
                          fontWeight: FontWeight.w500,
                          fontSize: 14,
                        ),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                      const SizedBox(height: 4),
                      SizedBox(
                        height: 36, // Approximately height for 3 lines of text with fontSize 12
                        child: Text(
                          video.description,
                          style: const TextStyle(
                            color: AiTubeColors.onSurfaceVariant,
                            fontSize: 12,
                          ),
                          maxLines: 3,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}