CashedNetworkImageWidget.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. final Widget? loadwidget;
  21. const CachedNetworkImageWidget({
  22. super.key,
  23. required this.imageUrl,
  24. required this.cachkey,
  25. required this.fit,
  26. this.count = 10,
  27. this.height,
  28. this.width,
  29. this.errorWidget,
  30. this.placeholder,
  31. this.cached = true,
  32. this.loadwidget = const CircularProgressIndicator(),
  33. });
  34. @override
  35. Widget build(BuildContext context) {
  36. return BlocProvider(
  37. create: (context) => CashedImageBloc()
  38. ..add(
  39. GetStartImageEvent(
  40. url: imageUrl,
  41. cached: cached,
  42. count: count,
  43. cachkey: cachkey,
  44. ),
  45. ),
  46. child: Builder(
  47. builder: (context) {
  48. return BlocBuilder<CashedImageBloc, CashedImageState>(
  49. builder: (context, state) {
  50. if (state is CashedImageGetErrorState) {
  51. return SizedBox(
  52. width: width,
  53. height: height,
  54. child: errorWidget ?? Center(child: Icon(Icons.error)),
  55. );
  56. }
  57. if (state is CashedImageGetState) {
  58. return Image.memory(
  59. state.bytes,
  60. fit: fit,
  61. width: width,
  62. height: height,
  63. );
  64. }
  65. return SizedBox(
  66. width: width,
  67. height: height,
  68. child:
  69. placeholder ?? Center(child: CircularProgressIndicator()),
  70. );
  71. },
  72. );
  73. },
  74. ),
  75. );
  76. }
  77. }