123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import 'package:cached_network_image/cached_network_image.dart';
- import 'package:flutter/material.dart';
- import 'package:ftrecruiter/comm/utils/image_utils.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;
- }
- }
- onClick ??= () {};
- }
- 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) {
- if (image.isEmpty || image.startsWith('http')) {
- final Widget placeholder =
- MyAssetImage(placeholderPath, height: height, width: width, fit: fit);
- return Container(
- decoration: BoxDecoration(
- border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
- borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
- ),
- child: GestureDetector(
- onTap: onClick,
- child: ClipRRect(
- borderRadius: BorderRadius.circular(cornerRadius ?? 0),
- child: CachedNetworkImage(
- imageUrl: image,
- placeholder: (_, __) => placeholder,
- errorWidget: (_, __, dynamic error) => placeholder,
- width: width,
- height: height,
- fit: fit,
- memCacheWidth: cacheWidth,
- memCacheHeight: cacheHeight,
- )),
- ));
- } else {
- return GestureDetector(
- onTap: onClick,
- child: 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) {
- return Image.asset(
- ImageUtils.getImgPath(image),
- height: height,
- width: width,
- cacheWidth: cacheWidth,
- cacheHeight: cacheHeight,
- fit: fit,
- color: color,
- /// 忽略图片语义
- excludeFromSemantics: true,
- );
- }
- }
|