123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- import 'dart:convert';
- import 'dart:io';
- import 'package:extended_image/extended_image.dart';
- import 'package:flutter/material.dart';
- import 'package:cs_resources/generated/assets.dart';
- import 'package:shared/utils/image_path_utils.dart';
- import 'package:shared/utils/reg_utils.dart';
- import 'package:shared/utils/util.dart';
- class MyLoadImage extends StatelessWidget {
- MyLoadImage(
- this.image, {
- Key? key,
- this.width,
- this.height,
- this.fit = BoxFit.cover,
- this.placeholderPath = '',
- this.cacheWidth,
- this.cacheHeight,
- this.isCircle,
- this.cornerRadius,
- this.borderColor,
- this.borderWidth,
- this.onClick,
- }) : super(key: key) {
- if (isCircle != null) {
- if (isCircle ?? true) {
- cornerRadius = width ?? 0 / 2;
- }
- }
- }
- final String? image;
- final double? width;
- final double? height;
- final BoxFit fit;
- final String placeholderPath;
- final int? cacheWidth;
- final int? cacheHeight;
- bool? isCircle = false;
- double? borderWidth = 0;
- Color? borderColor = Colors.transparent;
- VoidCallback? onClick;
- double? cornerRadius = 0;
- @override
- Widget build(BuildContext context) {
-
- final Widget placeholder = placeholderPath.isEmpty
- ? Container(
-
- width: width,
- height: height,
- color:const Color(0xffE0E0E0),
- alignment: Alignment.center,
- child: const MyAssetImage(
- Assets.baseLibImageDefaultPlaceholder,
- width: 38,
- height: 32,
- ),
- )
- : MyAssetImage(
-
- placeholderPath,
- height: height,
- width: width,
- fit: fit,
- );
- if (Utils.isEmpty(image) || image!.startsWith('http') || image!.startsWith('data:')) {
-
- return _buildDecorationNetImage(placeholder);
- } else if (Utils.isNotEmpty(image) && RegCheckUtils.isLocalImagePath(image!)) {
-
- return _buildDecorationFileImage();
- } else {
-
- return _buildDecorationAssetImage();
- }
- }
-
-
- Widget _buildDecorationNetImage(Widget placeholder) {
- if (cornerRadius != null && cornerRadius! > 0) {
- return Container(
- decoration: BoxDecoration(
- border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
- borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
- ),
- child: _buildGestureNetImg(placeholder),
- );
- } else {
- return _buildGestureNetImg(placeholder);
- }
- }
-
- Widget _buildGestureNetImg(Widget placeholder) {
- return onClick != null
- ? GestureDetector(
- onTap: onClick,
- child: _buildClipImg(placeholder, placeholder),
- )
- : _buildClipImg(placeholder, placeholder);
- }
-
- ClipRRect _buildClipImg(Widget placeholderWidget, Widget errorWidget) {
- if (image?.startsWith('data:') == true) {
- return ClipRRect(
- borderRadius: BorderRadius.circular(cornerRadius ?? 0),
-
- child: Image.memory(
- base64.decode(image?.replaceFirst('data:image/png;base64,', '') ?? ''),
- width: width,
- height: height,
- fit: fit,
- ));
- } else if (image?.startsWith('http') == true) {
- return ClipRRect(
- borderRadius: BorderRadius.circular(cornerRadius ?? 0),
-
- child: ExtendedImage.network(
- image ?? "",
- width: width,
- height: height,
- fit: fit,
- timeLimit: const Duration(milliseconds: 30000),
- cache: true,
-
-
- loadStateChanged: (ExtendedImageState state) {
- switch (state.extendedImageLoadState) {
- case LoadState.loading:
- return placeholderWidget;
- case LoadState.completed:
- return null;
- case LoadState.failed:
- return errorWidget;
- }
- },
- ));
- } else {
-
- return ClipRRect(
- borderRadius: BorderRadius.circular(cornerRadius ?? 0),
- child: placeholderWidget,
- );
- }
- }
-
-
- Widget _buildDecorationFileImage() {
- if (cornerRadius != null && cornerRadius! > 0) {
- return Container(
- decoration: BoxDecoration(
- border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
- borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
- ),
- child: _buildGestureFileImage(),
- );
- } else {
- return _buildGestureFileImage();
- }
- }
-
- Widget _buildGestureFileImage() {
- return onClick != null
- ? GestureDetector(
- onTap: onClick,
- child: _buildFileImage(),
- )
- : _buildFileImage();
- }
-
- Widget _buildFileImage() {
- return ClipRRect(
- borderRadius: BorderRadius.circular(cornerRadius ?? 0),
- child: Image.file(
- File(image!),
- height: height,
- width: width,
- cacheWidth: cacheWidth,
- cacheHeight: cacheHeight,
- fit: fit,
- excludeFromSemantics: true,
- ),
- );
- }
-
-
- Widget _buildDecorationAssetImage() {
- if (cornerRadius != null && cornerRadius! > 0) {
- return Container(
- decoration: BoxDecoration(
- border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
- borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
- ),
- child: _buildGestureAssetImage(),
- );
- } else {
- return _buildGestureAssetImage();
- }
- }
-
- Widget _buildGestureAssetImage() {
- return onClick != null
- ? GestureDetector(
- onTap: onClick,
- child: _buildAssetImg(),
- )
- : _buildAssetImg();
- }
-
- MyAssetImage _buildAssetImg() {
- return MyAssetImage(
- image ?? "",
- height: height,
- width: width,
- fit: fit,
- cacheWidth: cacheWidth,
- cacheHeight: cacheHeight,
- );
- }
- }
- class MyAssetImage extends StatelessWidget {
- const MyAssetImage(this.image, {Key? key, this.width, this.height, this.cacheWidth, this.cacheHeight, this.fit, this.color}) : super(key: key);
- final String image;
- final double? width;
- final double? height;
- final int? cacheWidth;
- final int? cacheHeight;
- final BoxFit? fit;
- final Color? color;
- @override
- Widget build(BuildContext context) {
- var finalPath = ImagePathUtils.getImgPath(image);
- print("finalPath:$finalPath");
- return Image.asset(
- finalPath,
- package: 'cs_resources',
- height: height,
- width: width,
- cacheWidth: cacheWidth,
- cacheHeight: cacheHeight,
- fit: fit,
- color: color,
-
- excludeFromSemantics: true,
- );
- }
- }
|