From a5fdd3c152555cfa20a4e2edd0e00c4b39d0356a Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Wed, 25 Dec 2024 01:17:20 +0530 Subject: [PATCH] feat: Add Mirai carousel view --- .../assets/json/carousel_view_example.json | 127 +++++ .../assets/json/home_screen.json | 31 ++ packages/mirai/lib/src/framework/mirai.dart | 1 + .../mirai_carousel_view.dart | 36 ++ .../mirai_carousel_view.freezed.dart | 507 ++++++++++++++++++ .../mirai_carousel_view.g.dart | 65 +++ .../mirai_carousel_view_parser.dart | 57 ++ packages/mirai/lib/src/parsers/parsers.dart | 1 + packages/mirai/lib/src/utils/widget_type.dart | 1 + 9 files changed, 826 insertions(+) create mode 100644 examples/mirai_gallery/assets/json/carousel_view_example.json create mode 100644 packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.freezed.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.g.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view_parser.dart diff --git a/examples/mirai_gallery/assets/json/carousel_view_example.json b/examples/mirai_gallery/assets/json/carousel_view_example.json new file mode 100644 index 00000000..b235b8b9 --- /dev/null +++ b/examples/mirai_gallery/assets/json/carousel_view_example.json @@ -0,0 +1,127 @@ +{ + "type": "scaffold", + "appBar": { + "type": "appBar", + "title": {"type": "text", "data": "Carousel View"} + }, + "body": { + "type": "listView", + "children": [ + { + "type": "container", + "height": 400, + "child": { + "type": "carouselView", + "padding": 12, + "carouselType": "weighted", + "itemSnapping": true, + "flexWeights": [1, 7, 1], + "children": [ + { + "type": "image", + "height": 400, + "fit": "cover", + "src": + "https://flutter.github.io/assets-for-api-docs/assets/material/content_based_color_scheme_1.png" + }, + { + "type": "image", + "height": 400, + "fit": "cover", + "src": + "https://flutter.github.io/assets-for-api-docs/assets/material/content_based_color_scheme_2.png" + }, + { + "type": "image", + "height": 400, + "fit": "cover", + "src": + "https://flutter.github.io/assets-for-api-docs/assets/material/content_based_color_scheme_3.png" + }, + { + "type": "image", + "height": 400, + "fit": "cover", + "src": + "https://flutter.github.io/assets-for-api-docs/assets/material/content_based_color_scheme_4.png" + }, + { + "type": "image", + "height": 400, + "fit": "cover", + "src": + "https://flutter.github.io/assets-for-api-docs/assets/material/content_based_color_scheme_5.png" + }, + { + "type": "image", + "height": 400, + "fit": "cover", + "src": + "https://flutter.github.io/assets-for-api-docs/assets/material/content_based_color_scheme_6.png" + } + ] + } + }, + { + "type": "container", + "height": 200, + "child": { + "type": "carouselView", + "itemExtent": 300, + "shrinkExtent": 80, + "padding": 12, + "children": [ + { + "type": "container", + "color": "#FFCDD2", + "child": { + "type": "center", + "child": { + "type": "text", + "data": "Show 0", + "style": { + "color": "#FFFFFF", + "fontSize": 20, + "fontWeight": "w400" + } + } + } + }, + { + "type": "container", + "color": "#C8E6C9", + "child": { + "type": "center", + "child": { + "type": "text", + "data": "Show 1", + "style": { + "color": "#FFFFFF", + "fontSize": 20, + "fontWeight": "w400" + } + } + } + }, + { + "type": "container", + "color": "#BBDEFB", + "child": { + "type": "center", + "child": { + "type": "text", + "data": "Show 2", + "style": { + "color": "#FFFFFF", + "fontSize": 20, + "fontWeight": "w400" + } + } + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/examples/mirai_gallery/assets/json/home_screen.json b/examples/mirai_gallery/assets/json/home_screen.json index 45cd5df3..f2250a64 100644 --- a/examples/mirai_gallery/assets/json/home_screen.json +++ b/examples/mirai_gallery/assets/json/home_screen.json @@ -154,6 +154,37 @@ } } }, + { + "type": "listTile", + "leading": { + "type": "icon", + "iconType": "material", + "icon": "view_carousel" + }, + "title": { + "type": "text", + "data": "Mirai Carousel View", + "style": { + "fontSize": 21 + } + }, + "subtitle": { + "type": "text", + "data": "The CarouselView presents a scrollable list of items, each of which can dynamically change size based on the chosen layout.", + "style": { + "fontSize": 12 + } + }, + "isThreeLine": true, + "onTap": { + "actionType": "navigate", + "navigationStyle": "push", + "widgetJson": { + "type": "exampleScreen", + "assetPath": "assets/json/carousel_view_example.json" + } + } + }, { "type": "listTile", "leading": { diff --git a/packages/mirai/lib/src/framework/mirai.dart b/packages/mirai/lib/src/framework/mirai.dart index 6ae319d0..bc3a4474 100644 --- a/packages/mirai/lib/src/framework/mirai.dart +++ b/packages/mirai/lib/src/framework/mirai.dart @@ -80,6 +80,7 @@ class Mirai { const MiraiAutoCompleteParser(), const MiraiTableParser(), const MiraiTableCellParser(), + const MiraiCarouselViewParser(), ]; static final _actionParsers = [ diff --git a/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.dart b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.dart new file mode 100644 index 00000000..69c9eb4f --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.dart @@ -0,0 +1,36 @@ +import 'package:flutter/material.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:mirai/mirai.dart'; + +export 'package:mirai/src/parsers/mirai_carousel_view/mirai_carousel_view_parser.dart'; + +part 'mirai_carousel_view.freezed.dart'; +part 'mirai_carousel_view.g.dart'; + +enum MiraiCarouselViewType { + regular, + weighted, +} + +@freezed +class MiraiCarouselView with _$MiraiCarouselView { + const factory MiraiCarouselView({ + @Default(MiraiCarouselViewType.regular) MiraiCarouselViewType carouselType, + MiraiEdgeInsets? padding, + String? backgroundColor, + double? elevation, + String? overlayColor, + @Default(false) bool itemSnapping, + @Default(0.0) double shrinkExtent, + @Default(Axis.horizontal) Axis scrollDirection, + @Default(false) bool reverse, + Map? onTap, + @Default(true) bool enableSplash, + double? itemExtent, + List? flexWeights, + required List>? children, + }) = _MiraiCarouselView; + + factory MiraiCarouselView.fromJson(Map json) => + _$MiraiCarouselViewFromJson(json); +} diff --git a/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.freezed.dart b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.freezed.dart new file mode 100644 index 00000000..027397b5 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.freezed.dart @@ -0,0 +1,507 @@ +// 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_carousel_view.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#adding-getters-and-methods-to-our-models'); + +MiraiCarouselView _$MiraiCarouselViewFromJson(Map json) { + return _MiraiCarouselView.fromJson(json); +} + +/// @nodoc +mixin _$MiraiCarouselView { + MiraiCarouselViewType get carouselType => throw _privateConstructorUsedError; + MiraiEdgeInsets? get padding => throw _privateConstructorUsedError; + String? get backgroundColor => throw _privateConstructorUsedError; + double? get elevation => throw _privateConstructorUsedError; + String? get overlayColor => throw _privateConstructorUsedError; + bool get itemSnapping => throw _privateConstructorUsedError; + double get shrinkExtent => throw _privateConstructorUsedError; + Axis get scrollDirection => throw _privateConstructorUsedError; + bool get reverse => throw _privateConstructorUsedError; + Map? get onTap => throw _privateConstructorUsedError; + bool get enableSplash => throw _privateConstructorUsedError; + double? get itemExtent => throw _privateConstructorUsedError; + List? get flexWeights => throw _privateConstructorUsedError; + List>? get children => + throw _privateConstructorUsedError; + + /// Serializes this MiraiCarouselView to a JSON map. + Map toJson() => throw _privateConstructorUsedError; + + /// Create a copy of MiraiCarouselView + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + $MiraiCarouselViewCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MiraiCarouselViewCopyWith<$Res> { + factory $MiraiCarouselViewCopyWith( + MiraiCarouselView value, $Res Function(MiraiCarouselView) then) = + _$MiraiCarouselViewCopyWithImpl<$Res, MiraiCarouselView>; + @useResult + $Res call( + {MiraiCarouselViewType carouselType, + MiraiEdgeInsets? padding, + String? backgroundColor, + double? elevation, + String? overlayColor, + bool itemSnapping, + double shrinkExtent, + Axis scrollDirection, + bool reverse, + Map? onTap, + bool enableSplash, + double? itemExtent, + List? flexWeights, + List>? children}); + + $MiraiEdgeInsetsCopyWith<$Res>? get padding; +} + +/// @nodoc +class _$MiraiCarouselViewCopyWithImpl<$Res, $Val extends MiraiCarouselView> + implements $MiraiCarouselViewCopyWith<$Res> { + _$MiraiCarouselViewCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + /// Create a copy of MiraiCarouselView + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? carouselType = null, + Object? padding = freezed, + Object? backgroundColor = freezed, + Object? elevation = freezed, + Object? overlayColor = freezed, + Object? itemSnapping = null, + Object? shrinkExtent = null, + Object? scrollDirection = null, + Object? reverse = null, + Object? onTap = freezed, + Object? enableSplash = null, + Object? itemExtent = freezed, + Object? flexWeights = freezed, + Object? children = freezed, + }) { + return _then(_value.copyWith( + carouselType: null == carouselType + ? _value.carouselType + : carouselType // ignore: cast_nullable_to_non_nullable + as MiraiCarouselViewType, + padding: freezed == padding + ? _value.padding + : padding // ignore: cast_nullable_to_non_nullable + as MiraiEdgeInsets?, + backgroundColor: freezed == backgroundColor + ? _value.backgroundColor + : backgroundColor // ignore: cast_nullable_to_non_nullable + as String?, + elevation: freezed == elevation + ? _value.elevation + : elevation // ignore: cast_nullable_to_non_nullable + as double?, + overlayColor: freezed == overlayColor + ? _value.overlayColor + : overlayColor // ignore: cast_nullable_to_non_nullable + as String?, + itemSnapping: null == itemSnapping + ? _value.itemSnapping + : itemSnapping // ignore: cast_nullable_to_non_nullable + as bool, + shrinkExtent: null == shrinkExtent + ? _value.shrinkExtent + : shrinkExtent // ignore: cast_nullable_to_non_nullable + as double, + scrollDirection: null == scrollDirection + ? _value.scrollDirection + : scrollDirection // ignore: cast_nullable_to_non_nullable + as Axis, + reverse: null == reverse + ? _value.reverse + : reverse // ignore: cast_nullable_to_non_nullable + as bool, + onTap: freezed == onTap + ? _value.onTap + : onTap // ignore: cast_nullable_to_non_nullable + as Map?, + enableSplash: null == enableSplash + ? _value.enableSplash + : enableSplash // ignore: cast_nullable_to_non_nullable + as bool, + itemExtent: freezed == itemExtent + ? _value.itemExtent + : itemExtent // ignore: cast_nullable_to_non_nullable + as double?, + flexWeights: freezed == flexWeights + ? _value.flexWeights + : flexWeights // ignore: cast_nullable_to_non_nullable + as List?, + children: freezed == children + ? _value.children + : children // ignore: cast_nullable_to_non_nullable + as List>?, + ) as $Val); + } + + /// Create a copy of MiraiCarouselView + /// with the given fields replaced by the non-null parameter values. + @override + @pragma('vm:prefer-inline') + $MiraiEdgeInsetsCopyWith<$Res>? get padding { + if (_value.padding == null) { + return null; + } + + return $MiraiEdgeInsetsCopyWith<$Res>(_value.padding!, (value) { + return _then(_value.copyWith(padding: value) as $Val); + }); + } +} + +/// @nodoc +abstract class _$$MiraiCarouselViewImplCopyWith<$Res> + implements $MiraiCarouselViewCopyWith<$Res> { + factory _$$MiraiCarouselViewImplCopyWith(_$MiraiCarouselViewImpl value, + $Res Function(_$MiraiCarouselViewImpl) then) = + __$$MiraiCarouselViewImplCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {MiraiCarouselViewType carouselType, + MiraiEdgeInsets? padding, + String? backgroundColor, + double? elevation, + String? overlayColor, + bool itemSnapping, + double shrinkExtent, + Axis scrollDirection, + bool reverse, + Map? onTap, + bool enableSplash, + double? itemExtent, + List? flexWeights, + List>? children}); + + @override + $MiraiEdgeInsetsCopyWith<$Res>? get padding; +} + +/// @nodoc +class __$$MiraiCarouselViewImplCopyWithImpl<$Res> + extends _$MiraiCarouselViewCopyWithImpl<$Res, _$MiraiCarouselViewImpl> + implements _$$MiraiCarouselViewImplCopyWith<$Res> { + __$$MiraiCarouselViewImplCopyWithImpl(_$MiraiCarouselViewImpl _value, + $Res Function(_$MiraiCarouselViewImpl) _then) + : super(_value, _then); + + /// Create a copy of MiraiCarouselView + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? carouselType = null, + Object? padding = freezed, + Object? backgroundColor = freezed, + Object? elevation = freezed, + Object? overlayColor = freezed, + Object? itemSnapping = null, + Object? shrinkExtent = null, + Object? scrollDirection = null, + Object? reverse = null, + Object? onTap = freezed, + Object? enableSplash = null, + Object? itemExtent = freezed, + Object? flexWeights = freezed, + Object? children = freezed, + }) { + return _then(_$MiraiCarouselViewImpl( + carouselType: null == carouselType + ? _value.carouselType + : carouselType // ignore: cast_nullable_to_non_nullable + as MiraiCarouselViewType, + padding: freezed == padding + ? _value.padding + : padding // ignore: cast_nullable_to_non_nullable + as MiraiEdgeInsets?, + backgroundColor: freezed == backgroundColor + ? _value.backgroundColor + : backgroundColor // ignore: cast_nullable_to_non_nullable + as String?, + elevation: freezed == elevation + ? _value.elevation + : elevation // ignore: cast_nullable_to_non_nullable + as double?, + overlayColor: freezed == overlayColor + ? _value.overlayColor + : overlayColor // ignore: cast_nullable_to_non_nullable + as String?, + itemSnapping: null == itemSnapping + ? _value.itemSnapping + : itemSnapping // ignore: cast_nullable_to_non_nullable + as bool, + shrinkExtent: null == shrinkExtent + ? _value.shrinkExtent + : shrinkExtent // ignore: cast_nullable_to_non_nullable + as double, + scrollDirection: null == scrollDirection + ? _value.scrollDirection + : scrollDirection // ignore: cast_nullable_to_non_nullable + as Axis, + reverse: null == reverse + ? _value.reverse + : reverse // ignore: cast_nullable_to_non_nullable + as bool, + onTap: freezed == onTap + ? _value._onTap + : onTap // ignore: cast_nullable_to_non_nullable + as Map?, + enableSplash: null == enableSplash + ? _value.enableSplash + : enableSplash // ignore: cast_nullable_to_non_nullable + as bool, + itemExtent: freezed == itemExtent + ? _value.itemExtent + : itemExtent // ignore: cast_nullable_to_non_nullable + as double?, + flexWeights: freezed == flexWeights + ? _value._flexWeights + : flexWeights // ignore: cast_nullable_to_non_nullable + as List?, + children: freezed == children + ? _value._children + : children // ignore: cast_nullable_to_non_nullable + as List>?, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$MiraiCarouselViewImpl implements _MiraiCarouselView { + const _$MiraiCarouselViewImpl( + {this.carouselType = MiraiCarouselViewType.regular, + this.padding, + this.backgroundColor, + this.elevation, + this.overlayColor, + this.itemSnapping = false, + this.shrinkExtent = 0.0, + this.scrollDirection = Axis.horizontal, + this.reverse = false, + final Map? onTap, + this.enableSplash = true, + this.itemExtent, + final List? flexWeights, + required final List>? children}) + : _onTap = onTap, + _flexWeights = flexWeights, + _children = children; + + factory _$MiraiCarouselViewImpl.fromJson(Map json) => + _$$MiraiCarouselViewImplFromJson(json); + + @override + @JsonKey() + final MiraiCarouselViewType carouselType; + @override + final MiraiEdgeInsets? padding; + @override + final String? backgroundColor; + @override + final double? elevation; + @override + final String? overlayColor; + @override + @JsonKey() + final bool itemSnapping; + @override + @JsonKey() + final double shrinkExtent; + @override + @JsonKey() + final Axis scrollDirection; + @override + @JsonKey() + final bool reverse; + final Map? _onTap; + @override + Map? get onTap { + final value = _onTap; + if (value == null) return null; + if (_onTap is EqualUnmodifiableMapView) return _onTap; + // ignore: implicit_dynamic_type + return EqualUnmodifiableMapView(value); + } + + @override + @JsonKey() + final bool enableSplash; + @override + final double? itemExtent; + final List? _flexWeights; + @override + List? get flexWeights { + final value = _flexWeights; + if (value == null) return null; + if (_flexWeights is EqualUnmodifiableListView) return _flexWeights; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(value); + } + + final List>? _children; + @override + List>? get children { + final value = _children; + if (value == null) return null; + if (_children is EqualUnmodifiableListView) return _children; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(value); + } + + @override + String toString() { + return 'MiraiCarouselView(carouselType: $carouselType, padding: $padding, backgroundColor: $backgroundColor, elevation: $elevation, overlayColor: $overlayColor, itemSnapping: $itemSnapping, shrinkExtent: $shrinkExtent, scrollDirection: $scrollDirection, reverse: $reverse, onTap: $onTap, enableSplash: $enableSplash, itemExtent: $itemExtent, flexWeights: $flexWeights, children: $children)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$MiraiCarouselViewImpl && + (identical(other.carouselType, carouselType) || + other.carouselType == carouselType) && + (identical(other.padding, padding) || other.padding == padding) && + (identical(other.backgroundColor, backgroundColor) || + other.backgroundColor == backgroundColor) && + (identical(other.elevation, elevation) || + other.elevation == elevation) && + (identical(other.overlayColor, overlayColor) || + other.overlayColor == overlayColor) && + (identical(other.itemSnapping, itemSnapping) || + other.itemSnapping == itemSnapping) && + (identical(other.shrinkExtent, shrinkExtent) || + other.shrinkExtent == shrinkExtent) && + (identical(other.scrollDirection, scrollDirection) || + other.scrollDirection == scrollDirection) && + (identical(other.reverse, reverse) || other.reverse == reverse) && + const DeepCollectionEquality().equals(other._onTap, _onTap) && + (identical(other.enableSplash, enableSplash) || + other.enableSplash == enableSplash) && + (identical(other.itemExtent, itemExtent) || + other.itemExtent == itemExtent) && + const DeepCollectionEquality() + .equals(other._flexWeights, _flexWeights) && + const DeepCollectionEquality().equals(other._children, _children)); + } + + @JsonKey(includeFromJson: false, includeToJson: false) + @override + int get hashCode => Object.hash( + runtimeType, + carouselType, + padding, + backgroundColor, + elevation, + overlayColor, + itemSnapping, + shrinkExtent, + scrollDirection, + reverse, + const DeepCollectionEquality().hash(_onTap), + enableSplash, + itemExtent, + const DeepCollectionEquality().hash(_flexWeights), + const DeepCollectionEquality().hash(_children)); + + /// Create a copy of MiraiCarouselView + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + @override + @pragma('vm:prefer-inline') + _$$MiraiCarouselViewImplCopyWith<_$MiraiCarouselViewImpl> get copyWith => + __$$MiraiCarouselViewImplCopyWithImpl<_$MiraiCarouselViewImpl>( + this, _$identity); + + @override + Map toJson() { + return _$$MiraiCarouselViewImplToJson( + this, + ); + } +} + +abstract class _MiraiCarouselView implements MiraiCarouselView { + const factory _MiraiCarouselView( + {final MiraiCarouselViewType carouselType, + final MiraiEdgeInsets? padding, + final String? backgroundColor, + final double? elevation, + final String? overlayColor, + final bool itemSnapping, + final double shrinkExtent, + final Axis scrollDirection, + final bool reverse, + final Map? onTap, + final bool enableSplash, + final double? itemExtent, + final List? flexWeights, + required final List>? children}) = + _$MiraiCarouselViewImpl; + + factory _MiraiCarouselView.fromJson(Map json) = + _$MiraiCarouselViewImpl.fromJson; + + @override + MiraiCarouselViewType get carouselType; + @override + MiraiEdgeInsets? get padding; + @override + String? get backgroundColor; + @override + double? get elevation; + @override + String? get overlayColor; + @override + bool get itemSnapping; + @override + double get shrinkExtent; + @override + Axis get scrollDirection; + @override + bool get reverse; + @override + Map? get onTap; + @override + bool get enableSplash; + @override + double? get itemExtent; + @override + List? get flexWeights; + @override + List>? get children; + + /// Create a copy of MiraiCarouselView + /// with the given fields replaced by the non-null parameter values. + @override + @JsonKey(includeFromJson: false, includeToJson: false) + _$$MiraiCarouselViewImplCopyWith<_$MiraiCarouselViewImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.g.dart b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.g.dart new file mode 100644 index 00000000..4d1ee6e0 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view.g.dart @@ -0,0 +1,65 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mirai_carousel_view.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$MiraiCarouselViewImpl _$$MiraiCarouselViewImplFromJson( + Map json) => + _$MiraiCarouselViewImpl( + carouselType: $enumDecodeNullable( + _$MiraiCarouselViewTypeEnumMap, json['carouselType']) ?? + MiraiCarouselViewType.regular, + padding: json['padding'] == null + ? null + : MiraiEdgeInsets.fromJson(json['padding']), + backgroundColor: json['backgroundColor'] as String?, + elevation: (json['elevation'] as num?)?.toDouble(), + overlayColor: json['overlayColor'] as String?, + itemSnapping: json['itemSnapping'] as bool? ?? false, + shrinkExtent: (json['shrinkExtent'] as num?)?.toDouble() ?? 0.0, + scrollDirection: + $enumDecodeNullable(_$AxisEnumMap, json['scrollDirection']) ?? + Axis.horizontal, + reverse: json['reverse'] as bool? ?? false, + onTap: json['onTap'] as Map?, + enableSplash: json['enableSplash'] as bool? ?? true, + itemExtent: (json['itemExtent'] as num?)?.toDouble(), + flexWeights: (json['flexWeights'] as List?) + ?.map((e) => (e as num).toInt()) + .toList(), + children: (json['children'] as List?) + ?.map((e) => e as Map) + .toList(), + ); + +Map _$$MiraiCarouselViewImplToJson( + _$MiraiCarouselViewImpl instance) => + { + 'carouselType': _$MiraiCarouselViewTypeEnumMap[instance.carouselType]!, + 'padding': instance.padding, + 'backgroundColor': instance.backgroundColor, + 'elevation': instance.elevation, + 'overlayColor': instance.overlayColor, + 'itemSnapping': instance.itemSnapping, + 'shrinkExtent': instance.shrinkExtent, + 'scrollDirection': _$AxisEnumMap[instance.scrollDirection]!, + 'reverse': instance.reverse, + 'onTap': instance.onTap, + 'enableSplash': instance.enableSplash, + 'itemExtent': instance.itemExtent, + 'flexWeights': instance.flexWeights, + 'children': instance.children, + }; + +const _$MiraiCarouselViewTypeEnumMap = { + MiraiCarouselViewType.regular: 'regular', + MiraiCarouselViewType.weighted: 'weighted', +}; + +const _$AxisEnumMap = { + Axis.horizontal: 'horizontal', + Axis.vertical: 'vertical', +}; diff --git a/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view_parser.dart b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view_parser.dart new file mode 100644 index 00000000..ab687e51 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_carousel_view/mirai_carousel_view_parser.dart @@ -0,0 +1,57 @@ +import 'package:flutter/material.dart'; +import 'package:mirai/mirai.dart'; +import 'package:mirai/src/utils/widget_type.dart'; + +class MiraiCarouselViewParser extends MiraiParser { + const MiraiCarouselViewParser(); + + @override + String get type => WidgetType.carouselView.name; + + @override + MiraiCarouselView getModel(Map json) => + MiraiCarouselView.fromJson(json); + + @override + Widget parse(BuildContext context, MiraiCarouselView model) { + switch (model.carouselType) { + case MiraiCarouselViewType.regular: + return CarouselView( + padding: model.padding.parse, + backgroundColor: model.backgroundColor.toColor(context), + elevation: model.elevation, + overlayColor: + WidgetStateProperty.all(model.overlayColor.toColor(context)), + itemSnapping: model.itemSnapping, + shrinkExtent: model.shrinkExtent, + scrollDirection: model.scrollDirection, + reverse: model.reverse, + onTap: (index) => Mirai.fromJson(model.onTap, context), + enableSplash: model.enableSplash, + itemExtent: model.itemExtent ?? 0, + children: model.children + ?.map((e) => Mirai.fromJson(e, context) ?? SizedBox()) + .toList() ?? + [], + ); + case MiraiCarouselViewType.weighted: + return CarouselView.weighted( + padding: model.padding.parse, + backgroundColor: model.backgroundColor.toColor(context), + elevation: model.elevation, + overlayColor: + WidgetStateProperty.all(model.overlayColor.toColor(context)), + itemSnapping: model.itemSnapping, + shrinkExtent: model.shrinkExtent, + scrollDirection: model.scrollDirection, + reverse: model.reverse, + onTap: (index) => Mirai.fromJson(model.onTap, context), + flexWeights: model.flexWeights ?? [], + children: model.children + ?.map((e) => Mirai.fromJson(e, context) ?? SizedBox()) + .toList() ?? + [], + ); + } + } +} diff --git a/packages/mirai/lib/src/parsers/parsers.dart b/packages/mirai/lib/src/parsers/parsers.dart index 35ffd496..28e44237 100644 --- a/packages/mirai/lib/src/parsers/parsers.dart +++ b/packages/mirai/lib/src/parsers/parsers.dart @@ -17,6 +17,7 @@ export 'package:mirai/src/parsers/mirai_box_shadow/mirai_box_shadow.dart'; export 'package:mirai/src/parsers/mirai_button_style/mirai_button_style.dart'; export 'package:mirai/src/parsers/mirai_card/mirai_card.dart'; export 'package:mirai/src/parsers/mirai_card_theme_data/mirai_card_theme_data.dart'; +export 'package:mirai/src/parsers/mirai_carousel_view/mirai_carousel_view.dart'; export 'package:mirai/src/parsers/mirai_center/mirai_center.dart'; export 'package:mirai/src/parsers/mirai_check_box_widget/mirai_check_box_widget.dart'; export 'package:mirai/src/parsers/mirai_chip/mirai_chip.dart'; diff --git a/packages/mirai/lib/src/utils/widget_type.dart b/packages/mirai/lib/src/utils/widget_type.dart index 0f9a4785..f236b049 100644 --- a/packages/mirai/lib/src/utils/widget_type.dart +++ b/packages/mirai/lib/src/utils/widget_type.dart @@ -51,4 +51,5 @@ enum WidgetType { autocomplete, table, tableCell, + carouselView, }