From b7b3b91a5d98342ffdf594fc4413f385fdfeacb8 Mon Sep 17 00:00:00 2001 From: kushalm Date: Sun, 24 Sep 2023 13:10:27 +0400 Subject: [PATCH] refactor (MiraiSwitch): Cubit replaced with StatefulWidget --- .../assets/json/switch_example.json | 3 +- .../cubit/mirai_switch_cubit.dart | 13 - .../cubit/mirai_switch_state.dart | 18 -- .../parsers/mirai_switch/mirai_switch.dart | 35 +++ .../mirai_switch/mirai_switch.freezed.dart | 250 +++++++++++++++++- .../parsers/mirai_switch/mirai_switch.g.dart | 20 ++ .../mirai_switch/mirai_switch_parser.dart | 21 +- .../widget/mirai_switch_widget.dart | 35 +++ .../widget/mirai_switch_widget_state.dart | 20 ++ packages/mirai/lib/src/utils/widget_type.dart | 2 - 10 files changed, 354 insertions(+), 63 deletions(-) delete mode 100644 packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_cubit.dart delete mode 100644 packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_state.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget.dart create mode 100644 packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget_state.dart diff --git a/examples/mirai_gallery/assets/json/switch_example.json b/examples/mirai_gallery/assets/json/switch_example.json index b4fbe561..0a29ef5d 100644 --- a/examples/mirai_gallery/assets/json/switch_example.json +++ b/examples/mirai_gallery/assets/json/switch_example.json @@ -34,7 +34,8 @@ { "type": "switchButton", "initialValue": false, - "onChanged": {} + "onChanged": {}, + "activeColor": "#FF0000" } ] }, diff --git a/packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_cubit.dart b/packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_cubit.dart deleted file mode 100644 index fc25063a..00000000 --- a/packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_cubit.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'package:flutter/foundation.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; - -part 'mirai_switch_state.dart'; - -class MiraiSwitchCubit extends Cubit { - MiraiSwitchCubit({required bool value}) - : super(MiraiSwitchState(isSelected: value)); - - void changeValue(bool value) { - emit(state.copyWith(isSelected: value)); - } -} diff --git a/packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_state.dart b/packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_state.dart deleted file mode 100644 index e44bfd39..00000000 --- a/packages/mirai/lib/src/parsers/mirai_switch/cubit/mirai_switch_state.dart +++ /dev/null @@ -1,18 +0,0 @@ -part of 'mirai_switch_cubit.dart'; - -@immutable -final class MiraiSwitchState { - const MiraiSwitchState({ - this.isSelected = false, - }); - - final bool isSelected; - - MiraiSwitchState copyWith({ - bool? isSelected, - }) { - return MiraiSwitchState( - isSelected: isSelected ?? this.isSelected, - ); - } -} diff --git a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.dart b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.dart index 09b4cc62..b5742e27 100644 --- a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.dart +++ b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.dart @@ -1,15 +1,50 @@ +import 'dart:ui'; + +import 'package:flutter/gestures.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:mirai/mirai.dart'; part 'mirai_switch.freezed.dart'; part 'mirai_switch.g.dart'; @freezed class MiraiSwitch with _$MiraiSwitch { + const MiraiSwitch._(); + const factory MiraiSwitch({ @Default(false) initialValue, Map? onChanged, + @Default(false) bool autofocus, + @Default(false) bool disabled, + String? activeColor, + String? activeTrackColor, + String? focusColor, + String? hoverColor, + String? inactiveThumbColor, + String? inactiveTrackColor, + double? splashRadius, + String? dragStartBehavior, }) = _MiraiSwitch; factory MiraiSwitch.fromJson(Map json) => _$MiraiSwitchFromJson(json); + + DragStartBehavior get dragStateBehaviorValue { + return DragStartBehavior.values.firstWhere( + (element) => element.name == dragStartBehavior, + orElse: () => DragStartBehavior.start, + ); + } + + Color? get activeColorValue => activeColor?.toColor; + + Color? get activeTrackColorValue => activeTrackColor?.toColor; + + Color? get focusColorValue => focusColor?.toColor; + + Color? get hoverColorValue => hoverColor?.toColor; + + Color? get inactiveThumbColorValue => inactiveThumbColor?.toColor; + + Color? get inactiveTrackColorValue => inactiveTrackColor?.toColor; } diff --git a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.freezed.dart b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.freezed.dart index 2c6e2933..ab908192 100644 --- a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.freezed.dart +++ b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.freezed.dart @@ -22,6 +22,16 @@ MiraiSwitch _$MiraiSwitchFromJson(Map json) { mixin _$MiraiSwitch { dynamic get initialValue => throw _privateConstructorUsedError; Map? get onChanged => throw _privateConstructorUsedError; + bool get autofocus => throw _privateConstructorUsedError; + bool get disabled => throw _privateConstructorUsedError; + String? get activeColor => throw _privateConstructorUsedError; + String? get activeTrackColor => throw _privateConstructorUsedError; + String? get focusColor => throw _privateConstructorUsedError; + String? get hoverColor => throw _privateConstructorUsedError; + String? get inactiveThumbColor => throw _privateConstructorUsedError; + String? get inactiveTrackColor => throw _privateConstructorUsedError; + double? get splashRadius => throw _privateConstructorUsedError; + String? get dragStartBehavior => throw _privateConstructorUsedError; Map toJson() => throw _privateConstructorUsedError; @JsonKey(ignore: true) @@ -35,7 +45,19 @@ abstract class $MiraiSwitchCopyWith<$Res> { MiraiSwitch value, $Res Function(MiraiSwitch) then) = _$MiraiSwitchCopyWithImpl<$Res, MiraiSwitch>; @useResult - $Res call({dynamic initialValue, Map? onChanged}); + $Res call( + {dynamic initialValue, + Map? onChanged, + bool autofocus, + bool disabled, + String? activeColor, + String? activeTrackColor, + String? focusColor, + String? hoverColor, + String? inactiveThumbColor, + String? inactiveTrackColor, + double? splashRadius, + String? dragStartBehavior}); } /// @nodoc @@ -53,6 +75,16 @@ class _$MiraiSwitchCopyWithImpl<$Res, $Val extends MiraiSwitch> $Res call({ Object? initialValue = freezed, Object? onChanged = freezed, + Object? autofocus = null, + Object? disabled = null, + Object? activeColor = freezed, + Object? activeTrackColor = freezed, + Object? focusColor = freezed, + Object? hoverColor = freezed, + Object? inactiveThumbColor = freezed, + Object? inactiveTrackColor = freezed, + Object? splashRadius = freezed, + Object? dragStartBehavior = freezed, }) { return _then(_value.copyWith( initialValue: freezed == initialValue @@ -63,6 +95,46 @@ class _$MiraiSwitchCopyWithImpl<$Res, $Val extends MiraiSwitch> ? _value.onChanged : onChanged // ignore: cast_nullable_to_non_nullable as Map?, + autofocus: null == autofocus + ? _value.autofocus + : autofocus // ignore: cast_nullable_to_non_nullable + as bool, + disabled: null == disabled + ? _value.disabled + : disabled // ignore: cast_nullable_to_non_nullable + as bool, + activeColor: freezed == activeColor + ? _value.activeColor + : activeColor // ignore: cast_nullable_to_non_nullable + as String?, + activeTrackColor: freezed == activeTrackColor + ? _value.activeTrackColor + : activeTrackColor // ignore: cast_nullable_to_non_nullable + as String?, + focusColor: freezed == focusColor + ? _value.focusColor + : focusColor // ignore: cast_nullable_to_non_nullable + as String?, + hoverColor: freezed == hoverColor + ? _value.hoverColor + : hoverColor // ignore: cast_nullable_to_non_nullable + as String?, + inactiveThumbColor: freezed == inactiveThumbColor + ? _value.inactiveThumbColor + : inactiveThumbColor // ignore: cast_nullable_to_non_nullable + as String?, + inactiveTrackColor: freezed == inactiveTrackColor + ? _value.inactiveTrackColor + : inactiveTrackColor // ignore: cast_nullable_to_non_nullable + as String?, + splashRadius: freezed == splashRadius + ? _value.splashRadius + : splashRadius // ignore: cast_nullable_to_non_nullable + as double?, + dragStartBehavior: freezed == dragStartBehavior + ? _value.dragStartBehavior + : dragStartBehavior // ignore: cast_nullable_to_non_nullable + as String?, ) as $Val); } } @@ -75,7 +147,19 @@ abstract class _$$_MiraiSwitchCopyWith<$Res> __$$_MiraiSwitchCopyWithImpl<$Res>; @override @useResult - $Res call({dynamic initialValue, Map? onChanged}); + $Res call( + {dynamic initialValue, + Map? onChanged, + bool autofocus, + bool disabled, + String? activeColor, + String? activeTrackColor, + String? focusColor, + String? hoverColor, + String? inactiveThumbColor, + String? inactiveTrackColor, + double? splashRadius, + String? dragStartBehavior}); } /// @nodoc @@ -91,6 +175,16 @@ class __$$_MiraiSwitchCopyWithImpl<$Res> $Res call({ Object? initialValue = freezed, Object? onChanged = freezed, + Object? autofocus = null, + Object? disabled = null, + Object? activeColor = freezed, + Object? activeTrackColor = freezed, + Object? focusColor = freezed, + Object? hoverColor = freezed, + Object? inactiveThumbColor = freezed, + Object? inactiveTrackColor = freezed, + Object? splashRadius = freezed, + Object? dragStartBehavior = freezed, }) { return _then(_$_MiraiSwitch( initialValue: @@ -99,16 +193,68 @@ class __$$_MiraiSwitchCopyWithImpl<$Res> ? _value._onChanged : onChanged // ignore: cast_nullable_to_non_nullable as Map?, + autofocus: null == autofocus + ? _value.autofocus + : autofocus // ignore: cast_nullable_to_non_nullable + as bool, + disabled: null == disabled + ? _value.disabled + : disabled // ignore: cast_nullable_to_non_nullable + as bool, + activeColor: freezed == activeColor + ? _value.activeColor + : activeColor // ignore: cast_nullable_to_non_nullable + as String?, + activeTrackColor: freezed == activeTrackColor + ? _value.activeTrackColor + : activeTrackColor // ignore: cast_nullable_to_non_nullable + as String?, + focusColor: freezed == focusColor + ? _value.focusColor + : focusColor // ignore: cast_nullable_to_non_nullable + as String?, + hoverColor: freezed == hoverColor + ? _value.hoverColor + : hoverColor // ignore: cast_nullable_to_non_nullable + as String?, + inactiveThumbColor: freezed == inactiveThumbColor + ? _value.inactiveThumbColor + : inactiveThumbColor // ignore: cast_nullable_to_non_nullable + as String?, + inactiveTrackColor: freezed == inactiveTrackColor + ? _value.inactiveTrackColor + : inactiveTrackColor // ignore: cast_nullable_to_non_nullable + as String?, + splashRadius: freezed == splashRadius + ? _value.splashRadius + : splashRadius // ignore: cast_nullable_to_non_nullable + as double?, + dragStartBehavior: freezed == dragStartBehavior + ? _value.dragStartBehavior + : dragStartBehavior // ignore: cast_nullable_to_non_nullable + as String?, )); } } /// @nodoc @JsonSerializable() -class _$_MiraiSwitch implements _MiraiSwitch { +class _$_MiraiSwitch extends _MiraiSwitch { const _$_MiraiSwitch( - {this.initialValue = false, final Map? onChanged}) - : _onChanged = onChanged; + {this.initialValue = false, + final Map? onChanged, + this.autofocus = false, + this.disabled = false, + this.activeColor, + this.activeTrackColor, + this.focusColor, + this.hoverColor, + this.inactiveThumbColor, + this.inactiveTrackColor, + this.splashRadius, + this.dragStartBehavior}) + : _onChanged = onChanged, + super._(); factory _$_MiraiSwitch.fromJson(Map json) => _$$_MiraiSwitchFromJson(json); @@ -126,9 +272,32 @@ class _$_MiraiSwitch implements _MiraiSwitch { return EqualUnmodifiableMapView(value); } + @override + @JsonKey() + final bool autofocus; + @override + @JsonKey() + final bool disabled; + @override + final String? activeColor; + @override + final String? activeTrackColor; + @override + final String? focusColor; + @override + final String? hoverColor; + @override + final String? inactiveThumbColor; + @override + final String? inactiveTrackColor; + @override + final double? splashRadius; + @override + final String? dragStartBehavior; + @override String toString() { - return 'MiraiSwitch(initialValue: $initialValue, onChanged: $onChanged)'; + return 'MiraiSwitch(initialValue: $initialValue, onChanged: $onChanged, autofocus: $autofocus, disabled: $disabled, activeColor: $activeColor, activeTrackColor: $activeTrackColor, focusColor: $focusColor, hoverColor: $hoverColor, inactiveThumbColor: $inactiveThumbColor, inactiveTrackColor: $inactiveTrackColor, splashRadius: $splashRadius, dragStartBehavior: $dragStartBehavior)'; } @override @@ -139,7 +308,27 @@ class _$_MiraiSwitch implements _MiraiSwitch { const DeepCollectionEquality() .equals(other.initialValue, initialValue) && const DeepCollectionEquality() - .equals(other._onChanged, _onChanged)); + .equals(other._onChanged, _onChanged) && + (identical(other.autofocus, autofocus) || + other.autofocus == autofocus) && + (identical(other.disabled, disabled) || + other.disabled == disabled) && + (identical(other.activeColor, activeColor) || + other.activeColor == activeColor) && + (identical(other.activeTrackColor, activeTrackColor) || + other.activeTrackColor == activeTrackColor) && + (identical(other.focusColor, focusColor) || + other.focusColor == focusColor) && + (identical(other.hoverColor, hoverColor) || + other.hoverColor == hoverColor) && + (identical(other.inactiveThumbColor, inactiveThumbColor) || + other.inactiveThumbColor == inactiveThumbColor) && + (identical(other.inactiveTrackColor, inactiveTrackColor) || + other.inactiveTrackColor == inactiveTrackColor) && + (identical(other.splashRadius, splashRadius) || + other.splashRadius == splashRadius) && + (identical(other.dragStartBehavior, dragStartBehavior) || + other.dragStartBehavior == dragStartBehavior)); } @JsonKey(ignore: true) @@ -147,7 +336,17 @@ class _$_MiraiSwitch implements _MiraiSwitch { int get hashCode => Object.hash( runtimeType, const DeepCollectionEquality().hash(initialValue), - const DeepCollectionEquality().hash(_onChanged)); + const DeepCollectionEquality().hash(_onChanged), + autofocus, + disabled, + activeColor, + activeTrackColor, + focusColor, + hoverColor, + inactiveThumbColor, + inactiveTrackColor, + splashRadius, + dragStartBehavior); @JsonKey(ignore: true) @override @@ -163,10 +362,21 @@ class _$_MiraiSwitch implements _MiraiSwitch { } } -abstract class _MiraiSwitch implements MiraiSwitch { +abstract class _MiraiSwitch extends MiraiSwitch { const factory _MiraiSwitch( {final dynamic initialValue, - final Map? onChanged}) = _$_MiraiSwitch; + final Map? onChanged, + final bool autofocus, + final bool disabled, + final String? activeColor, + final String? activeTrackColor, + final String? focusColor, + final String? hoverColor, + final String? inactiveThumbColor, + final String? inactiveTrackColor, + final double? splashRadius, + final String? dragStartBehavior}) = _$_MiraiSwitch; + const _MiraiSwitch._() : super._(); factory _MiraiSwitch.fromJson(Map json) = _$_MiraiSwitch.fromJson; @@ -176,6 +386,26 @@ abstract class _MiraiSwitch implements MiraiSwitch { @override Map? get onChanged; @override + bool get autofocus; + @override + bool get disabled; + @override + String? get activeColor; + @override + String? get activeTrackColor; + @override + String? get focusColor; + @override + String? get hoverColor; + @override + String? get inactiveThumbColor; + @override + String? get inactiveTrackColor; + @override + double? get splashRadius; + @override + String? get dragStartBehavior; + @override @JsonKey(ignore: true) _$$_MiraiSwitchCopyWith<_$_MiraiSwitch> get copyWith => throw _privateConstructorUsedError; diff --git a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.g.dart b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.g.dart index 14f56977..0128dcf6 100644 --- a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.g.dart +++ b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.g.dart @@ -10,10 +10,30 @@ _$_MiraiSwitch _$$_MiraiSwitchFromJson(Map json) => _$_MiraiSwitch( initialValue: json['initialValue'] ?? false, onChanged: json['onChanged'] as Map?, + autofocus: json['autofocus'] as bool? ?? false, + disabled: json['disabled'] as bool? ?? false, + activeColor: json['activeColor'] as String?, + activeTrackColor: json['activeTrackColor'] as String?, + focusColor: json['focusColor'] as String?, + hoverColor: json['hoverColor'] as String?, + inactiveThumbColor: json['inactiveThumbColor'] as String?, + inactiveTrackColor: json['inactiveTrackColor'] as String?, + splashRadius: (json['splashRadius'] as num?)?.toDouble(), + dragStartBehavior: json['dragStartBehavior'] as String?, ); Map _$$_MiraiSwitchToJson(_$_MiraiSwitch instance) => { 'initialValue': instance.initialValue, 'onChanged': instance.onChanged, + 'autofocus': instance.autofocus, + 'disabled': instance.disabled, + 'activeColor': instance.activeColor, + 'activeTrackColor': instance.activeTrackColor, + 'focusColor': instance.focusColor, + 'hoverColor': instance.hoverColor, + 'inactiveThumbColor': instance.inactiveThumbColor, + 'inactiveTrackColor': instance.inactiveTrackColor, + 'splashRadius': instance.splashRadius, + 'dragStartBehavior': instance.dragStartBehavior, }; diff --git a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch_parser.dart b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch_parser.dart index ee0f4351..0abb4534 100644 --- a/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_switch/mirai_switch_parser.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mirai/src/framework/framework.dart'; -import 'package:mirai/src/parsers/mirai_switch/cubit/mirai_switch_cubit.dart'; import 'package:mirai/src/parsers/mirai_switch/mirai_switch.dart'; +import 'package:mirai/src/parsers/mirai_switch/widget/mirai_switch_widget.dart'; import 'package:mirai/src/utils/widget_type.dart'; class MiraiSwitchParser extends MiraiParser { @@ -16,22 +15,6 @@ class MiraiSwitchParser extends MiraiParser { @override Widget parse(BuildContext context, MiraiSwitch model) { - return BlocProvider( - create: (context) => MiraiSwitchCubit(value: model.initialValue ?? false), - child: BlocSelector( - selector: (state) => state.isSelected, - builder: (context, isSelected) { - return Switch( - value: isSelected, - onChanged: (bool value) { - context.read().changeValue(value); - if (model.onChanged != null) { - Mirai.onCallFromJson(model.onChanged, context); - } - }, - ); - }, - ), - ); + return MiraiSwitchWidget(model: model); } } diff --git a/packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget.dart b/packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget.dart new file mode 100644 index 00000000..6c36e2d7 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget.dart @@ -0,0 +1,35 @@ +import 'package:flutter/material.dart'; +import 'package:mirai/mirai.dart'; +import 'package:mirai/src/parsers/mirai_switch/mirai_switch.dart'; + +part 'mirai_switch_widget_state.dart'; + +class MiraiSwitchWidget extends StatefulWidget { + const MiraiSwitchWidget({super.key, required this.model}); + + final MiraiSwitch model; + + @override + State createState() => _MiraiSwitchUiWidget(); +} + +class _MiraiSwitchUiWidget extends _MiraiSwitchWidgetState { + @override + Widget build(BuildContext context) { + final MiraiSwitch model = widget.model; + + return Switch( + value: isSelected, + onChanged: !model.disabled ? _changeValue : null, + activeColor: model.activeColorValue, + activeTrackColor: model.activeTrackColorValue, + autofocus: model.autofocus, + focusColor: model.focusColorValue, + hoverColor: model.hoverColorValue, + inactiveThumbColor: model.inactiveThumbColorValue, + inactiveTrackColor: model.inactiveTrackColorValue, + splashRadius: model.splashRadius, + dragStartBehavior: model.dragStateBehaviorValue, + ); + } +} diff --git a/packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget_state.dart b/packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget_state.dart new file mode 100644 index 00000000..aee180f2 --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_switch/widget/mirai_switch_widget_state.dart @@ -0,0 +1,20 @@ +part of 'mirai_switch_widget.dart'; + +abstract class _MiraiSwitchWidgetState extends State { + bool isSelected = false; + + @override + void initState() { + super.initState(); + isSelected = widget.model.initialValue ?? false; + } + + /// Change the value of the switch as the user toggles it. + void _changeValue(bool value) { + isSelected = value; + if (widget.model.onChanged != null) { + Mirai.onCallFromJson(widget.model.onChanged, context); + } + setState(() {}); + } +} diff --git a/packages/mirai/lib/src/utils/widget_type.dart b/packages/mirai/lib/src/utils/widget_type.dart index 33ddbf91..c5de69a1 100644 --- a/packages/mirai/lib/src/utils/widget_type.dart +++ b/packages/mirai/lib/src/utils/widget_type.dart @@ -36,7 +36,5 @@ enum WidgetType { expanded, flexible, safeArea, - - /// cant use switch here because it is a keyword switchButton, }