diff --git a/examples/mirai_gallery/assets/json/align_example.json b/examples/mirai_gallery/assets/json/align_example.json new file mode 100644 index 00000000..eda9900c --- /dev/null +++ b/examples/mirai_gallery/assets/json/align_example.json @@ -0,0 +1,33 @@ +{ + "type": "scaffold", + "appBar": { + "type": "appBar", + "title": { + "type": "text", + "data": "Align" + } + }, + "body": { + "type": "align", + "alignment": "topEnd", + "child": { + "type": "container", + "color": "#FC5632", + "clipBehavior": "hardEdge", + "height": 250, + "width": 200, + "child": { + "type": "align", + "alignment": "bottomCenter", + "child": { + "type": "text", + "data": "Flutter", + "style": { + "fontSize": 23, + "fontWeight": "w600" + } + } + } + } + } +} \ No newline at end of file diff --git a/examples/mirai_gallery/assets/json/container_example.json b/examples/mirai_gallery/assets/json/container_example.json index 54a39de9..c0e0287e 100644 --- a/examples/mirai_gallery/assets/json/container_example.json +++ b/examples/mirai_gallery/assets/json/container_example.json @@ -35,7 +35,20 @@ "color": "#FC5632", "clipBehavior": "hardEdge", "height": 250, - "width": 200 + "width": 200, + "child": { + "type": "align", + "alignment": "bottomCenter", + "child": { + "type": "text", + "data": "Flutter", + "align": "center", + "style": { + "fontSize": 23, + "fontWeight": "w600" + } + } + } } ] } diff --git a/examples/mirai_gallery/assets/json/home_screen.json b/examples/mirai_gallery/assets/json/home_screen.json index 6117f958..f53e458d 100644 --- a/examples/mirai_gallery/assets/json/home_screen.json +++ b/examples/mirai_gallery/assets/json/home_screen.json @@ -176,6 +176,37 @@ } } }, + { + "type": "listTile", + "leading": { + "type": "icon", + "iconType": "material", + "icon": "format_align_center" + }, + "title": { + "type": "text", + "data": "Mirai Align", + "style": { + "fontSize": 21 + } + }, + "subtitle": { + "type": "text", + "data": "widget that is used to align its child within itself and optionally sizes itself based on the child’s size", + "style": { + "fontSize": 12 + } + }, + "isThreeLine": true, + "onTap": { + "actionType": "navigate", + "navigationStyle": "push", + "widgetJson": { + "type": "exampleScreen", + "assetPath": "assets/json/align_example.json" + } + } + }, { "type": "listTile", "leading": { diff --git a/packages/mirai/lib/src/framework/mirai.dart b/packages/mirai/lib/src/framework/mirai.dart index 6fec6279..be16f595 100644 --- a/packages/mirai/lib/src/framework/mirai.dart +++ b/packages/mirai/lib/src/framework/mirai.dart @@ -6,6 +6,9 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:mirai/src/action_parsers/action_parsers.dart'; import 'package:mirai/src/framework/mirai_registry.dart'; +import 'package:mirai/src/network/mirai_network.dart'; +import 'package:mirai/src/network/mirai_request.dart'; +import 'package:mirai/src/parsers/mirai_align/mirai_align_parser.dart'; import 'package:mirai/src/parsers/mirai_center/mirai_center_parser.dart'; import 'package:mirai/src/parsers/mirai_check_box_widget/mirai_check_box_widget_parser.dart'; import 'package:mirai/src/parsers/mirai_form/mirai_form_parser.dart'; @@ -63,6 +66,7 @@ class Mirai { const MiraiFlexibleParser(), const MiraiSafeAreaParser(), const MiraiSwitchParser(), + const MiraiAlignParser(), ]; static final _actionParsers = [ diff --git a/packages/mirai/lib/src/parsers/mirai_align/mirai_align.dart b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.dart new file mode 100644 index 00000000..3341a28f --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.dart @@ -0,0 +1,21 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:mirai/src/parsers/parsers.dart'; + +export 'package:mirai/src/parsers/mirai_stack/mirai_stack_parser.dart'; + +part 'mirai_align.freezed.dart'; +part 'mirai_align.g.dart'; + +@freezed +class MiraiAlign with _$MiraiAlign { + const factory MiraiAlign({ + @Default(MiraiAlignmentDirectional.center) + MiraiAlignmentDirectional alignment, + double? widthFactor, + double? heightFactor, + Map? child, + }) = _MiraiAlign; + + factory MiraiAlign.fromJson(Map json) => + _$MiraiAlignFromJson(json); +} diff --git a/packages/mirai/lib/src/parsers/mirai_align/mirai_align.freezed.dart b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.freezed.dart new file mode 100644 index 00000000..c286282e --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.freezed.dart @@ -0,0 +1,228 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// 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 'mirai_align.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +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#custom-getters-and-methods'); + +MiraiAlign _$MiraiAlignFromJson(Map json) { + return _MiraiAlign.fromJson(json); +} + +/// @nodoc +mixin _$MiraiAlign { + MiraiAlignmentDirectional get alignment => throw _privateConstructorUsedError; + double? get widthFactor => throw _privateConstructorUsedError; + double? get heightFactor => throw _privateConstructorUsedError; + Map? get child => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $MiraiAlignCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MiraiAlignCopyWith<$Res> { + factory $MiraiAlignCopyWith( + MiraiAlign value, $Res Function(MiraiAlign) then) = + _$MiraiAlignCopyWithImpl<$Res, MiraiAlign>; + @useResult + $Res call( + {MiraiAlignmentDirectional alignment, + double? widthFactor, + double? heightFactor, + Map? child}); +} + +/// @nodoc +class _$MiraiAlignCopyWithImpl<$Res, $Val extends MiraiAlign> + implements $MiraiAlignCopyWith<$Res> { + _$MiraiAlignCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? alignment = null, + Object? widthFactor = freezed, + Object? heightFactor = freezed, + Object? child = freezed, + }) { + return _then(_value.copyWith( + alignment: null == alignment + ? _value.alignment + : alignment // ignore: cast_nullable_to_non_nullable + as MiraiAlignmentDirectional, + widthFactor: freezed == widthFactor + ? _value.widthFactor + : widthFactor // ignore: cast_nullable_to_non_nullable + as double?, + heightFactor: freezed == heightFactor + ? _value.heightFactor + : heightFactor // ignore: cast_nullable_to_non_nullable + as double?, + child: freezed == child + ? _value.child + : child // ignore: cast_nullable_to_non_nullable + as Map?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$MiraiAlignImplCopyWith<$Res> + implements $MiraiAlignCopyWith<$Res> { + factory _$$MiraiAlignImplCopyWith( + _$MiraiAlignImpl value, $Res Function(_$MiraiAlignImpl) then) = + __$$MiraiAlignImplCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {MiraiAlignmentDirectional alignment, + double? widthFactor, + double? heightFactor, + Map? child}); +} + +/// @nodoc +class __$$MiraiAlignImplCopyWithImpl<$Res> + extends _$MiraiAlignCopyWithImpl<$Res, _$MiraiAlignImpl> + implements _$$MiraiAlignImplCopyWith<$Res> { + __$$MiraiAlignImplCopyWithImpl( + _$MiraiAlignImpl _value, $Res Function(_$MiraiAlignImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? alignment = null, + Object? widthFactor = freezed, + Object? heightFactor = freezed, + Object? child = freezed, + }) { + return _then(_$MiraiAlignImpl( + alignment: null == alignment + ? _value.alignment + : alignment // ignore: cast_nullable_to_non_nullable + as MiraiAlignmentDirectional, + widthFactor: freezed == widthFactor + ? _value.widthFactor + : widthFactor // ignore: cast_nullable_to_non_nullable + as double?, + heightFactor: freezed == heightFactor + ? _value.heightFactor + : heightFactor // ignore: cast_nullable_to_non_nullable + as double?, + child: freezed == child + ? _value._child + : child // ignore: cast_nullable_to_non_nullable + as Map?, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$MiraiAlignImpl implements _MiraiAlign { + const _$MiraiAlignImpl( + {this.alignment = MiraiAlignmentDirectional.center, + this.widthFactor, + this.heightFactor, + final Map? child}) + : _child = child; + + factory _$MiraiAlignImpl.fromJson(Map json) => + _$$MiraiAlignImplFromJson(json); + + @override + @JsonKey() + final MiraiAlignmentDirectional alignment; + @override + final double? widthFactor; + @override + final double? 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); + } + + @override + String toString() { + return 'MiraiAlign(alignment: $alignment, widthFactor: $widthFactor, heightFactor: $heightFactor, child: $child)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$MiraiAlignImpl && + (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(ignore: true) + @override + int get hashCode => Object.hash(runtimeType, alignment, widthFactor, + heightFactor, const DeepCollectionEquality().hash(_child)); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$MiraiAlignImplCopyWith<_$MiraiAlignImpl> get copyWith => + __$$MiraiAlignImplCopyWithImpl<_$MiraiAlignImpl>(this, _$identity); + + @override + Map toJson() { + return _$$MiraiAlignImplToJson( + this, + ); + } +} + +abstract class _MiraiAlign implements MiraiAlign { + const factory _MiraiAlign( + {final MiraiAlignmentDirectional alignment, + final double? widthFactor, + final double? heightFactor, + final Map? child}) = _$MiraiAlignImpl; + + factory _MiraiAlign.fromJson(Map json) = + _$MiraiAlignImpl.fromJson; + + @override + MiraiAlignmentDirectional get alignment; + @override + double? get widthFactor; + @override + double? get heightFactor; + @override + Map? get child; + @override + @JsonKey(ignore: true) + _$$MiraiAlignImplCopyWith<_$MiraiAlignImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/mirai/lib/src/parsers/mirai_align/mirai_align.g.dart b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.g.dart new file mode 100644 index 00000000..6b56d670 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.g.dart @@ -0,0 +1,37 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mirai_align.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$MiraiAlignImpl _$$MiraiAlignImplFromJson(Map json) => + _$MiraiAlignImpl( + alignment: $enumDecodeNullable( + _$MiraiAlignmentDirectionalEnumMap, json['alignment']) ?? + MiraiAlignmentDirectional.center, + widthFactor: (json['widthFactor'] as num?)?.toDouble(), + heightFactor: (json['heightFactor'] as num?)?.toDouble(), + child: json['child'] as Map?, + ); + +Map _$$MiraiAlignImplToJson(_$MiraiAlignImpl instance) => + { + 'alignment': _$MiraiAlignmentDirectionalEnumMap[instance.alignment]!, + 'widthFactor': instance.widthFactor, + 'heightFactor': instance.heightFactor, + 'child': instance.child, + }; + +const _$MiraiAlignmentDirectionalEnumMap = { + MiraiAlignmentDirectional.topStart: 'topStart', + MiraiAlignmentDirectional.topCenter: 'topCenter', + MiraiAlignmentDirectional.topEnd: 'topEnd', + MiraiAlignmentDirectional.centerStart: 'centerStart', + MiraiAlignmentDirectional.center: 'center', + MiraiAlignmentDirectional.centerEnd: 'centerEnd', + MiraiAlignmentDirectional.bottomStart: 'bottomStart', + MiraiAlignmentDirectional.bottomCenter: 'bottomCenter', + MiraiAlignmentDirectional.bottomEnd: 'bottomEnd', +}; diff --git a/packages/mirai/lib/src/parsers/mirai_align/mirai_align_parser.dart b/packages/mirai/lib/src/parsers/mirai_align/mirai_align_parser.dart new file mode 100644 index 00000000..0e8b1d55 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_align/mirai_align_parser.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; +import 'package:mirai/src/framework/framework.dart'; +import 'package:mirai/src/parsers/mirai_align/mirai_align.dart'; +import 'package:mirai/src/utils/widget_type.dart'; +import 'package:mirai_framework/mirai_framework.dart'; + +class MiraiAlignParser extends MiraiParser { + const MiraiAlignParser(); + + @override + MiraiAlign getModel(Map json) => MiraiAlign.fromJson(json); + + @override + String get type => WidgetType.align.name; + + @override + Widget parse(BuildContext context, MiraiAlign model) { + return Align( + alignment: model.alignment.value, + heightFactor: model.heightFactor, + widthFactor: model.widthFactor, + child: Mirai.fromJson(model.child, context), + ); + } +} diff --git a/packages/mirai/lib/src/parsers/parsers.dart b/packages/mirai/lib/src/parsers/parsers.dart index da5ecc8c..fa79f8e3 100644 --- a/packages/mirai/lib/src/parsers/parsers.dart +++ b/packages/mirai/lib/src/parsers/parsers.dart @@ -58,3 +58,4 @@ export 'package:mirai/src/parsers/mirai_text_field/mirai_text_field.dart'; export 'package:mirai/src/parsers/mirai_text_form_field/mirai_text_form_field.dart'; export 'package:mirai/src/parsers/mirai_text_style/mirai_text_style.dart'; export 'package:mirai/src/parsers/mirai_theme/mirai_theme.dart'; +export 'package:mirai/src/parsers/mirai_align/mirai_align.dart'; diff --git a/packages/mirai/lib/src/utils/widget_type.dart b/packages/mirai/lib/src/utils/widget_type.dart index c5de69a1..34457fa5 100644 --- a/packages/mirai/lib/src/utils/widget_type.dart +++ b/packages/mirai/lib/src/utils/widget_type.dart @@ -37,4 +37,5 @@ enum WidgetType { flexible, safeArea, switchButton, + align }