Toggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage
Redirecting factory constructors
• Section: Dart
Using sealed classes with factories
aka Redirecting factory constructors
import'package:core/helper/dartz_helper.dart';import'package:core/styles.dart';import'package:core/widget/corner_property.dart';import'package:core_widget/core/display_size.dart';import'package:flutter/material.dart';// Maintainable, const-friendly sum type without unused fields
sealedclassImageSource{finalString?blurHash;finalSizesize;constImageSource({this.blurHash,requiredthis.size});DisplaySizegetdisplaySize=>DisplaySize.size(size);CornerPropertygetcorner=>kAppCorner.cornerFromSize(size);doublegetaspectRatio=>(size.width/size.height).roundToDouble();constfactoryImageSource.uri(Uriuri,{String?blurHash,requiredSizesize})=_ImageSourceUri;constfactoryImageSource.asset(Stringname,{String?blurHash,requiredSizesize})=_ImageSourceAsset;constfactoryImageSource.image(Imageimage,{String?blurHash,requiredSizesize})=_ImageSourceImage;EitherC<Uri,ImageProvider>getsrc;@overridebooloperator==(Objectother){if(identical(this,other))returntrue;if(other.runtimeType!=runtimeType)returnfalse;returnotherisImageSource&&other.src==src&&other.size==size&&other.blurHash==blurHash;}@overrideintgethashCode=>Object.hash(src,size,blurHash);}class_ImageSourceUriextendsImageSource{const_ImageSourceUri(this.uri,{super.blurHash,requiredsuper.size,});finalUriuri;@overrideEitherC<Uri,ImageProvider>getsrc=>LeftC(uri);}class_ImageSourceAssetextendsImageSource{finalStringname;const_ImageSourceAsset(this.name,{super.blurHash,requiredsuper.size,});@overrideEitherC<Uri,ImageProvider>getsrc=>RightC(AssetImage(name));}class_ImageSourceImageextendsImageSource{finalImageimage;const_ImageSourceImage(this.image,{super.blurHash,requiredsuper.size,});@overrideEitherC<Uri,ImageProvider>getsrc=>RightC(image.image);}