From 8c3021548692542e35b58d26d7e322e302acf3db Mon Sep 17 00:00:00 2001 From: Francesco Berardi Date: Wed, 26 Mar 2025 10:36:25 +0100 Subject: [PATCH] reactive_image_picker: Update freezed to version "3.0.0" - Bump freezed version to "3.0.0" - Use sealed class and dart pattern matching since when/map methods are removed from freezed - Updated example code --- .../example/lib/main.dart | 11 +- .../lib/src/image_file.dart | 2 +- .../lib/src/image_file.freezed.dart | 383 +++++------------- .../lib/src/selected_file_view.dart | 11 +- packages/reactive_image_picker/pubspec.yaml | 4 +- 5 files changed, 116 insertions(+), 295 deletions(-) diff --git a/packages/reactive_image_picker/example/lib/main.dart b/packages/reactive_image_picker/example/lib/main.dart index c88635f3..d2aea526 100644 --- a/packages/reactive_image_picker/example/lib/main.dart +++ b/packages/reactive_image_picker/example/lib/main.dart @@ -142,9 +142,9 @@ class MyApp extends StatelessWidget { spacing: 24, children: value .map( - (e) => e.map( - video: (_) => Text("video"), - image: (i) => SizedBox.square( + (selectedFile) => switch (selectedFile) { + SelectedFileVideo() => Text("video"), + SelectedFileImage() => SizedBox.square( dimension: 50, child: InkWell( onTap: () { @@ -153,8 +153,9 @@ class MyApp extends StatelessWidget { null, ); }, - child: ImageView(image: i))), - ), + child: ImageView( + image: selectedFile))), + }, ) .toList() ..add(IconButton( diff --git a/packages/reactive_image_picker/lib/src/image_file.dart b/packages/reactive_image_picker/lib/src/image_file.dart index 3ffdc75a..d62166d0 100644 --- a/packages/reactive_image_picker/lib/src/image_file.dart +++ b/packages/reactive_image_picker/lib/src/image_file.dart @@ -4,7 +4,7 @@ import 'package:image_picker/image_picker.dart'; part 'image_file.freezed.dart'; @freezed -class SelectedFile with _$SelectedFile { +sealed class SelectedFile with _$SelectedFile { SelectedFile._(); factory SelectedFile.video({ diff --git a/packages/reactive_image_picker/lib/src/image_file.freezed.dart b/packages/reactive_image_picker/lib/src/image_file.freezed.dart index 4c40e89d..e16b5cbf 100644 --- a/packages/reactive_image_picker/lib/src/image_file.freezed.dart +++ b/packages/reactive_image_picker/lib/src/image_file.freezed.dart @@ -1,3 +1,4 @@ +// dart format width=80 // coverage:ignore-file // GENERATED CODE - DO NOT MODIFY BY HAND // ignore_for_file: type=lint @@ -9,119 +10,55 @@ part of 'image_file.dart'; // FreezedGenerator // ************************************************************************** +// dart format off T _$identity(T value) => value; -final _privateConstructorUsedError = UnsupportedError( - 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models'); - /// @nodoc mixin _$SelectedFile { - String? get url => throw _privateConstructorUsedError; - XFile? get file => throw _privateConstructorUsedError; - @optionalTypeArgs - TResult when({ - required TResult Function(String? url, XFile? file) video, - required TResult Function(String? url, XFile? file) image, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(String? url, XFile? file)? video, - TResult? Function(String? url, XFile? file)? image, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(String? url, XFile? file)? video, - TResult Function(String? url, XFile? file)? image, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult map({ - required TResult Function(SelectedFileVideo value) video, - required TResult Function(SelectedFileImage value) image, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(SelectedFileVideo value)? video, - TResult? Function(SelectedFileImage value)? image, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeMap({ - TResult Function(SelectedFileVideo value)? video, - TResult Function(SelectedFileImage value)? image, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; + String? get url; + XFile? get file; /// Create a copy of SelectedFile /// with the given fields replaced by the non-null parameter values. @JsonKey(includeFromJson: false, includeToJson: false) + @pragma('vm:prefer-inline') $SelectedFileCopyWith get copyWith => - throw _privateConstructorUsedError; -} + _$SelectedFileCopyWithImpl( + this as SelectedFile, _$identity); -/// @nodoc -abstract class $SelectedFileCopyWith<$Res> { - factory $SelectedFileCopyWith( - SelectedFile value, $Res Function(SelectedFile) then) = - _$SelectedFileCopyWithImpl<$Res, SelectedFile>; - @useResult - $Res call({String? url, XFile? file}); -} - -/// @nodoc -class _$SelectedFileCopyWithImpl<$Res, $Val extends SelectedFile> - implements $SelectedFileCopyWith<$Res> { - _$SelectedFileCopyWithImpl(this._value, this._then); + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is SelectedFile && + (identical(other.url, url) || other.url == url) && + (identical(other.file, file) || other.file == file)); + } - // ignore: unused_field - final $Val _value; - // ignore: unused_field - final $Res Function($Val) _then; + @override + int get hashCode => Object.hash(runtimeType, url, file); - /// Create a copy of SelectedFile - /// with the given fields replaced by the non-null parameter values. - @pragma('vm:prefer-inline') @override - $Res call({ - Object? url = freezed, - Object? file = freezed, - }) { - return _then(_value.copyWith( - url: freezed == url - ? _value.url - : url // ignore: cast_nullable_to_non_nullable - as String?, - file: freezed == file - ? _value.file - : file // ignore: cast_nullable_to_non_nullable - as XFile?, - ) as $Val); + String toString() { + return 'SelectedFile(url: $url, file: $file)'; } } /// @nodoc -abstract class _$$SelectedFileVideoImplCopyWith<$Res> - implements $SelectedFileCopyWith<$Res> { - factory _$$SelectedFileVideoImplCopyWith(_$SelectedFileVideoImpl value, - $Res Function(_$SelectedFileVideoImpl) then) = - __$$SelectedFileVideoImplCopyWithImpl<$Res>; - @override +abstract mixin class $SelectedFileCopyWith<$Res> { + factory $SelectedFileCopyWith( + SelectedFile value, $Res Function(SelectedFile) _then) = + _$SelectedFileCopyWithImpl; @useResult $Res call({String? url, XFile? file}); } /// @nodoc -class __$$SelectedFileVideoImplCopyWithImpl<$Res> - extends _$SelectedFileCopyWithImpl<$Res, _$SelectedFileVideoImpl> - implements _$$SelectedFileVideoImplCopyWith<$Res> { - __$$SelectedFileVideoImplCopyWithImpl(_$SelectedFileVideoImpl _value, - $Res Function(_$SelectedFileVideoImpl) _then) - : super(_value, _then); +class _$SelectedFileCopyWithImpl<$Res> implements $SelectedFileCopyWith<$Res> { + _$SelectedFileCopyWithImpl(this._self, this._then); + + final SelectedFile _self; + final $Res Function(SelectedFile) _then; /// Create a copy of SelectedFile /// with the given fields replaced by the non-null parameter values. @@ -131,13 +68,13 @@ class __$$SelectedFileVideoImplCopyWithImpl<$Res> Object? url = freezed, Object? file = freezed, }) { - return _then(_$SelectedFileVideoImpl( + return _then(_self.copyWith( url: freezed == url - ? _value.url + ? _self.url : url // ignore: cast_nullable_to_non_nullable as String?, file: freezed == file - ? _value.file + ? _self.file : file // ignore: cast_nullable_to_non_nullable as XFile?, )); @@ -146,24 +83,27 @@ class __$$SelectedFileVideoImplCopyWithImpl<$Res> /// @nodoc -class _$SelectedFileVideoImpl extends SelectedFileVideo { - _$SelectedFileVideoImpl({this.url, this.file}) : super._(); +class SelectedFileVideo extends SelectedFile { + SelectedFileVideo({this.url, this.file}) : super._(); @override final String? url; @override final XFile? file; + /// Create a copy of SelectedFile + /// with the given fields replaced by the non-null parameter values. @override - String toString() { - return 'SelectedFile.video(url: $url, file: $file)'; - } + @JsonKey(includeFromJson: false, includeToJson: false) + @pragma('vm:prefer-inline') + $SelectedFileVideoCopyWith get copyWith => + _$SelectedFileVideoCopyWithImpl(this, _$identity); @override bool operator ==(Object other) { return identical(this, other) || (other.runtimeType == runtimeType && - other is _$SelectedFileVideoImpl && + other is SelectedFileVideo && (identical(other.url, url) || other.url == url) && (identical(other.file, file) || other.file == file)); } @@ -171,130 +111,46 @@ class _$SelectedFileVideoImpl extends SelectedFileVideo { @override int get hashCode => Object.hash(runtimeType, url, file); - /// Create a copy of SelectedFile - /// with the given fields replaced by the non-null parameter values. - @JsonKey(includeFromJson: false, includeToJson: false) - @override - @pragma('vm:prefer-inline') - _$$SelectedFileVideoImplCopyWith<_$SelectedFileVideoImpl> get copyWith => - __$$SelectedFileVideoImplCopyWithImpl<_$SelectedFileVideoImpl>( - this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(String? url, XFile? file) video, - required TResult Function(String? url, XFile? file) image, - }) { - return video(url, file); - } - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(String? url, XFile? file)? video, - TResult? Function(String? url, XFile? file)? image, - }) { - return video?.call(url, file); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(String? url, XFile? file)? video, - TResult Function(String? url, XFile? file)? image, - required TResult orElse(), - }) { - if (video != null) { - return video(url, file); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(SelectedFileVideo value) video, - required TResult Function(SelectedFileImage value) image, - }) { - return video(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(SelectedFileVideo value)? video, - TResult? Function(SelectedFileImage value)? image, - }) { - return video?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(SelectedFileVideo value)? video, - TResult Function(SelectedFileImage value)? image, - required TResult orElse(), - }) { - if (video != null) { - return video(this); - } - return orElse(); + String toString() { + return 'SelectedFile.video(url: $url, file: $file)'; } } -abstract class SelectedFileVideo extends SelectedFile { - factory SelectedFileVideo({final String? url, final XFile? file}) = - _$SelectedFileVideoImpl; - SelectedFileVideo._() : super._(); - - @override - String? get url; - @override - XFile? get file; - - /// Create a copy of SelectedFile - /// with the given fields replaced by the non-null parameter values. - @override - @JsonKey(includeFromJson: false, includeToJson: false) - _$$SelectedFileVideoImplCopyWith<_$SelectedFileVideoImpl> get copyWith => - throw _privateConstructorUsedError; -} - /// @nodoc -abstract class _$$SelectedFileImageImplCopyWith<$Res> +abstract mixin class $SelectedFileVideoCopyWith<$Res> implements $SelectedFileCopyWith<$Res> { - factory _$$SelectedFileImageImplCopyWith(_$SelectedFileImageImpl value, - $Res Function(_$SelectedFileImageImpl) then) = - __$$SelectedFileImageImplCopyWithImpl<$Res>; + factory $SelectedFileVideoCopyWith( + SelectedFileVideo value, $Res Function(SelectedFileVideo) _then) = + _$SelectedFileVideoCopyWithImpl; @override @useResult $Res call({String? url, XFile? file}); } /// @nodoc -class __$$SelectedFileImageImplCopyWithImpl<$Res> - extends _$SelectedFileCopyWithImpl<$Res, _$SelectedFileImageImpl> - implements _$$SelectedFileImageImplCopyWith<$Res> { - __$$SelectedFileImageImplCopyWithImpl(_$SelectedFileImageImpl _value, - $Res Function(_$SelectedFileImageImpl) _then) - : super(_value, _then); +class _$SelectedFileVideoCopyWithImpl<$Res> + implements $SelectedFileVideoCopyWith<$Res> { + _$SelectedFileVideoCopyWithImpl(this._self, this._then); + + final SelectedFileVideo _self; + final $Res Function(SelectedFileVideo) _then; /// Create a copy of SelectedFile /// with the given fields replaced by the non-null parameter values. - @pragma('vm:prefer-inline') @override + @pragma('vm:prefer-inline') $Res call({ Object? url = freezed, Object? file = freezed, }) { - return _then(_$SelectedFileImageImpl( + return _then(SelectedFileVideo( url: freezed == url - ? _value.url + ? _self.url : url // ignore: cast_nullable_to_non_nullable as String?, file: freezed == file - ? _value.file + ? _self.file : file // ignore: cast_nullable_to_non_nullable as XFile?, )); @@ -303,24 +159,27 @@ class __$$SelectedFileImageImplCopyWithImpl<$Res> /// @nodoc -class _$SelectedFileImageImpl extends SelectedFileImage { - _$SelectedFileImageImpl({this.url, this.file}) : super._(); +class SelectedFileImage extends SelectedFile { + SelectedFileImage({this.url, this.file}) : super._(); @override final String? url; @override final XFile? file; + /// Create a copy of SelectedFile + /// with the given fields replaced by the non-null parameter values. @override - String toString() { - return 'SelectedFile.image(url: $url, file: $file)'; - } + @JsonKey(includeFromJson: false, includeToJson: false) + @pragma('vm:prefer-inline') + $SelectedFileImageCopyWith get copyWith => + _$SelectedFileImageCopyWithImpl(this, _$identity); @override bool operator ==(Object other) { return identical(this, other) || (other.runtimeType == runtimeType && - other is _$SelectedFileImageImpl && + other is SelectedFileImage && (identical(other.url, url) || other.url == url) && (identical(other.file, file) || other.file == file)); } @@ -328,92 +187,50 @@ class _$SelectedFileImageImpl extends SelectedFileImage { @override int get hashCode => Object.hash(runtimeType, url, file); - /// Create a copy of SelectedFile - /// with the given fields replaced by the non-null parameter values. - @JsonKey(includeFromJson: false, includeToJson: false) - @override - @pragma('vm:prefer-inline') - _$$SelectedFileImageImplCopyWith<_$SelectedFileImageImpl> get copyWith => - __$$SelectedFileImageImplCopyWithImpl<_$SelectedFileImageImpl>( - this, _$identity); - @override - @optionalTypeArgs - TResult when({ - required TResult Function(String? url, XFile? file) video, - required TResult Function(String? url, XFile? file) image, - }) { - return image(url, file); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(String? url, XFile? file)? video, - TResult? Function(String? url, XFile? file)? image, - }) { - return image?.call(url, file); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(String? url, XFile? file)? video, - TResult Function(String? url, XFile? file)? image, - required TResult orElse(), - }) { - if (image != null) { - return image(url, file); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(SelectedFileVideo value) video, - required TResult Function(SelectedFileImage value) image, - }) { - return image(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(SelectedFileVideo value)? video, - TResult? Function(SelectedFileImage value)? image, - }) { - return image?.call(this); + String toString() { + return 'SelectedFile.image(url: $url, file: $file)'; } +} +/// @nodoc +abstract mixin class $SelectedFileImageCopyWith<$Res> + implements $SelectedFileCopyWith<$Res> { + factory $SelectedFileImageCopyWith( + SelectedFileImage value, $Res Function(SelectedFileImage) _then) = + _$SelectedFileImageCopyWithImpl; @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(SelectedFileVideo value)? video, - TResult Function(SelectedFileImage value)? image, - required TResult orElse(), - }) { - if (image != null) { - return image(this); - } - return orElse(); - } + @useResult + $Res call({String? url, XFile? file}); } -abstract class SelectedFileImage extends SelectedFile { - factory SelectedFileImage({final String? url, final XFile? file}) = - _$SelectedFileImageImpl; - SelectedFileImage._() : super._(); +/// @nodoc +class _$SelectedFileImageCopyWithImpl<$Res> + implements $SelectedFileImageCopyWith<$Res> { + _$SelectedFileImageCopyWithImpl(this._self, this._then); - @override - String? get url; - @override - XFile? get file; + final SelectedFileImage _self; + final $Res Function(SelectedFileImage) _then; /// Create a copy of SelectedFile /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(includeFromJson: false, includeToJson: false) - _$$SelectedFileImageImplCopyWith<_$SelectedFileImageImpl> get copyWith => - throw _privateConstructorUsedError; + @pragma('vm:prefer-inline') + $Res call({ + Object? url = freezed, + Object? file = freezed, + }) { + return _then(SelectedFileImage( + url: freezed == url + ? _self.url + : url // ignore: cast_nullable_to_non_nullable + as String?, + file: freezed == file + ? _self.file + : file // ignore: cast_nullable_to_non_nullable + as XFile?, + )); + } } + +// dart format on diff --git a/packages/reactive_image_picker/lib/src/selected_file_view.dart b/packages/reactive_image_picker/lib/src/selected_file_view.dart index 63e3080d..2f610965 100644 --- a/packages/reactive_image_picker/lib/src/selected_file_view.dart +++ b/packages/reactive_image_picker/lib/src/selected_file_view.dart @@ -33,15 +33,18 @@ class SelectedFileView extends StatelessWidget { @override Widget build(BuildContext context) { + final selectedFile = file; return Column( mainAxisAlignment: MainAxisAlignment.start, children: [ SizedBox.fromSize( size: mediaSize ?? const Size(double.infinity, 250), - child: file.map( - video: (v) => selectedVideoBuilder?.call(v) ?? VideoView(video: v), - image: (i) => selectedImageBuilder?.call(i) ?? ImageView(image: i), - ), + child: switch (selectedFile) { + SelectedFileVideo() => selectedVideoBuilder?.call(selectedFile) ?? + VideoView(video: selectedFile), + SelectedFileImage() => selectedImageBuilder?.call(selectedFile) ?? + ImageView(image: selectedFile), + }, ), Row( mainAxisAlignment: MainAxisAlignment.end, diff --git a/packages/reactive_image_picker/pubspec.yaml b/packages/reactive_image_picker/pubspec.yaml index 443310ea..0e3a9e03 100644 --- a/packages/reactive_image_picker/pubspec.yaml +++ b/packages/reactive_image_picker/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: flutter: sdk: flutter reactive_forms: ">=16.0.0 <18.0.0" - freezed_annotation: ^2.4.4 + freezed_annotation: ^3.0.0 image_picker: ^1.1.2 app_settings: ^5.1.1 video_player: ^2.9.2 @@ -20,7 +20,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - freezed: ^2.5.7 + freezed: ^3.0.0 build_runner: ^2.4.13 flutter_lints: ^5.0.0