CashedNetworkImageWidget.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'src/bloc/cashed_image_bloc.dart'
  4. show
  5. CashedImageBloc,
  6. CashedImageGetErrorState,
  7. CashedImageGetState,
  8. CashedImageState,
  9. GetStartImageEvent;
  10. class CachedNetworkImageWidget extends StatelessWidget {
  11. final String imageUrl;
  12. final int count;
  13. final BoxFit fit;
  14. final double? width;
  15. final double? height;
  16. final Widget? placeholder;
  17. final Widget? errorWidget;
  18. final bool cached;
  19. final String? cachkey;
  20. const CachedNetworkImageWidget({
  21. super.key,
  22. required this.imageUrl,
  23. required this.cachkey,
  24. required this.fit,
  25. this.count = 10,
  26. this.height,
  27. this.width,
  28. this.errorWidget,
  29. this.placeholder,
  30. this.cached = true,
  31. });
  32. @override
  33. Widget build(BuildContext context) {
  34. return BlocProvider(
  35. create: (context) => CashedImageBloc()
  36. ..add(
  37. GetStartImageEvent(
  38. url: imageUrl,
  39. cached: cached,
  40. count: count,
  41. cachkey: cachkey,
  42. ),
  43. ),
  44. child: Builder(
  45. builder: (context) {
  46. return BlocBuilder<CashedImageBloc, CashedImageState>(
  47. builder: (context, state) {
  48. if (state is CashedImageGetErrorState) {
  49. return SizedBox(
  50. width: width,
  51. height: height,
  52. child: errorWidget ?? Center(child: Icon(Icons.error)),
  53. );
  54. }
  55. if (state is CashedImageGetState) {
  56. return Image.memory(
  57. state.bytes,
  58. fit: fit,
  59. width: width,
  60. height: height,
  61. );
  62. }
  63. return SizedBox(
  64. width: width,
  65. height: height,
  66. child:
  67. placeholder ?? Center(child: CircularProgressIndicator()),
  68. );
  69. },
  70. );
  71. },
  72. ),
  73. );
  74. }
  75. }