Houshan-Basa/lib/ui/widgets/components/image/network_image.dart

167 lines
6.0 KiB
Dart

// ignore_for_file: curly_braces_in_flow_control_structures
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:hoshan/core/gen/assets.gen.dart';
import 'package:hoshan/core/services/api/dio_service.dart';
import 'package:hoshan/data/storage/shared_preferences_helper.dart';
import 'package:hoshan/ui/theme/cubit/theme_mode_cubit.dart';
import 'package:hoshan/ui/widgets/components/dialog/dialog_handler.dart';
import 'package:hoshan/ui/widgets/sections/loading/default_placeholder.dart';
import 'package:string_validator/string_validator.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
class ImageNetwork extends StatefulWidget {
final String? url;
final String? baseUrl;
final double radius;
final double? width;
final double? height;
final bool showHero;
final Widget? error;
final Widget? placeholder;
final BoxFit fit;
final Color? color;
final BlendMode? blendMode;
const ImageNetwork(
{super.key,
required this.url,
this.radius = 0,
this.showHero = false,
this.error,
this.width,
this.height,
this.fit = BoxFit.cover,
this.baseUrl,
this.placeholder,
this.color,
this.blendMode});
@override
State<ImageNetwork> createState() => _ImageNetworkState();
}
class _ImageNetworkState extends State<ImageNetwork> {
@override
Widget build(BuildContext context) {
bool inError = false;
final token = AuthTokenStorage.getToken();
if (widget.url == null)
return SizedBox(
width: widget.width,
height: widget.height,
child: ClipRRect(
borderRadius: BorderRadius.circular(widget.radius),
child: Container(
decoration:
BoxDecoration(color: Theme.of(context).colorScheme.surface),
child: (context.read<ThemeModeCubit>().isDark()
? Assets.icon.launcherIcons.houshanIconWhie
: Assets.icon.launcherIcons.houshanIconPrimary)
.image(),
),
),
);
final url =
'${widget.baseUrl ?? (widget.url!.isURL() ? '' : DioService.baseURL)}${widget.url}';
if (widget.url!.isEmpty) return const SizedBox.shrink();
return GestureDetector(
onTap: widget.showHero
? () {
if (!inError) {
DialogHandler(context: context)
.showImageHero(image: widget.url!);
}
}
: null,
child: SizedBox(
width: widget.width,
height: widget.height,
child: ClipRRect(
borderRadius: BorderRadius.circular(widget.radius),
child: widget.url!.split('.').last == 'svg'
? SvgPicture.network(
widget.url!,
fit: widget.fit,
headers: url.startsWith(DioService.baseURL)
? {
'Authorization': "Bearer $token",
}
: null,
placeholderBuilder: (context) =>
widget.placeholder ??
DefaultPlaceHolder(
child: Container(
height: MediaQuery.sizeOf(context).height,
width: MediaQuery.sizeOf(context).width,
color: Colors.white,
),
),
errorBuilder: (context, url, error) {
inError = true;
if (kDebugMode) {
print("Catch image with Url: $url Failed Error: $error");
}
return widget.error ??
AspectRatio(
aspectRatio: 1 / 1,
child: Container(
color: Theme.of(context).colorScheme.surface,
child: Icon(
Icons.image_not_supported_outlined,
color: Theme.of(context).colorScheme.onSurface,
),
),
);
},
)
: CachedNetworkImage(
fit: widget.fit,
color: widget.color,
colorBlendMode: widget.blendMode,
httpHeaders: url.startsWith(DioService.baseURL)
? {
'Authorization': "Bearer $token",
}
: null,
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
imageUrl: url,
placeholder: (context, url) =>
widget.placeholder ??
DefaultPlaceHolder(
child: Container(
height: MediaQuery.sizeOf(context).height,
width: MediaQuery.sizeOf(context).width,
color: Colors.white,
),
),
errorWidget: (context, url, error) {
inError = true;
if (kDebugMode) {
print("Catch image with Url: $url Failed Error: $error");
}
return widget.error ??
AspectRatio(
aspectRatio: 1 / 1,
child: Container(
color: Theme.of(context).colorScheme.surface,
child: Icon(
Icons.image_not_supported_outlined,
color: Theme.of(context).colorScheme.onSurface,
),
),
);
},
),
),
),
);
}
}