|
1 | | -import 'dart:typed_data'; |
2 | | - |
3 | | -import 'package:dio/dio.dart'; |
4 | | -import 'package:dio_cache_interceptor/dio_cache_interceptor.dart'; |
| 1 | +import 'package:cached_network_image/cached_network_image.dart'; |
5 | 2 | import 'package:flutter/material.dart'; |
6 | | -import 'package:flutter_riverpod/flutter_riverpod.dart'; |
7 | | -import 'package:riverpod_annotation/riverpod_annotation.dart'; |
8 | | -import 'package:tail_app/Frontend/utils.dart'; |
9 | | -import 'package:tail_app/constants.dart'; |
10 | | - |
11 | | -part 'tail_blog_image.g.dart'; |
12 | 3 |
|
13 | | -class TailBlogImage extends ConsumerStatefulWidget { |
| 4 | +class TailBlogImage extends StatelessWidget { |
14 | 5 | const TailBlogImage({required this.url, super.key}); |
15 | 6 |
|
16 | 7 | final String url; |
17 | 8 |
|
18 | | - @override |
19 | | - ConsumerState<ConsumerStatefulWidget> createState() => _TailBlogImageState(); |
20 | | -} |
21 | | - |
22 | | -class _TailBlogImageState extends ConsumerState<TailBlogImage> { |
23 | 9 | @override |
24 | 10 | Widget build(BuildContext context) { |
25 | | - return Builder( |
26 | | - builder: (context) { |
27 | | - if (widget.url != "") { |
28 | | - var snapshot = ref.watch(getBlogImageProvider(widget.url)); |
29 | | - return AnimatedOpacity( |
30 | | - duration: animationTransitionDuration, |
31 | | - opacity: snapshot.hasValue ? 1 : 0, |
32 | | - child: snapshot.hasValue ? snapshot.value! : const CircularProgressIndicator(), |
33 | | - ); |
34 | | - } else { |
35 | | - return Container(); |
36 | | - } |
37 | | - }, |
38 | | - ); |
39 | | - } |
40 | | -} |
41 | | - |
42 | | -@Riverpod() |
43 | | -Future<Widget> getBlogImage(Ref ref, String url) async { |
44 | | - if (await isLimitedDataEnvironment()) { |
45 | | - return Container(); |
46 | | - } |
47 | | - Dio dio = await initDio(); |
48 | | - |
49 | | - Response<List<int>> response = await dio.get( |
50 | | - url, |
51 | | - options: cacheOptions |
52 | | - .copyWith( |
53 | | - policy: CachePolicy.forceCache, |
54 | | - ) |
55 | | - .toOptions() |
56 | | - .copyWith( |
57 | | - responseType: ResponseType.bytes, |
58 | | - followRedirects: true, |
59 | | - ), |
60 | | - ); |
61 | | - |
62 | | - if (response.statusCode! < 400) { |
63 | | - Uint8List data = Uint8List.fromList(response.data!); |
64 | 11 | return SizedBox.expand( |
65 | 12 | child: FittedBox( |
66 | 13 | alignment: Alignment.center, |
67 | | - // TRY THIS: Try changing the fit types to see how they change the way |
68 | | - // the placeholder fits into the container. |
69 | 14 | fit: BoxFit.cover, |
70 | | - child: Image.memory( |
71 | | - data, |
72 | | - width: 300, |
| 15 | + child: CachedNetworkImage( |
| 16 | + height: 300, |
| 17 | + imageUrl: url, |
| 18 | + progressIndicatorBuilder: (context, url, downloadProgress) => SizedBox.square( |
| 19 | + dimension: 40, |
| 20 | + child: CircularProgressIndicator( |
| 21 | + value: downloadProgress.progress, |
| 22 | + ), |
| 23 | + ), |
| 24 | + errorWidget: (context, url, error) => Icon(Icons.error), |
73 | 25 | ), |
74 | 26 | ), |
75 | 27 | ); |
76 | 28 | } |
77 | | - return Container(); |
78 | 29 | } |
0 commit comments