networkImageRender function

ImageRender networkImageRender({
  1. Map<String, String>? headers,
  2. String mapUrl(
    1. String?
    )?,
  3. double? width,
  4. double? height,
  5. Widget altWidget(
    1. String?
    )?,
  6. Widget loadingWidget()?,
})

Implementation

ImageRender networkImageRender({
  Map<String, String>? headers,
  String Function(String?)? mapUrl,
  double? width,
  double? height,
  Widget Function(String?)? altWidget,
  Widget Function()? loadingWidget,
}) =>
    (context, attributes, element) {
      final src = mapUrl?.call(_src(attributes)) ?? _src(attributes)!;
      precacheImage(
        NetworkImage(
          src,
          headers: headers,
        ),
        context.buildContext,
        onError: (exception, StackTrace? stackTrace) {
          context.parser.onImageError?.call(exception, stackTrace);
        },
      );
      Completer<Size> completer = Completer();
      Image image = Image.network(src, frameBuilder: (ctx, child, frame, _) {
        if (frame == null) {
          if (!completer.isCompleted) {
            completer.completeError("error");
          }
          return child;
        } else {
          return child;
        }
      });

      image.image.resolve(ImageConfiguration()).addListener(
            ImageStreamListener((ImageInfo image, bool synchronousCall) {
              var myImage = image.image;
              Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
              if (!completer.isCompleted) {
                completer.complete(size);
              }
            }, onError: (object, stacktrace) {
              if (!completer.isCompleted) {
                completer.completeError(object);
              }
            }),
          );
      return FutureBuilder<Size>(
        future: completer.future,
        builder: (BuildContext buildContext, AsyncSnapshot<Size> snapshot) {
          if (snapshot.hasData) {
            return Container(
              constraints: BoxConstraints(
                  maxWidth: width ?? _width(attributes) ?? snapshot.data!.width,
                  maxHeight:
                      (width ?? _width(attributes) ?? snapshot.data!.width) / _aspectRatio(attributes, snapshot)),
              child: AspectRatio(
                aspectRatio: _aspectRatio(attributes, snapshot),
                child: Image.network(
                  src,
                  headers: headers,
                  width: width ?? _width(attributes) ?? snapshot.data!.width,
                  height: height ?? _height(attributes),
                  frameBuilder: (ctx, child, frame, _) {
                    if (frame == null) {
                      return altWidget?.call(_alt(attributes)) ??
                          Text(_alt(attributes) ?? "", style: context.style.generateTextStyle());
                    }
                    return child;
                  },
                ),
              ),
            );
          } else if (snapshot.hasError) {
            return altWidget?.call(_alt(attributes)) ??
                Text(_alt(attributes) ?? "", style: context.style.generateTextStyle());
          } else {
            /*return loadingWidget?.call() ?? const CircularProgressIndicator();*/
            return Container();
          }
        },
      );
    };