You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

154 lines
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. child: _buildImageWithBorder(),
  67. );
  68. } else {
  69. return _buildImageWithBorder();
  70. }
  71. }
  72. ///build the image with border and border radius style
  73. _buildImageWithBorder() {
  74. if (border != null) {
  75. return Container(
  76. decoration: BoxDecoration(
  77. border: border,
  78. borderRadius: radius,
  79. ),
  80. child: _buildImageView(),
  81. );
  82. } else {
  83. return _buildImageView();
  84. }
  85. }
  86. Widget _buildImageView() {
  87. if (svgPath != null && svgPath!.isNotEmpty) {
  88. return Container(
  89. height: height,
  90. width: width,
  91. child: SvgPicture.asset(
  92. svgPath!,
  93. height: height,
  94. width: width,
  95. fit: fit ?? BoxFit.contain,
  96. color: color,
  97. ),
  98. );
  99. } else if (file != null && file!.path.isNotEmpty) {
  100. return Image.file(
  101. file!,
  102. height: height,
  103. width: width,
  104. fit: fit ?? BoxFit.cover,
  105. color: color,
  106. );
  107. } else if (url != null && url!.isNotEmpty) {
  108. return CachedNetworkImage(
  109. height: height,
  110. width: width,
  111. fit: fit,
  112. imageUrl: url!,
  113. color: color,
  114. placeholder: (context, url) => Container(
  115. height: 30,
  116. width: 30,
  117. child: LinearProgressIndicator(
  118. color: Colors.grey.shade200,
  119. backgroundColor: Colors.grey.shade100,
  120. ),
  121. ),
  122. errorWidget: (context, url, error) => Image.asset(
  123. placeHolder,
  124. height: height,
  125. width: width,
  126. fit: fit ?? BoxFit.cover,
  127. ),
  128. );
  129. } else if (imagePath != null && imagePath!.isNotEmpty) {
  130. return Image.asset(
  131. imagePath!,
  132. height: height,
  133. width: width,
  134. fit: fit ?? BoxFit.cover,
  135. color: color,
  136. );
  137. }
  138. return SizedBox();
  139. }
  140. }