From 720ef1775f43be87b847578dc849adbaead0f8c2 Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 23 Sep 2025 19:18:53 +0530 Subject: [PATCH] refactor: Move StacAlign to Stac_core for DSL support --- .../widgets/stac_align/stac_align.dart | 22 - .../stac_align/stac_align.freezed.dart | 404 ------------------ .../widgets/stac_align/stac_align.g.dart | 40 -- .../widgets/stac_align/stac_align_parser.dart | 18 +- .../stac/lib/src/parsers/widgets/widgets.dart | 2 +- .../snack_bar/stac_snack_bar_action.g.dart | 1 + .../lib/widgets/align/stac_align.dart | 68 +++ .../lib/widgets/align/stac_align.g.dart | 41 ++ packages/stac_core/lib/widgets/widgets.dart | 1 + 9 files changed, 120 insertions(+), 477 deletions(-) delete mode 100644 packages/stac/lib/src/parsers/widgets/stac_align/stac_align.dart delete mode 100644 packages/stac/lib/src/parsers/widgets/stac_align/stac_align.freezed.dart delete mode 100644 packages/stac/lib/src/parsers/widgets/stac_align/stac_align.g.dart create mode 100644 packages/stac_core/lib/widgets/align/stac_align.dart create mode 100644 packages/stac_core/lib/widgets/align/stac_align.g.dart diff --git a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.dart b/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.dart deleted file mode 100644 index 819beaa5..00000000 --- a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:stac/src/parsers/widgets/stac_double/stac_double.dart'; -import 'package:stac_core/stac_core.dart'; - -export 'stac_align_parser.dart'; - -part 'stac_align.freezed.dart'; -part 'stac_align.g.dart'; - -@freezed -abstract class StacAlign with _$StacAlign { - const factory StacAlign({ - @Default(StacAlignmentDirectional.center) - StacAlignmentDirectional alignment, - StacDouble? widthFactor, - StacDouble? heightFactor, - Map? child, - }) = _StacAlign; - - factory StacAlign.fromJson(Map json) => - _$StacAlignFromJson(json); -} diff --git a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.freezed.dart b/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.freezed.dart deleted file mode 100644 index 68657556..00000000 --- a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.freezed.dart +++ /dev/null @@ -1,404 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND -// coverage:ignore-file -// ignore_for_file: type=lint -// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark - -part of 'stac_align.dart'; - -// ************************************************************************** -// FreezedGenerator -// ************************************************************************** - -// dart format off -T _$identity(T value) => value; - -/// @nodoc -mixin _$StacAlign { - StacAlignmentDirectional get alignment; - StacDouble? get widthFactor; - StacDouble? get heightFactor; - Map? get child; - - /// Create a copy of StacAlign - /// with the given fields replaced by the non-null parameter values. - @JsonKey(includeFromJson: false, includeToJson: false) - @pragma('vm:prefer-inline') - $StacAlignCopyWith get copyWith => - _$StacAlignCopyWithImpl(this as StacAlign, _$identity); - - /// Serializes this StacAlign to a JSON map. - Map toJson(); - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is StacAlign && - (identical(other.alignment, alignment) || - other.alignment == alignment) && - (identical(other.widthFactor, widthFactor) || - other.widthFactor == widthFactor) && - (identical(other.heightFactor, heightFactor) || - other.heightFactor == heightFactor) && - const DeepCollectionEquality().equals(other.child, child)); - } - - @JsonKey(includeFromJson: false, includeToJson: false) - @override - int get hashCode => Object.hash(runtimeType, alignment, widthFactor, - heightFactor, const DeepCollectionEquality().hash(child)); - - @override - String toString() { - return 'StacAlign(alignment: $alignment, widthFactor: $widthFactor, heightFactor: $heightFactor, child: $child)'; - } -} - -/// @nodoc -abstract mixin class $StacAlignCopyWith<$Res> { - factory $StacAlignCopyWith(StacAlign value, $Res Function(StacAlign) _then) = - _$StacAlignCopyWithImpl; - @useResult - $Res call( - {StacAlignmentDirectional alignment, - StacDouble? widthFactor, - StacDouble? heightFactor, - Map? child}); -} - -/// @nodoc -class _$StacAlignCopyWithImpl<$Res> implements $StacAlignCopyWith<$Res> { - _$StacAlignCopyWithImpl(this._self, this._then); - - final StacAlign _self; - final $Res Function(StacAlign) _then; - - /// Create a copy of StacAlign - /// with the given fields replaced by the non-null parameter values. - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? alignment = null, - Object? widthFactor = freezed, - Object? heightFactor = freezed, - Object? child = freezed, - }) { - return _then(_self.copyWith( - alignment: null == alignment - ? _self.alignment - : alignment // ignore: cast_nullable_to_non_nullable - as StacAlignmentDirectional, - widthFactor: freezed == widthFactor - ? _self.widthFactor - : widthFactor // ignore: cast_nullable_to_non_nullable - as StacDouble?, - heightFactor: freezed == heightFactor - ? _self.heightFactor - : heightFactor // ignore: cast_nullable_to_non_nullable - as StacDouble?, - child: freezed == child - ? _self.child - : child // ignore: cast_nullable_to_non_nullable - as Map?, - )); - } -} - -/// Adds pattern-matching-related methods to [StacAlign]. -extension StacAlignPatterns on StacAlign { - /// A variant of `map` that fallback to returning `orElse`. - /// - /// It is equivalent to doing: - /// ```dart - /// switch (sealedClass) { - /// case final Subclass value: - /// return ...; - /// case _: - /// return orElse(); - /// } - /// ``` - - @optionalTypeArgs - TResult maybeMap( - TResult Function(_StacAlign value)? $default, { - required TResult orElse(), - }) { - final _that = this; - switch (_that) { - case _StacAlign() when $default != null: - return $default(_that); - case _: - return orElse(); - } - } - - /// A `switch`-like method, using callbacks. - /// - /// Callbacks receives the raw object, upcasted. - /// It is equivalent to doing: - /// ```dart - /// switch (sealedClass) { - /// case final Subclass value: - /// return ...; - /// case final Subclass2 value: - /// return ...; - /// } - /// ``` - - @optionalTypeArgs - TResult map( - TResult Function(_StacAlign value) $default, - ) { - final _that = this; - switch (_that) { - case _StacAlign(): - return $default(_that); - case _: - throw StateError('Unexpected subclass'); - } - } - - /// A variant of `map` that fallback to returning `null`. - /// - /// It is equivalent to doing: - /// ```dart - /// switch (sealedClass) { - /// case final Subclass value: - /// return ...; - /// case _: - /// return null; - /// } - /// ``` - - @optionalTypeArgs - TResult? mapOrNull( - TResult? Function(_StacAlign value)? $default, - ) { - final _that = this; - switch (_that) { - case _StacAlign() when $default != null: - return $default(_that); - case _: - return null; - } - } - - /// A variant of `when` that fallback to an `orElse` callback. - /// - /// It is equivalent to doing: - /// ```dart - /// switch (sealedClass) { - /// case Subclass(:final field): - /// return ...; - /// case _: - /// return orElse(); - /// } - /// ``` - - @optionalTypeArgs - TResult maybeWhen( - TResult Function( - StacAlignmentDirectional alignment, - StacDouble? widthFactor, - StacDouble? heightFactor, - Map? child)? - $default, { - required TResult orElse(), - }) { - final _that = this; - switch (_that) { - case _StacAlign() when $default != null: - return $default(_that.alignment, _that.widthFactor, _that.heightFactor, - _that.child); - case _: - return orElse(); - } - } - - /// A `switch`-like method, using callbacks. - /// - /// As opposed to `map`, this offers destructuring. - /// It is equivalent to doing: - /// ```dart - /// switch (sealedClass) { - /// case Subclass(:final field): - /// return ...; - /// case Subclass2(:final field2): - /// return ...; - /// } - /// ``` - - @optionalTypeArgs - TResult when( - TResult Function( - StacAlignmentDirectional alignment, - StacDouble? widthFactor, - StacDouble? heightFactor, - Map? child) - $default, - ) { - final _that = this; - switch (_that) { - case _StacAlign(): - return $default(_that.alignment, _that.widthFactor, _that.heightFactor, - _that.child); - case _: - throw StateError('Unexpected subclass'); - } - } - - /// A variant of `when` that fallback to returning `null` - /// - /// It is equivalent to doing: - /// ```dart - /// switch (sealedClass) { - /// case Subclass(:final field): - /// return ...; - /// case _: - /// return null; - /// } - /// ``` - - @optionalTypeArgs - TResult? whenOrNull( - TResult? Function( - StacAlignmentDirectional alignment, - StacDouble? widthFactor, - StacDouble? heightFactor, - Map? child)? - $default, - ) { - final _that = this; - switch (_that) { - case _StacAlign() when $default != null: - return $default(_that.alignment, _that.widthFactor, _that.heightFactor, - _that.child); - case _: - return null; - } - } -} - -/// @nodoc -@JsonSerializable() -class _StacAlign implements StacAlign { - const _StacAlign( - {this.alignment = StacAlignmentDirectional.center, - this.widthFactor, - this.heightFactor, - final Map? child}) - : _child = child; - factory _StacAlign.fromJson(Map json) => - _$StacAlignFromJson(json); - - @override - @JsonKey() - final StacAlignmentDirectional alignment; - @override - final StacDouble? widthFactor; - @override - final StacDouble? heightFactor; - final Map? _child; - @override - Map? get child { - final value = _child; - if (value == null) return null; - if (_child is EqualUnmodifiableMapView) return _child; - // ignore: implicit_dynamic_type - return EqualUnmodifiableMapView(value); - } - - /// Create a copy of StacAlign - /// with the given fields replaced by the non-null parameter values. - @override - @JsonKey(includeFromJson: false, includeToJson: false) - @pragma('vm:prefer-inline') - _$StacAlignCopyWith<_StacAlign> get copyWith => - __$StacAlignCopyWithImpl<_StacAlign>(this, _$identity); - - @override - Map toJson() { - return _$StacAlignToJson( - this, - ); - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _StacAlign && - (identical(other.alignment, alignment) || - other.alignment == alignment) && - (identical(other.widthFactor, widthFactor) || - other.widthFactor == widthFactor) && - (identical(other.heightFactor, heightFactor) || - other.heightFactor == heightFactor) && - const DeepCollectionEquality().equals(other._child, _child)); - } - - @JsonKey(includeFromJson: false, includeToJson: false) - @override - int get hashCode => Object.hash(runtimeType, alignment, widthFactor, - heightFactor, const DeepCollectionEquality().hash(_child)); - - @override - String toString() { - return 'StacAlign(alignment: $alignment, widthFactor: $widthFactor, heightFactor: $heightFactor, child: $child)'; - } -} - -/// @nodoc -abstract mixin class _$StacAlignCopyWith<$Res> - implements $StacAlignCopyWith<$Res> { - factory _$StacAlignCopyWith( - _StacAlign value, $Res Function(_StacAlign) _then) = - __$StacAlignCopyWithImpl; - @override - @useResult - $Res call( - {StacAlignmentDirectional alignment, - StacDouble? widthFactor, - StacDouble? heightFactor, - Map? child}); -} - -/// @nodoc -class __$StacAlignCopyWithImpl<$Res> implements _$StacAlignCopyWith<$Res> { - __$StacAlignCopyWithImpl(this._self, this._then); - - final _StacAlign _self; - final $Res Function(_StacAlign) _then; - - /// Create a copy of StacAlign - /// with the given fields replaced by the non-null parameter values. - @override - @pragma('vm:prefer-inline') - $Res call({ - Object? alignment = null, - Object? widthFactor = freezed, - Object? heightFactor = freezed, - Object? child = freezed, - }) { - return _then(_StacAlign( - alignment: null == alignment - ? _self.alignment - : alignment // ignore: cast_nullable_to_non_nullable - as StacAlignmentDirectional, - widthFactor: freezed == widthFactor - ? _self.widthFactor - : widthFactor // ignore: cast_nullable_to_non_nullable - as StacDouble?, - heightFactor: freezed == heightFactor - ? _self.heightFactor - : heightFactor // ignore: cast_nullable_to_non_nullable - as StacDouble?, - child: freezed == child - ? _self._child - : child // ignore: cast_nullable_to_non_nullable - as Map?, - )); - } -} - -// dart format on diff --git a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.g.dart b/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.g.dart deleted file mode 100644 index 53baa5ee..00000000 --- a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align.g.dart +++ /dev/null @@ -1,40 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'stac_align.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -_StacAlign _$StacAlignFromJson(Map json) => _StacAlign( - alignment: $enumDecodeNullable( - _$StacAlignmentDirectionalEnumMap, json['alignment']) ?? - StacAlignmentDirectional.center, - widthFactor: json['widthFactor'] == null - ? null - : StacDouble.fromJson(json['widthFactor']), - heightFactor: json['heightFactor'] == null - ? null - : StacDouble.fromJson(json['heightFactor']), - child: json['child'] as Map?, - ); - -Map _$StacAlignToJson(_StacAlign instance) => - { - 'alignment': _$StacAlignmentDirectionalEnumMap[instance.alignment]!, - 'widthFactor': instance.widthFactor, - 'heightFactor': instance.heightFactor, - 'child': instance.child, - }; - -const _$StacAlignmentDirectionalEnumMap = { - StacAlignmentDirectional.topStart: 'topStart', - StacAlignmentDirectional.topCenter: 'topCenter', - StacAlignmentDirectional.topEnd: 'topEnd', - StacAlignmentDirectional.centerStart: 'centerStart', - StacAlignmentDirectional.center: 'center', - StacAlignmentDirectional.centerEnd: 'centerEnd', - StacAlignmentDirectional.bottomStart: 'bottomStart', - StacAlignmentDirectional.bottomCenter: 'bottomCenter', - StacAlignmentDirectional.bottomEnd: 'bottomEnd', -}; diff --git a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align_parser.dart b/packages/stac/lib/src/parsers/widgets/stac_align/stac_align_parser.dart index 92faefa9..edb31cdf 100644 --- a/packages/stac/lib/src/parsers/widgets/stac_align/stac_align_parser.dart +++ b/packages/stac/lib/src/parsers/widgets/stac_align/stac_align_parser.dart @@ -1,8 +1,6 @@ import 'package:flutter/material.dart'; -import 'package:stac/src/framework/framework.dart'; -import 'package:stac/src/parsers/foundation/foundation.dart'; -import 'package:stac/src/parsers/widgets/stac_align/stac_align.dart'; -import 'package:stac/src/parsers/widgets/stac_double/stac_double.dart'; +import 'package:stac/src/parsers/core/stac_widget_parser.dart'; +import 'package:stac/src/parsers/foundation/alignment/stac_alignment_directional_parser.dart'; import 'package:stac_core/stac_core.dart'; import 'package:stac_framework/stac_framework.dart'; @@ -10,18 +8,18 @@ class StacAlignParser extends StacParser { const StacAlignParser(); @override - StacAlign getModel(Map json) => StacAlign.fromJson(json); + String get type => WidgetType.align.name; @override - String get type => WidgetType.align.name; + StacAlign getModel(Map json) => StacAlign.fromJson(json); @override Widget parse(BuildContext context, StacAlign model) { return Align( - alignment: model.alignment.parse, - heightFactor: model.heightFactor?.parse, - widthFactor: model.widthFactor?.parse, - child: Stac.fromJson(model.child, context), + alignment: model.alignment?.parse ?? Alignment.center, + heightFactor: model.heightFactor, + widthFactor: model.widthFactor, + child: model.child?.parse(context), ); } } diff --git a/packages/stac/lib/src/parsers/widgets/widgets.dart b/packages/stac/lib/src/parsers/widgets/widgets.dart index 6ff2cc67..8fdbc499 100644 --- a/packages/stac/lib/src/parsers/widgets/widgets.dart +++ b/packages/stac/lib/src/parsers/widgets/widgets.dart @@ -1,7 +1,7 @@ export 'package:stac/src/parsers/theme/stac_color_scheme/stac_color_scheme.dart'; export 'package:stac/src/parsers/theme/stac_floating_action_button_theme_data/stac_floating_action_button_theme_data.dart'; export 'package:stac/src/parsers/widgets/stac_alert_dialog/stac_alert_dialog_parser.dart'; -export 'package:stac/src/parsers/widgets/stac_align/stac_align.dart'; +export 'package:stac/src/parsers/widgets/stac_align/stac_align_parser.dart'; export 'package:stac/src/parsers/widgets/stac_aspect_ratio/stac_aspect_ratio_parser.dart'; export 'package:stac/src/parsers/widgets/stac_auto_complete/stac_auto_complete_parser.dart'; export 'package:stac/src/parsers/widgets/stac_backdrop_filter/stac_backdrop_filter_parser.dart'; diff --git a/packages/stac_core/lib/actions/snack_bar/stac_snack_bar_action.g.dart b/packages/stac_core/lib/actions/snack_bar/stac_snack_bar_action.g.dart index e675ebbf..7eaacc98 100644 --- a/packages/stac_core/lib/actions/snack_bar/stac_snack_bar_action.g.dart +++ b/packages/stac_core/lib/actions/snack_bar/stac_snack_bar_action.g.dart @@ -26,4 +26,5 @@ Map _$StacSnackBarActionToJson(StacSnackBarAction instance) => 'disabledBackgroundColor': instance.disabledBackgroundColor, 'label': instance.label, 'onPressed': instance.onPressed?.toJson(), + 'actionType': instance.actionType, }; diff --git a/packages/stac_core/lib/widgets/align/stac_align.dart b/packages/stac_core/lib/widgets/align/stac_align.dart new file mode 100644 index 00000000..ff97d859 --- /dev/null +++ b/packages/stac_core/lib/widgets/align/stac_align.dart @@ -0,0 +1,68 @@ +import 'package:json_annotation/json_annotation.dart'; +import 'package:stac_core/core/converters/double_converter.dart'; +import 'package:stac_core/core/stac_widget.dart'; +import 'package:stac_core/foundation/foundation.dart'; + +part 'stac_align.g.dart'; + +/// A Stac model representing Flutter's [Align] widget. +/// +/// Aligns its child within itself and optionally sizes itself based on the +/// child's size. Supports alignment, width factor, and height factor properties. +/// +/// ```dart +/// StacAlign( +/// alignment: StacAlignmentDirectional.center, +/// widthFactor: 0.8, +/// heightFactor: 0.6, +/// child: StacText(data: 'Centered'), +/// ) +/// ``` +/// +/// ```json +/// { +/// "type": "align", +/// "alignment": "center", +/// "widthFactor": 0.8, +/// "heightFactor": 0.6, +/// "child": {"type": "text", "data": "Centered"} +/// } +/// ``` +@JsonSerializable() +class StacAlign extends StacWidget { + /// Creates a [StacAlign] with optional alignment and sizing properties. + const StacAlign({ + this.alignment, + this.widthFactor, + this.heightFactor, + this.child, + }); + + /// How to align the [child] within the align widget. + final StacAlignmentDirectional? alignment; + + /// If non-null, sets the width of this widget to the child's width + /// multiplied by this factor. + @DoubleConverter() + final double? widthFactor; + + /// If non-null, sets the height of this widget to the child's height + /// multiplied by this factor. + @DoubleConverter() + final double? heightFactor; + + /// The child widget to align. + final StacWidget? child; + + /// Widget type identifier. + @override + String get type => WidgetType.align.name; + + /// Creates a [StacAlign] from JSON. + factory StacAlign.fromJson(Map json) => + _$StacAlignFromJson(json); + + /// Converts this align widget to JSON. + @override + Map toJson() => _$StacAlignToJson(this); +} diff --git a/packages/stac_core/lib/widgets/align/stac_align.g.dart b/packages/stac_core/lib/widgets/align/stac_align.g.dart new file mode 100644 index 00000000..c3587726 --- /dev/null +++ b/packages/stac_core/lib/widgets/align/stac_align.g.dart @@ -0,0 +1,41 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'stac_align.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +StacAlign _$StacAlignFromJson(Map json) => StacAlign( + alignment: + $enumDecodeNullable( + _$StacAlignmentDirectionalEnumMap, + json['alignment'], + ) ?? + StacAlignmentDirectional.center, + widthFactor: const DoubleConverter().fromJson(json['widthFactor']), + heightFactor: const DoubleConverter().fromJson(json['heightFactor']), + child: json['child'] == null + ? null + : StacWidget.fromJson(json['child'] as Map), +); + +Map _$StacAlignToJson(StacAlign instance) => { + 'alignment': _$StacAlignmentDirectionalEnumMap[instance.alignment]!, + 'widthFactor': const DoubleConverter().toJson(instance.widthFactor), + 'heightFactor': const DoubleConverter().toJson(instance.heightFactor), + 'child': instance.child?.toJson(), + 'type': instance.type, +}; + +const _$StacAlignmentDirectionalEnumMap = { + StacAlignmentDirectional.topStart: 'topStart', + StacAlignmentDirectional.topCenter: 'topCenter', + StacAlignmentDirectional.topEnd: 'topEnd', + StacAlignmentDirectional.centerStart: 'centerStart', + StacAlignmentDirectional.center: 'center', + StacAlignmentDirectional.centerEnd: 'centerEnd', + StacAlignmentDirectional.bottomStart: 'bottomStart', + StacAlignmentDirectional.bottomCenter: 'bottomCenter', + StacAlignmentDirectional.bottomEnd: 'bottomEnd', +}; diff --git a/packages/stac_core/lib/widgets/widgets.dart b/packages/stac_core/lib/widgets/widgets.dart index ca5612a4..9ab09152 100644 --- a/packages/stac_core/lib/widgets/widgets.dart +++ b/packages/stac_core/lib/widgets/widgets.dart @@ -1,6 +1,7 @@ library; export 'alert_dialog/stac_alert_dialog.dart'; +export 'align/stac_align.dart'; export 'app_bar/stac_app_bar.dart'; export 'aspect_ratio/stac_aspect_ratio.dart'; export 'auto_complete/stac_auto_complete.dart';