Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

155 строки
3.6 KiB

  1. // ignore_for_file: must_be_immutable
  2. import 'dart:io';
  3. import 'package:cached_network_image/cached_network_image.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_svg/flutter_svg.dart';
  6. class CustomImageView extends StatelessWidget {
  7. ///[url] is required parameter for fetching network image
  8. String? url;
  9. ///[imagePath] is required parameter for showing png,jpg,etc image
  10. String? imagePath;
  11. ///[svgPath] is required parameter for showing svg image
  12. String? svgPath;
  13. ///[file] is required parameter for fetching image file
  14. File? file;
  15. double? height;
  16. double? width;
  17. Color? color;
  18. BoxFit? fit;
  19. final String placeHolder;
  20. Alignment? alignment;
  21. VoidCallback? onTap;
  22. EdgeInsetsGeometry? margin;
  23. BorderRadius? radius;
  24. BoxBorder? border;
  25. ///a [CustomImageView] it can be used for showing any type of images
  26. /// it will shows the placeholder image if image is not found on network image
  27. CustomImageView({
  28. this.url,
  29. this.imagePath,
  30. this.svgPath,
  31. this.file,
  32. this.height,
  33. this.width,
  34. this.color,
  35. this.fit,
  36. this.alignment,
  37. this.onTap,
  38. this.radius,
  39. this.margin,
  40. this.border,
  41. this.placeHolder = 'assets/images/image_not_found.png',
  42. });
  43. @override
  44. Widget build(BuildContext context) {
  45. return alignment != null
  46. ? Align(
  47. alignment: alignment!,
  48. child: _buildWidget(),
  49. )
  50. : _buildWidget();
  51. }
  52. Widget _buildWidget() {
  53. return Padding(
  54. padding: margin ?? EdgeInsets.zero,
  55. child: InkWell(
  56. onTap: onTap,
  57. child: _buildCircleImage(),
  58. ),
  59. );
  60. }
  61. ///build the image with border radius
  62. _buildCircleImage() {
  63. if (radius != null) {
  64. return ClipRRect(
  65. // borderRadius: radius,
  66. borderRadius: radius ?? BorderRadius.zero,
  67. child: _buildImageWithBorder(),
  68. );
  69. } else {
  70. return _buildImageWithBorder();
  71. }
  72. }
  73. ///build the image with border and border radius style
  74. _buildImageWithBorder() {
  75. if (border != null) {
  76. return Container(
  77. decoration: BoxDecoration(
  78. border: border,
  79. borderRadius: radius,
  80. ),
  81. child: _buildImageView(),
  82. );
  83. } else {
  84. return _buildImageView();
  85. }
  86. }
  87. Widget _buildImageView() {
  88. if (svgPath != null && svgPath!.isNotEmpty) {
  89. return Container(
  90. height: height,
  91. width: width,
  92. child: SvgPicture.asset(
  93. svgPath!,
  94. height: height,
  95. width: width,
  96. fit: fit ?? BoxFit.contain,
  97. color: color,
  98. ),
  99. );
  100. } else if (file != null && file!.path.isNotEmpty) {
  101. return Image.file(
  102. file!,
  103. height: height,
  104. width: width,
  105. fit: fit ?? BoxFit.cover,
  106. color: color,
  107. );
  108. } else if (url != null && url!.isNotEmpty) {
  109. return CachedNetworkImage(
  110. height: height,
  111. width: width,
  112. fit: fit,
  113. imageUrl: url!,
  114. color: color,
  115. placeholder: (context, url) => Container(
  116. height: 30,
  117. width: 30,
  118. child: LinearProgressIndicator(
  119. color: Colors.grey.shade200,
  120. backgroundColor: Colors.grey.shade100,
  121. ),
  122. ),
  123. errorWidget: (context, url, error) => Image.asset(
  124. placeHolder,
  125. height: height,
  126. width: width,
  127. fit: fit ?? BoxFit.cover,
  128. ),
  129. );
  130. } else if (imagePath != null && imagePath!.isNotEmpty) {
  131. return Image.asset(
  132. imagePath!,
  133. height: height,
  134. width: width,
  135. fit: fit ?? BoxFit.cover,
  136. color: color,
  137. );
  138. }
  139. return SizedBox();
  140. }
  141. }