From 9aaac173c01ce84bd6119c4f409792f05bc3c917 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Thu, 18 Jan 2024 15:31:13 +0400 Subject: [PATCH] refactor: Sorted page-view parser properties and added support for onPageChanged --- .../assets/json/home_screen.json | 56 +++++++------- .../mirai_page_view/mirai_page_view.dart | 15 ++-- .../mirai_page_view.freezed.dart | 75 +++++++++++++------ .../mirai_page_view/mirai_page_view.g.dart | 6 +- .../mirai_page_view_parser.dart | 19 +++-- 5 files changed, 104 insertions(+), 67 deletions(-) diff --git a/examples/mirai_gallery/assets/json/home_screen.json b/examples/mirai_gallery/assets/json/home_screen.json index 61f362f0..5807490c 100644 --- a/examples/mirai_gallery/assets/json/home_screen.json +++ b/examples/mirai_gallery/assets/json/home_screen.json @@ -646,18 +646,18 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "table_rows" + "icon": "table_chart" }, "title": { "type": "text", - "data": "Mirai Row", + "data": "Mirai PageView", "style": { "fontSize": 21 } }, "subtitle": { "type": "text", - "data": "Layout a list of child widgets in the horizontal direction", + "data": "A Material Design widget that displays a horizontal row of pages", "style": { "fontSize": 12 } @@ -668,7 +668,7 @@ "navigationStyle": "push", "widgetJson": { "type": "exampleScreen", - "assetPath": "assets/json/row_example.json" + "assetPath": "assets/json/page_view_example.json" } } }, @@ -677,18 +677,18 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "widgets" + "icon": "table_rows" }, "title": { "type": "text", - "data": "Mirai Scaffold", + "data": "Mirai Row", "style": { "fontSize": 21 } }, "subtitle": { "type": "text", - "data": "Implements the basic Material Design visual layout structure", + "data": "Layout a list of child widgets in the horizontal direction", "style": { "fontSize": 12 } @@ -699,7 +699,7 @@ "navigationStyle": "push", "widgetJson": { "type": "exampleScreen", - "assetPath": "assets/json/scaffold_example.json" + "assetPath": "assets/json/row_example.json" } } }, @@ -708,18 +708,18 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "swipe_down" + "icon": "widgets" }, "title": { "type": "text", - "data": "Mirai Scroll View", + "data": "Mirai Scaffold", "style": { "fontSize": 21 } }, "subtitle": { "type": "text", - "data": "A box in which a single widget can be scrolled", + "data": "Implements the basic Material Design visual layout structure", "style": { "fontSize": 12 } @@ -730,7 +730,7 @@ "navigationStyle": "push", "widgetJson": { "type": "exampleScreen", - "assetPath": "assets/json/scroll_view_example.json" + "assetPath": "assets/json/scaffold_example.json" } } }, @@ -739,18 +739,18 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "space_bar" + "icon": "swipe_down" }, "title": { "type": "text", - "data": "Mirai Sizedbox", + "data": "Mirai Scroll View", "style": { "fontSize": 21 } }, "subtitle": { "type": "text", - "data": "A box with a specified size. If given a child, this widget forces its child to have a specific dimensions.", + "data": "A box in which a single widget can be scrolled", "style": { "fontSize": 12 } @@ -761,7 +761,7 @@ "navigationStyle": "push", "widgetJson": { "type": "exampleScreen", - "assetPath": "assets/json/sized_box_example.json" + "assetPath": "assets/json/scroll_view_example.json" } } }, @@ -770,18 +770,18 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "toggle_on" + "icon": "space_bar" }, "title": { "type": "text", - "data": "Mirai Switch", + "data": "Mirai Sizedbox", "style": { "fontSize": 21 } }, "subtitle": { "type": "text", - "data": "A switch component that can be toggled via dragging or tapping on it.", + "data": "A box with a specified size. If given a child, this widget forces its child to have a specific dimensions.", "style": { "fontSize": 12 } @@ -792,7 +792,7 @@ "navigationStyle": "push", "widgetJson": { "type": "exampleScreen", - "assetPath": "assets/json/switch_example.json" + "assetPath": "assets/json/sized_box_example.json" } } }, @@ -801,18 +801,18 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "space_bar" + "icon": "toggle_on" }, "title": { "type": "text", - "data": "Mirai Fractionally Sizedbox", + "data": "Mirai Switch", "style": { "fontSize": 21 } }, "subtitle": { "type": "text", - "data": "A widget that sizes its child to a fraction of the total available space.", + "data": "A switch component that can be toggled via dragging or tapping on it.", "style": { "fontSize": 12 } @@ -823,7 +823,7 @@ "navigationStyle": "push", "widgetJson": { "type": "exampleScreen", - "assetPath": "assets/json/fractionally_sized_box_example.json" + "assetPath": "assets/json/switch_example.json" } } }, @@ -832,18 +832,18 @@ "leading": { "type": "icon", "iconType": "material", - "icon": "table_chart" + "icon": "space_bar" }, "title": { "type": "text", - "data": "Mirai PageView", + "data": "Mirai Fractionally Sizedbox", "style": { "fontSize": 21 } }, "subtitle": { "type": "text", - "data": "A Material Design widget that displays a horizontal row of pages", + "data": "A widget that sizes its child to a fraction of the total available space.", "style": { "fontSize": 12 } @@ -854,7 +854,7 @@ "navigationStyle": "push", "widgetJson": { "type": "exampleScreen", - "assetPath": "assets/json/page_view_example.json" + "assetPath": "assets/json/fractionally_sized_box_example.json" } } }, diff --git a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.dart b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.dart index 37553746..c9ff23ba 100644 --- a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.dart +++ b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.dart @@ -11,17 +11,18 @@ part 'mirai_page_view.g.dart'; @freezed class MiraiPageView with _$MiraiPageView { const factory MiraiPageView({ + @Default(Axis.horizontal) Axis scrollDirection, + @Default(false) bool reverse, + MiraiScrollPhysics? physics, + @Default(true) bool pageSnapping, + Map? onPageChanged, + @Default(DragStartBehavior.start) DragStartBehavior dragStartBehavior, @Default(false) bool allowImplicitScrolling, + String? restorationId, @Default(Clip.hardEdge) Clip clipBehavior, - @Default(DragStartBehavior.start) DragStartBehavior dragStartBehavior, + @Default(true) bool padEnds, @Default(0) int initialPage, @Default(true) keepPage, - String? restorationId, - MiraiScrollPhysics? physics, - @Default(true) bool pageSnapping, - @Default(true) bool padEnds, - @Default(false) bool reverse, - @Default(Axis.horizontal) Axis scrollDirection, @Default(1.0) double viewportFraction, @Default([]) List> children, }) = _MiraiPageView; diff --git a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.freezed.dart b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.freezed.dart index 3a001889..a7e503b2 100644 --- a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.freezed.dart +++ b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.freezed.dart @@ -25,10 +25,11 @@ mixin _$MiraiPageView { DragStartBehavior get dragStartBehavior => throw _privateConstructorUsedError; int get initialPage => throw _privateConstructorUsedError; dynamic get keepPage => throw _privateConstructorUsedError; - String? get restorationId => throw _privateConstructorUsedError; + Map? get onPageChanged => throw _privateConstructorUsedError; MiraiScrollPhysics? get physics => throw _privateConstructorUsedError; bool get pageSnapping => throw _privateConstructorUsedError; bool get padEnds => throw _privateConstructorUsedError; + String? get restorationId => throw _privateConstructorUsedError; bool get reverse => throw _privateConstructorUsedError; Axis get scrollDirection => throw _privateConstructorUsedError; double get viewportFraction => throw _privateConstructorUsedError; @@ -52,10 +53,11 @@ abstract class $MiraiPageViewCopyWith<$Res> { DragStartBehavior dragStartBehavior, int initialPage, dynamic keepPage, - String? restorationId, + Map? onPageChanged, MiraiScrollPhysics? physics, bool pageSnapping, bool padEnds, + String? restorationId, bool reverse, Axis scrollDirection, double viewportFraction, @@ -80,10 +82,11 @@ class _$MiraiPageViewCopyWithImpl<$Res, $Val extends MiraiPageView> Object? dragStartBehavior = null, Object? initialPage = null, Object? keepPage = freezed, - Object? restorationId = freezed, + Object? onPageChanged = freezed, Object? physics = freezed, Object? pageSnapping = null, Object? padEnds = null, + Object? restorationId = freezed, Object? reverse = null, Object? scrollDirection = null, Object? viewportFraction = null, @@ -110,10 +113,10 @@ class _$MiraiPageViewCopyWithImpl<$Res, $Val extends MiraiPageView> ? _value.keepPage : keepPage // ignore: cast_nullable_to_non_nullable as dynamic, - restorationId: freezed == restorationId - ? _value.restorationId - : restorationId // ignore: cast_nullable_to_non_nullable - as String?, + onPageChanged: freezed == onPageChanged + ? _value.onPageChanged + : onPageChanged // ignore: cast_nullable_to_non_nullable + as Map?, physics: freezed == physics ? _value.physics : physics // ignore: cast_nullable_to_non_nullable @@ -126,6 +129,10 @@ class _$MiraiPageViewCopyWithImpl<$Res, $Val extends MiraiPageView> ? _value.padEnds : padEnds // ignore: cast_nullable_to_non_nullable as bool, + restorationId: freezed == restorationId + ? _value.restorationId + : restorationId // ignore: cast_nullable_to_non_nullable + as String?, reverse: null == reverse ? _value.reverse : reverse // ignore: cast_nullable_to_non_nullable @@ -160,10 +167,11 @@ abstract class _$$MiraiPageViewImplCopyWith<$Res> DragStartBehavior dragStartBehavior, int initialPage, dynamic keepPage, - String? restorationId, + Map? onPageChanged, MiraiScrollPhysics? physics, bool pageSnapping, bool padEnds, + String? restorationId, bool reverse, Axis scrollDirection, double viewportFraction, @@ -186,10 +194,11 @@ class __$$MiraiPageViewImplCopyWithImpl<$Res> Object? dragStartBehavior = null, Object? initialPage = null, Object? keepPage = freezed, - Object? restorationId = freezed, + Object? onPageChanged = freezed, Object? physics = freezed, Object? pageSnapping = null, Object? padEnds = null, + Object? restorationId = freezed, Object? reverse = null, Object? scrollDirection = null, Object? viewportFraction = null, @@ -213,10 +222,10 @@ class __$$MiraiPageViewImplCopyWithImpl<$Res> : initialPage // ignore: cast_nullable_to_non_nullable as int, keepPage: freezed == keepPage ? _value.keepPage! : keepPage, - restorationId: freezed == restorationId - ? _value.restorationId - : restorationId // ignore: cast_nullable_to_non_nullable - as String?, + onPageChanged: freezed == onPageChanged + ? _value._onPageChanged + : onPageChanged // ignore: cast_nullable_to_non_nullable + as Map?, physics: freezed == physics ? _value.physics : physics // ignore: cast_nullable_to_non_nullable @@ -229,6 +238,10 @@ class __$$MiraiPageViewImplCopyWithImpl<$Res> ? _value.padEnds : padEnds // ignore: cast_nullable_to_non_nullable as bool, + restorationId: freezed == restorationId + ? _value.restorationId + : restorationId // ignore: cast_nullable_to_non_nullable + as String?, reverse: null == reverse ? _value.reverse : reverse // ignore: cast_nullable_to_non_nullable @@ -258,15 +271,17 @@ class _$MiraiPageViewImpl implements _MiraiPageView { this.dragStartBehavior = DragStartBehavior.start, this.initialPage = 0, this.keepPage = true, - this.restorationId, + final Map? onPageChanged, this.physics, this.pageSnapping = true, this.padEnds = true, + this.restorationId, this.reverse = false, this.scrollDirection = Axis.horizontal, this.viewportFraction = 1.0, final List> children = const []}) - : _children = children; + : _onPageChanged = onPageChanged, + _children = children; factory _$MiraiPageViewImpl.fromJson(Map json) => _$$MiraiPageViewImplFromJson(json); @@ -286,8 +301,16 @@ class _$MiraiPageViewImpl implements _MiraiPageView { @override @JsonKey() final dynamic keepPage; + final Map? _onPageChanged; @override - final String? restorationId; + Map? get onPageChanged { + final value = _onPageChanged; + if (value == null) return null; + if (_onPageChanged is EqualUnmodifiableMapView) return _onPageChanged; + // ignore: implicit_dynamic_type + return EqualUnmodifiableMapView(value); + } + @override final MiraiScrollPhysics? physics; @override @@ -297,6 +320,8 @@ class _$MiraiPageViewImpl implements _MiraiPageView { @JsonKey() final bool padEnds; @override + final String? restorationId; + @override @JsonKey() final bool reverse; @override @@ -316,7 +341,7 @@ class _$MiraiPageViewImpl implements _MiraiPageView { @override String toString() { - return 'MiraiPageView(allowImplicitScrolling: $allowImplicitScrolling, clipBehavior: $clipBehavior, dragStartBehavior: $dragStartBehavior, initialPage: $initialPage, keepPage: $keepPage, restorationId: $restorationId, physics: $physics, pageSnapping: $pageSnapping, padEnds: $padEnds, reverse: $reverse, scrollDirection: $scrollDirection, viewportFraction: $viewportFraction, children: $children)'; + return 'MiraiPageView(allowImplicitScrolling: $allowImplicitScrolling, clipBehavior: $clipBehavior, dragStartBehavior: $dragStartBehavior, initialPage: $initialPage, keepPage: $keepPage, onPageChanged: $onPageChanged, physics: $physics, pageSnapping: $pageSnapping, padEnds: $padEnds, restorationId: $restorationId, reverse: $reverse, scrollDirection: $scrollDirection, viewportFraction: $viewportFraction, children: $children)'; } @override @@ -333,12 +358,14 @@ class _$MiraiPageViewImpl implements _MiraiPageView { (identical(other.initialPage, initialPage) || other.initialPage == initialPage) && const DeepCollectionEquality().equals(other.keepPage, keepPage) && - (identical(other.restorationId, restorationId) || - other.restorationId == restorationId) && + const DeepCollectionEquality() + .equals(other._onPageChanged, _onPageChanged) && (identical(other.physics, physics) || other.physics == physics) && (identical(other.pageSnapping, pageSnapping) || other.pageSnapping == pageSnapping) && (identical(other.padEnds, padEnds) || other.padEnds == padEnds) && + (identical(other.restorationId, restorationId) || + other.restorationId == restorationId) && (identical(other.reverse, reverse) || other.reverse == reverse) && (identical(other.scrollDirection, scrollDirection) || other.scrollDirection == scrollDirection) && @@ -356,10 +383,11 @@ class _$MiraiPageViewImpl implements _MiraiPageView { dragStartBehavior, initialPage, const DeepCollectionEquality().hash(keepPage), - restorationId, + const DeepCollectionEquality().hash(_onPageChanged), physics, pageSnapping, padEnds, + restorationId, reverse, scrollDirection, viewportFraction, @@ -386,10 +414,11 @@ abstract class _MiraiPageView implements MiraiPageView { final DragStartBehavior dragStartBehavior, final int initialPage, final dynamic keepPage, - final String? restorationId, + final Map? onPageChanged, final MiraiScrollPhysics? physics, final bool pageSnapping, final bool padEnds, + final String? restorationId, final bool reverse, final Axis scrollDirection, final double viewportFraction, @@ -409,7 +438,7 @@ abstract class _MiraiPageView implements MiraiPageView { @override dynamic get keepPage; @override - String? get restorationId; + Map? get onPageChanged; @override MiraiScrollPhysics? get physics; @override @@ -417,6 +446,8 @@ abstract class _MiraiPageView implements MiraiPageView { @override bool get padEnds; @override + String? get restorationId; + @override bool get reverse; @override Axis get scrollDirection; diff --git a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.g.dart b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.g.dart index 1c24e99d..8cae2f1d 100644 --- a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.g.dart +++ b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view.g.dart @@ -16,11 +16,12 @@ _$MiraiPageViewImpl _$$MiraiPageViewImplFromJson(Map json) => DragStartBehavior.start, initialPage: json['initialPage'] as int? ?? 0, keepPage: json['keepPage'] ?? true, - restorationId: json['restorationId'] as String?, + onPageChanged: json['onPageChanged'] as Map?, physics: $enumDecodeNullable(_$MiraiScrollPhysicsEnumMap, json['physics']), pageSnapping: json['pageSnapping'] as bool? ?? true, padEnds: json['padEnds'] as bool? ?? true, + restorationId: json['restorationId'] as String?, reverse: json['reverse'] as bool? ?? false, scrollDirection: $enumDecodeNullable(_$AxisEnumMap, json['scrollDirection']) ?? @@ -40,10 +41,11 @@ Map _$$MiraiPageViewImplToJson(_$MiraiPageViewImpl instance) => _$DragStartBehaviorEnumMap[instance.dragStartBehavior]!, 'initialPage': instance.initialPage, 'keepPage': instance.keepPage, - 'restorationId': instance.restorationId, + 'onPageChanged': instance.onPageChanged, 'physics': _$MiraiScrollPhysicsEnumMap[instance.physics], 'pageSnapping': instance.pageSnapping, 'padEnds': instance.padEnds, + 'restorationId': instance.restorationId, 'reverse': instance.reverse, 'scrollDirection': _$AxisEnumMap[instance.scrollDirection]!, 'viewportFraction': instance.viewportFraction, diff --git a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view_parser.dart b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view_parser.dart index 194918e6..3afcef55 100644 --- a/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_page_view/mirai_page_view_parser.dart @@ -48,21 +48,24 @@ class _MiraiPageViewWidgetState extends State<_MiraiPageViewWidget> { @override Widget build(BuildContext context) { return PageView.builder( + scrollDirection: widget.model.scrollDirection, + reverse: widget.model.reverse, controller: _pageController, - allowImplicitScrolling: widget.model.allowImplicitScrolling, - clipBehavior: widget.model.clipBehavior, - dragStartBehavior: widget.model.dragStartBehavior, + physics: widget.model.physics?.parse, + pageSnapping: widget.model.pageSnapping, + onPageChanged: (int index) { + Mirai.onCallFromJson(widget.model.onPageChanged, context); + }, itemBuilder: (context, index) { return Mirai.fromJson(widget.model.children[index], context) ?? const SizedBox(); }, itemCount: widget.model.children.length, - padEnds: true, - pageSnapping: widget.model.pageSnapping, - physics: widget.model.physics?.parse, + dragStartBehavior: widget.model.dragStartBehavior, + allowImplicitScrolling: widget.model.allowImplicitScrolling, restorationId: widget.model.restorationId, - reverse: widget.model.reverse, - scrollDirection: widget.model.scrollDirection, + clipBehavior: widget.model.clipBehavior, + padEnds: true, ); } }