|
@@ -17,10 +17,9 @@ import 'bloc/cashed_image_bloc.dart'
|
|
|
typedef ImageWidgetBuilder =
|
|
typedef ImageWidgetBuilder =
|
|
|
Widget Function(BuildContext context, ImageProvider imageProvider);
|
|
Widget Function(BuildContext context, ImageProvider imageProvider);
|
|
|
|
|
|
|
|
-class CachedNetworkImageWidget extends StatelessWidget {
|
|
|
|
|
|
|
+class CachedNetworkImageWidget extends StatefulWidget {
|
|
|
final String imageUrl;
|
|
final String imageUrl;
|
|
|
final int count;
|
|
final int count;
|
|
|
-
|
|
|
|
|
final double? width;
|
|
final double? width;
|
|
|
final double? height;
|
|
final double? height;
|
|
|
final Widget? placeholder;
|
|
final Widget? placeholder;
|
|
@@ -35,7 +34,6 @@ class CachedNetworkImageWidget extends StatelessWidget {
|
|
|
super.key,
|
|
super.key,
|
|
|
required this.imageUrl,
|
|
required this.imageUrl,
|
|
|
required this.cacheKey,
|
|
required this.cacheKey,
|
|
|
-
|
|
|
|
|
this.count = 10,
|
|
this.count = 10,
|
|
|
this.height,
|
|
this.height,
|
|
|
this.width,
|
|
this.width,
|
|
@@ -47,42 +45,67 @@ class CachedNetworkImageWidget extends StatelessWidget {
|
|
|
required this.imageBuilder,
|
|
required this.imageBuilder,
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ @override
|
|
|
|
|
+ CachedNetworkImageWidgetState createState() =>
|
|
|
|
|
+ CachedNetworkImageWidgetState();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class CachedNetworkImageWidgetState extends State<CachedNetworkImageWidget>
|
|
|
|
|
+ with AutomaticKeepAliveClientMixin {
|
|
|
|
|
+ late final CashedImageBloc bloc;
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ void initState() {
|
|
|
|
|
+ bloc = CashedImageBloc()
|
|
|
|
|
+ ..add(
|
|
|
|
|
+ GetStartImageEvent(
|
|
|
|
|
+ httpHeaders: widget.httpHeaders,
|
|
|
|
|
+ url: widget.imageUrl,
|
|
|
|
|
+ cached: widget.cached,
|
|
|
|
|
+ count: widget.count,
|
|
|
|
|
+ cachkey: widget.cacheKey,
|
|
|
|
|
+ ),
|
|
|
|
|
+ );
|
|
|
|
|
+ super.initState();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ void dispose() {
|
|
|
|
|
+ bloc.close();
|
|
|
|
|
+ super.dispose();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ bool get wantKeepAlive => true;
|
|
|
|
|
+
|
|
|
@override
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
Widget build(BuildContext context) {
|
|
|
|
|
+ super.build(context);
|
|
|
|
|
+
|
|
|
return BlocProvider(
|
|
return BlocProvider(
|
|
|
- create: (context) => CashedImageBloc()
|
|
|
|
|
- ..add(
|
|
|
|
|
- GetStartImageEvent(
|
|
|
|
|
- httpHeaders: httpHeaders,
|
|
|
|
|
- url: imageUrl,
|
|
|
|
|
- cached: cached,
|
|
|
|
|
- count: count,
|
|
|
|
|
- cachkey: cacheKey,
|
|
|
|
|
- ),
|
|
|
|
|
- ),
|
|
|
|
|
- child: Builder(
|
|
|
|
|
- builder: (context) {
|
|
|
|
|
- return BlocBuilder<CashedImageBloc, CashedImageState>(
|
|
|
|
|
- builder: (context, state) {
|
|
|
|
|
- if (state is CashedImageGetErrorState) {
|
|
|
|
|
- return SizedBox(
|
|
|
|
|
- width: width,
|
|
|
|
|
- height: height,
|
|
|
|
|
- child: errorWidget ?? Center(child: Icon(Icons.error)),
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (state is CashedImageGetState) {
|
|
|
|
|
- ImageProvider imageProvider = MemoryImage(state.bytes);
|
|
|
|
|
- return imageBuilder(context, imageProvider);
|
|
|
|
|
- }
|
|
|
|
|
- return SizedBox(
|
|
|
|
|
- width: width,
|
|
|
|
|
- height: height,
|
|
|
|
|
- child:
|
|
|
|
|
- placeholder ?? Center(child: CircularProgressIndicator()),
|
|
|
|
|
- );
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ create: (context) => bloc,
|
|
|
|
|
+ child: BlocBuilder<CashedImageBloc, CashedImageState>(
|
|
|
|
|
+ buildWhen: (prev, curr) => prev.runtimeType != curr.runtimeType,
|
|
|
|
|
+ builder: (context, state) {
|
|
|
|
|
+ if (state is CashedImageGetErrorState) {
|
|
|
|
|
+ return SizedBox(
|
|
|
|
|
+ width: widget.width,
|
|
|
|
|
+ height: widget.height,
|
|
|
|
|
+ child: widget.errorWidget ?? Center(child: Icon(Icons.error)),
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (state is CashedImageGetState) {
|
|
|
|
|
+ ImageProvider imageProvider = MemoryImage(state.bytes);
|
|
|
|
|
+ return widget.imageBuilder(context, imageProvider);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return SizedBox(
|
|
|
|
|
+ width: widget.width,
|
|
|
|
|
+ height: widget.height,
|
|
|
|
|
+ child:
|
|
|
|
|
+ widget.placeholder ??
|
|
|
|
|
+ Center(child: widget.loadwidget ?? CircularProgressIndicator()),
|
|
|
);
|
|
);
|
|
|
},
|
|
},
|
|
|
),
|
|
),
|