diff --git a/examples/mirai_gallery/assets/json/home_screen.json b/examples/mirai_gallery/assets/json/home_screen.json index 436b14d0..c980bacf 100644 --- a/examples/mirai_gallery/assets/json/home_screen.json +++ b/examples/mirai_gallery/assets/json/home_screen.json @@ -1489,6 +1489,40 @@ } } }, + { + "type": "listTile", + "leading": { + "type": "icon", + "iconType": "cupertino", + "icon": "slider_horizontal_3" + }, + "title": { + "type": "text", + "data": "Mirai Slider", + "align": "center", + "style": { + "fontSize": 21 + } + }, + "subtitle": { + "type": "text", + "data": "A Material Design Slider widget", + "align": "center", + "style": { + "fontSize": 12 + } + }, + "isThreeLine": true, + "style": "list", + "onTap": { + "actionType": "navigate", + "navigationStyle": "push", + "widgetJson": { + "type": "exampleScreen", + "assetPath": "assets/json/slider_example.json" + } + } + }, { "type": "sizedBox", "height": 24.0 diff --git a/examples/mirai_gallery/assets/json/slider_example.json b/examples/mirai_gallery/assets/json/slider_example.json new file mode 100644 index 00000000..44b29b80 --- /dev/null +++ b/examples/mirai_gallery/assets/json/slider_example.json @@ -0,0 +1,24 @@ +{ + "type": "scaffold", + "appBar": { + "type": "appBar", + "title": { + "type": "text", + "data": "Mirai Slider" + } + }, + "body": { + "type": "form", + "child": { + "type": "center", + "child": { + "id": "example_slider", + "type": "slider", + "sliderType": "material", + "value": 20, + "max": 100, + "divisions": 5 + } + } + } +} \ No newline at end of file diff --git a/packages/mirai/lib/src/framework/mirai.dart b/packages/mirai/lib/src/framework/mirai.dart index 138b74f1..fbb47063 100644 --- a/packages/mirai/lib/src/framework/mirai.dart +++ b/packages/mirai/lib/src/framework/mirai.dart @@ -89,6 +89,7 @@ class Mirai { const MiraiHeroParser(), const MiraiRadioParser(), const MiraiRadioGroupParser(), + const MiraiSliderParser(), ]; static final _actionParsers = [ diff --git a/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.dart b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.dart new file mode 100644 index 00000000..e83c04bd --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.dart @@ -0,0 +1,38 @@ +import 'package:flutter/material.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:mirai/src/parsers/mirai_mouse_cursor/mirai_mouse_cursor.dart'; + +export 'package:mirai/src/parsers/mirai_slider/mirai_slider_parser.dart'; + +part 'mirai_slider.freezed.dart'; +part 'mirai_slider.g.dart'; + +enum MiraiSliderType { adaptive, cupertino, material } + +@freezed +class MiraiSlider with _$MiraiSlider { + const factory MiraiSlider({ + String? id, + @Default(MiraiSliderType.material) MiraiSliderType sliderType, + required double value, + double? secondaryTrackValue, + Map? onChanged, + Map? onChangeStart, + Map? onChangeEnd, + @Default(0.0) double min, + @Default(1.0) double max, + int? divisions, + String? label, + String? activeColor, + String? inactiveColor, + String? secondaryActiveColor, + String? thumbColor, + String? overlayColor, + MiraiMouseCursor? mouseCursor, + @Default(false) bool autofocus, + SliderInteraction? allowedInteraction, + }) = _MiraiSlider; + + factory MiraiSlider.fromJson(Map json) => + _$MiraiSliderFromJson(json); +} diff --git a/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.freezed.dart b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.freezed.dart new file mode 100644 index 00000000..f8b2c96e --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.freezed.dart @@ -0,0 +1,595 @@ +// 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_slider.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'); + +MiraiSlider _$MiraiSliderFromJson(Map json) { + return _MiraiSlider.fromJson(json); +} + +/// @nodoc +mixin _$MiraiSlider { + String? get id => throw _privateConstructorUsedError; + MiraiSliderType get sliderType => throw _privateConstructorUsedError; + double get value => throw _privateConstructorUsedError; + double? get secondaryTrackValue => throw _privateConstructorUsedError; + Map? get onChanged => throw _privateConstructorUsedError; + Map? get onChangeStart => throw _privateConstructorUsedError; + Map? get onChangeEnd => throw _privateConstructorUsedError; + double get min => throw _privateConstructorUsedError; + double get max => throw _privateConstructorUsedError; + int? get divisions => throw _privateConstructorUsedError; + String? get label => throw _privateConstructorUsedError; + String? get activeColor => throw _privateConstructorUsedError; + String? get inactiveColor => throw _privateConstructorUsedError; + String? get secondaryActiveColor => throw _privateConstructorUsedError; + String? get thumbColor => throw _privateConstructorUsedError; + String? get overlayColor => throw _privateConstructorUsedError; + MiraiMouseCursor? get mouseCursor => throw _privateConstructorUsedError; + bool get autofocus => throw _privateConstructorUsedError; + SliderInteraction? get allowedInteraction => + throw _privateConstructorUsedError; + + /// Serializes this MiraiSlider to a JSON map. + Map toJson() => throw _privateConstructorUsedError; + + /// Create a copy of MiraiSlider + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + $MiraiSliderCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MiraiSliderCopyWith<$Res> { + factory $MiraiSliderCopyWith( + MiraiSlider value, $Res Function(MiraiSlider) then) = + _$MiraiSliderCopyWithImpl<$Res, MiraiSlider>; + @useResult + $Res call( + {String? id, + MiraiSliderType sliderType, + double value, + double? secondaryTrackValue, + Map? onChanged, + Map? onChangeStart, + Map? onChangeEnd, + double min, + double max, + int? divisions, + String? label, + String? activeColor, + String? inactiveColor, + String? secondaryActiveColor, + String? thumbColor, + String? overlayColor, + MiraiMouseCursor? mouseCursor, + bool autofocus, + SliderInteraction? allowedInteraction}); +} + +/// @nodoc +class _$MiraiSliderCopyWithImpl<$Res, $Val extends MiraiSlider> + implements $MiraiSliderCopyWith<$Res> { + _$MiraiSliderCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + /// Create a copy of MiraiSlider + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = freezed, + Object? sliderType = null, + Object? value = null, + Object? secondaryTrackValue = freezed, + Object? onChanged = freezed, + Object? onChangeStart = freezed, + Object? onChangeEnd = freezed, + Object? min = null, + Object? max = null, + Object? divisions = freezed, + Object? label = freezed, + Object? activeColor = freezed, + Object? inactiveColor = freezed, + Object? secondaryActiveColor = freezed, + Object? thumbColor = freezed, + Object? overlayColor = freezed, + Object? mouseCursor = freezed, + Object? autofocus = null, + Object? allowedInteraction = freezed, + }) { + return _then(_value.copyWith( + id: freezed == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String?, + sliderType: null == sliderType + ? _value.sliderType + : sliderType // ignore: cast_nullable_to_non_nullable + as MiraiSliderType, + value: null == value + ? _value.value + : value // ignore: cast_nullable_to_non_nullable + as double, + secondaryTrackValue: freezed == secondaryTrackValue + ? _value.secondaryTrackValue + : secondaryTrackValue // ignore: cast_nullable_to_non_nullable + as double?, + onChanged: freezed == onChanged + ? _value.onChanged + : onChanged // ignore: cast_nullable_to_non_nullable + as Map?, + onChangeStart: freezed == onChangeStart + ? _value.onChangeStart + : onChangeStart // ignore: cast_nullable_to_non_nullable + as Map?, + onChangeEnd: freezed == onChangeEnd + ? _value.onChangeEnd + : onChangeEnd // ignore: cast_nullable_to_non_nullable + as Map?, + min: null == min + ? _value.min + : min // ignore: cast_nullable_to_non_nullable + as double, + max: null == max + ? _value.max + : max // ignore: cast_nullable_to_non_nullable + as double, + divisions: freezed == divisions + ? _value.divisions + : divisions // ignore: cast_nullable_to_non_nullable + as int?, + label: freezed == label + ? _value.label + : label // ignore: cast_nullable_to_non_nullable + as String?, + activeColor: freezed == activeColor + ? _value.activeColor + : activeColor // ignore: cast_nullable_to_non_nullable + as String?, + inactiveColor: freezed == inactiveColor + ? _value.inactiveColor + : inactiveColor // ignore: cast_nullable_to_non_nullable + as String?, + secondaryActiveColor: freezed == secondaryActiveColor + ? _value.secondaryActiveColor + : secondaryActiveColor // ignore: cast_nullable_to_non_nullable + as String?, + thumbColor: freezed == thumbColor + ? _value.thumbColor + : thumbColor // ignore: cast_nullable_to_non_nullable + as String?, + overlayColor: freezed == overlayColor + ? _value.overlayColor + : overlayColor // ignore: cast_nullable_to_non_nullable + as String?, + mouseCursor: freezed == mouseCursor + ? _value.mouseCursor + : mouseCursor // ignore: cast_nullable_to_non_nullable + as MiraiMouseCursor?, + autofocus: null == autofocus + ? _value.autofocus + : autofocus // ignore: cast_nullable_to_non_nullable + as bool, + allowedInteraction: freezed == allowedInteraction + ? _value.allowedInteraction + : allowedInteraction // ignore: cast_nullable_to_non_nullable + as SliderInteraction?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$MiraiSliderImplCopyWith<$Res> + implements $MiraiSliderCopyWith<$Res> { + factory _$$MiraiSliderImplCopyWith( + _$MiraiSliderImpl value, $Res Function(_$MiraiSliderImpl) then) = + __$$MiraiSliderImplCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {String? id, + MiraiSliderType sliderType, + double value, + double? secondaryTrackValue, + Map? onChanged, + Map? onChangeStart, + Map? onChangeEnd, + double min, + double max, + int? divisions, + String? label, + String? activeColor, + String? inactiveColor, + String? secondaryActiveColor, + String? thumbColor, + String? overlayColor, + MiraiMouseCursor? mouseCursor, + bool autofocus, + SliderInteraction? allowedInteraction}); +} + +/// @nodoc +class __$$MiraiSliderImplCopyWithImpl<$Res> + extends _$MiraiSliderCopyWithImpl<$Res, _$MiraiSliderImpl> + implements _$$MiraiSliderImplCopyWith<$Res> { + __$$MiraiSliderImplCopyWithImpl( + _$MiraiSliderImpl _value, $Res Function(_$MiraiSliderImpl) _then) + : super(_value, _then); + + /// Create a copy of MiraiSlider + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = freezed, + Object? sliderType = null, + Object? value = null, + Object? secondaryTrackValue = freezed, + Object? onChanged = freezed, + Object? onChangeStart = freezed, + Object? onChangeEnd = freezed, + Object? min = null, + Object? max = null, + Object? divisions = freezed, + Object? label = freezed, + Object? activeColor = freezed, + Object? inactiveColor = freezed, + Object? secondaryActiveColor = freezed, + Object? thumbColor = freezed, + Object? overlayColor = freezed, + Object? mouseCursor = freezed, + Object? autofocus = null, + Object? allowedInteraction = freezed, + }) { + return _then(_$MiraiSliderImpl( + id: freezed == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String?, + sliderType: null == sliderType + ? _value.sliderType + : sliderType // ignore: cast_nullable_to_non_nullable + as MiraiSliderType, + value: null == value + ? _value.value + : value // ignore: cast_nullable_to_non_nullable + as double, + secondaryTrackValue: freezed == secondaryTrackValue + ? _value.secondaryTrackValue + : secondaryTrackValue // ignore: cast_nullable_to_non_nullable + as double?, + onChanged: freezed == onChanged + ? _value._onChanged + : onChanged // ignore: cast_nullable_to_non_nullable + as Map?, + onChangeStart: freezed == onChangeStart + ? _value._onChangeStart + : onChangeStart // ignore: cast_nullable_to_non_nullable + as Map?, + onChangeEnd: freezed == onChangeEnd + ? _value._onChangeEnd + : onChangeEnd // ignore: cast_nullable_to_non_nullable + as Map?, + min: null == min + ? _value.min + : min // ignore: cast_nullable_to_non_nullable + as double, + max: null == max + ? _value.max + : max // ignore: cast_nullable_to_non_nullable + as double, + divisions: freezed == divisions + ? _value.divisions + : divisions // ignore: cast_nullable_to_non_nullable + as int?, + label: freezed == label + ? _value.label + : label // ignore: cast_nullable_to_non_nullable + as String?, + activeColor: freezed == activeColor + ? _value.activeColor + : activeColor // ignore: cast_nullable_to_non_nullable + as String?, + inactiveColor: freezed == inactiveColor + ? _value.inactiveColor + : inactiveColor // ignore: cast_nullable_to_non_nullable + as String?, + secondaryActiveColor: freezed == secondaryActiveColor + ? _value.secondaryActiveColor + : secondaryActiveColor // ignore: cast_nullable_to_non_nullable + as String?, + thumbColor: freezed == thumbColor + ? _value.thumbColor + : thumbColor // ignore: cast_nullable_to_non_nullable + as String?, + overlayColor: freezed == overlayColor + ? _value.overlayColor + : overlayColor // ignore: cast_nullable_to_non_nullable + as String?, + mouseCursor: freezed == mouseCursor + ? _value.mouseCursor + : mouseCursor // ignore: cast_nullable_to_non_nullable + as MiraiMouseCursor?, + autofocus: null == autofocus + ? _value.autofocus + : autofocus // ignore: cast_nullable_to_non_nullable + as bool, + allowedInteraction: freezed == allowedInteraction + ? _value.allowedInteraction + : allowedInteraction // ignore: cast_nullable_to_non_nullable + as SliderInteraction?, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$MiraiSliderImpl implements _MiraiSlider { + const _$MiraiSliderImpl( + {this.id, + this.sliderType = MiraiSliderType.material, + required this.value, + this.secondaryTrackValue, + final Map? onChanged, + final Map? onChangeStart, + final Map? onChangeEnd, + this.min = 0.0, + this.max = 1.0, + this.divisions, + this.label, + this.activeColor, + this.inactiveColor, + this.secondaryActiveColor, + this.thumbColor, + this.overlayColor, + this.mouseCursor, + this.autofocus = false, + this.allowedInteraction}) + : _onChanged = onChanged, + _onChangeStart = onChangeStart, + _onChangeEnd = onChangeEnd; + + factory _$MiraiSliderImpl.fromJson(Map json) => + _$$MiraiSliderImplFromJson(json); + + @override + final String? id; + @override + @JsonKey() + final MiraiSliderType sliderType; + @override + final double value; + @override + final double? secondaryTrackValue; + final Map? _onChanged; + @override + Map? get onChanged { + final value = _onChanged; + if (value == null) return null; + if (_onChanged is EqualUnmodifiableMapView) return _onChanged; + // ignore: implicit_dynamic_type + return EqualUnmodifiableMapView(value); + } + + final Map? _onChangeStart; + @override + Map? get onChangeStart { + final value = _onChangeStart; + if (value == null) return null; + if (_onChangeStart is EqualUnmodifiableMapView) return _onChangeStart; + // ignore: implicit_dynamic_type + return EqualUnmodifiableMapView(value); + } + + final Map? _onChangeEnd; + @override + Map? get onChangeEnd { + final value = _onChangeEnd; + if (value == null) return null; + if (_onChangeEnd is EqualUnmodifiableMapView) return _onChangeEnd; + // ignore: implicit_dynamic_type + return EqualUnmodifiableMapView(value); + } + + @override + @JsonKey() + final double min; + @override + @JsonKey() + final double max; + @override + final int? divisions; + @override + final String? label; + @override + final String? activeColor; + @override + final String? inactiveColor; + @override + final String? secondaryActiveColor; + @override + final String? thumbColor; + @override + final String? overlayColor; + @override + final MiraiMouseCursor? mouseCursor; + @override + @JsonKey() + final bool autofocus; + @override + final SliderInteraction? allowedInteraction; + + @override + String toString() { + return 'MiraiSlider(id: $id, sliderType: $sliderType, value: $value, secondaryTrackValue: $secondaryTrackValue, onChanged: $onChanged, onChangeStart: $onChangeStart, onChangeEnd: $onChangeEnd, min: $min, max: $max, divisions: $divisions, label: $label, activeColor: $activeColor, inactiveColor: $inactiveColor, secondaryActiveColor: $secondaryActiveColor, thumbColor: $thumbColor, overlayColor: $overlayColor, mouseCursor: $mouseCursor, autofocus: $autofocus, allowedInteraction: $allowedInteraction)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$MiraiSliderImpl && + (identical(other.id, id) || other.id == id) && + (identical(other.sliderType, sliderType) || + other.sliderType == sliderType) && + (identical(other.value, value) || other.value == value) && + (identical(other.secondaryTrackValue, secondaryTrackValue) || + other.secondaryTrackValue == secondaryTrackValue) && + const DeepCollectionEquality() + .equals(other._onChanged, _onChanged) && + const DeepCollectionEquality() + .equals(other._onChangeStart, _onChangeStart) && + const DeepCollectionEquality() + .equals(other._onChangeEnd, _onChangeEnd) && + (identical(other.min, min) || other.min == min) && + (identical(other.max, max) || other.max == max) && + (identical(other.divisions, divisions) || + other.divisions == divisions) && + (identical(other.label, label) || other.label == label) && + (identical(other.activeColor, activeColor) || + other.activeColor == activeColor) && + (identical(other.inactiveColor, inactiveColor) || + other.inactiveColor == inactiveColor) && + (identical(other.secondaryActiveColor, secondaryActiveColor) || + other.secondaryActiveColor == secondaryActiveColor) && + (identical(other.thumbColor, thumbColor) || + other.thumbColor == thumbColor) && + (identical(other.overlayColor, overlayColor) || + other.overlayColor == overlayColor) && + (identical(other.mouseCursor, mouseCursor) || + other.mouseCursor == mouseCursor) && + (identical(other.autofocus, autofocus) || + other.autofocus == autofocus) && + (identical(other.allowedInteraction, allowedInteraction) || + other.allowedInteraction == allowedInteraction)); + } + + @JsonKey(includeFromJson: false, includeToJson: false) + @override + int get hashCode => Object.hashAll([ + runtimeType, + id, + sliderType, + value, + secondaryTrackValue, + const DeepCollectionEquality().hash(_onChanged), + const DeepCollectionEquality().hash(_onChangeStart), + const DeepCollectionEquality().hash(_onChangeEnd), + min, + max, + divisions, + label, + activeColor, + inactiveColor, + secondaryActiveColor, + thumbColor, + overlayColor, + mouseCursor, + autofocus, + allowedInteraction + ]); + + /// Create a copy of MiraiSlider + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + @override + @pragma('vm:prefer-inline') + _$$MiraiSliderImplCopyWith<_$MiraiSliderImpl> get copyWith => + __$$MiraiSliderImplCopyWithImpl<_$MiraiSliderImpl>(this, _$identity); + + @override + Map toJson() { + return _$$MiraiSliderImplToJson( + this, + ); + } +} + +abstract class _MiraiSlider implements MiraiSlider { + const factory _MiraiSlider( + {final String? id, + final MiraiSliderType sliderType, + required final double value, + final double? secondaryTrackValue, + final Map? onChanged, + final Map? onChangeStart, + final Map? onChangeEnd, + final double min, + final double max, + final int? divisions, + final String? label, + final String? activeColor, + final String? inactiveColor, + final String? secondaryActiveColor, + final String? thumbColor, + final String? overlayColor, + final MiraiMouseCursor? mouseCursor, + final bool autofocus, + final SliderInteraction? allowedInteraction}) = _$MiraiSliderImpl; + + factory _MiraiSlider.fromJson(Map json) = + _$MiraiSliderImpl.fromJson; + + @override + String? get id; + @override + MiraiSliderType get sliderType; + @override + double get value; + @override + double? get secondaryTrackValue; + @override + Map? get onChanged; + @override + Map? get onChangeStart; + @override + Map? get onChangeEnd; + @override + double get min; + @override + double get max; + @override + int? get divisions; + @override + String? get label; + @override + String? get activeColor; + @override + String? get inactiveColor; + @override + String? get secondaryActiveColor; + @override + String? get thumbColor; + @override + String? get overlayColor; + @override + MiraiMouseCursor? get mouseCursor; + @override + bool get autofocus; + @override + SliderInteraction? get allowedInteraction; + + /// Create a copy of MiraiSlider + /// with the given fields replaced by the non-null parameter values. + @override + @JsonKey(includeFromJson: false, includeToJson: false) + _$$MiraiSliderImplCopyWith<_$MiraiSliderImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.g.dart b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.g.dart new file mode 100644 index 00000000..8171bb4a --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider.g.dart @@ -0,0 +1,110 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mirai_slider.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$MiraiSliderImpl _$$MiraiSliderImplFromJson(Map json) => + _$MiraiSliderImpl( + id: json['id'] as String?, + sliderType: + $enumDecodeNullable(_$MiraiSliderTypeEnumMap, json['sliderType']) ?? + MiraiSliderType.material, + value: (json['value'] as num).toDouble(), + secondaryTrackValue: (json['secondaryTrackValue'] as num?)?.toDouble(), + onChanged: json['onChanged'] as Map?, + onChangeStart: json['onChangeStart'] as Map?, + onChangeEnd: json['onChangeEnd'] as Map?, + min: (json['min'] as num?)?.toDouble() ?? 0.0, + max: (json['max'] as num?)?.toDouble() ?? 1.0, + divisions: (json['divisions'] as num?)?.toInt(), + label: json['label'] as String?, + activeColor: json['activeColor'] as String?, + inactiveColor: json['inactiveColor'] as String?, + secondaryActiveColor: json['secondaryActiveColor'] as String?, + thumbColor: json['thumbColor'] as String?, + overlayColor: json['overlayColor'] as String?, + mouseCursor: + $enumDecodeNullable(_$MiraiMouseCursorEnumMap, json['mouseCursor']), + autofocus: json['autofocus'] as bool? ?? false, + allowedInteraction: $enumDecodeNullable( + _$SliderInteractionEnumMap, json['allowedInteraction']), + ); + +Map _$$MiraiSliderImplToJson(_$MiraiSliderImpl instance) => + { + 'id': instance.id, + 'sliderType': _$MiraiSliderTypeEnumMap[instance.sliderType]!, + 'value': instance.value, + 'secondaryTrackValue': instance.secondaryTrackValue, + 'onChanged': instance.onChanged, + 'onChangeStart': instance.onChangeStart, + 'onChangeEnd': instance.onChangeEnd, + 'min': instance.min, + 'max': instance.max, + 'divisions': instance.divisions, + 'label': instance.label, + 'activeColor': instance.activeColor, + 'inactiveColor': instance.inactiveColor, + 'secondaryActiveColor': instance.secondaryActiveColor, + 'thumbColor': instance.thumbColor, + 'overlayColor': instance.overlayColor, + 'mouseCursor': _$MiraiMouseCursorEnumMap[instance.mouseCursor], + 'autofocus': instance.autofocus, + 'allowedInteraction': + _$SliderInteractionEnumMap[instance.allowedInteraction], + }; + +const _$MiraiSliderTypeEnumMap = { + MiraiSliderType.adaptive: 'adaptive', + MiraiSliderType.cupertino: 'cupertino', + MiraiSliderType.material: 'material', +}; + +const _$MiraiMouseCursorEnumMap = { + MiraiMouseCursor.none: 'none', + MiraiMouseCursor.basic: 'basic', + MiraiMouseCursor.click: 'click', + MiraiMouseCursor.forbidden: 'forbidden', + MiraiMouseCursor.wait: 'wait', + MiraiMouseCursor.progress: 'progress', + MiraiMouseCursor.contextMenu: 'contextMenu', + MiraiMouseCursor.help: 'help', + MiraiMouseCursor.text: 'text', + MiraiMouseCursor.verticalText: 'verticalText', + MiraiMouseCursor.cell: 'cell', + MiraiMouseCursor.precise: 'precise', + MiraiMouseCursor.move: 'move', + MiraiMouseCursor.grab: 'grab', + MiraiMouseCursor.grabbing: 'grabbing', + MiraiMouseCursor.noDrop: 'noDrop', + MiraiMouseCursor.alias: 'alias', + MiraiMouseCursor.copy: 'copy', + MiraiMouseCursor.disappearing: 'disappearing', + MiraiMouseCursor.allScroll: 'allScroll', + MiraiMouseCursor.resizeLeftRight: 'resizeLeftRight', + MiraiMouseCursor.resizeUpDown: 'resizeUpDown', + MiraiMouseCursor.resizeUpLeftDownRight: 'resizeUpLeftDownRight', + MiraiMouseCursor.resizeUpRightDownLeft: 'resizeUpRightDownLeft', + MiraiMouseCursor.resizeUp: 'resizeUp', + MiraiMouseCursor.resizeDown: 'resizeDown', + MiraiMouseCursor.resizeLeft: 'resizeLeft', + MiraiMouseCursor.resizeRight: 'resizeRight', + MiraiMouseCursor.resizeUpLeft: 'resizeUpLeft', + MiraiMouseCursor.resizeUpRight: 'resizeUpRight', + MiraiMouseCursor.resizeDownLeft: 'resizeDownLeft', + MiraiMouseCursor.resizeDownRight: 'resizeDownRight', + MiraiMouseCursor.resizeColumn: 'resizeColumn', + MiraiMouseCursor.resizeRow: 'resizeRow', + MiraiMouseCursor.zoomIn: 'zoomIn', + MiraiMouseCursor.zoomOut: 'zoomOut', +}; + +const _$SliderInteractionEnumMap = { + SliderInteraction.tapAndSlide: 'tapAndSlide', + SliderInteraction.tapOnly: 'tapOnly', + SliderInteraction.slideOnly: 'slideOnly', + SliderInteraction.slideThumb: 'slideThumb', +}; diff --git a/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider_parser.dart b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider_parser.dart new file mode 100644 index 00000000..ccf9be0e --- /dev/null +++ b/packages/mirai/lib/src/parsers/mirai_slider/mirai_slider_parser.dart @@ -0,0 +1,158 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:mirai/mirai.dart'; +import 'package:mirai/src/utils/widget_type.dart'; + +import '../mirai_form/mirai_form_scope.dart'; + +class MiraiSliderParser extends MiraiParser { + const MiraiSliderParser(); + + @override + String get type => WidgetType.slider.name; + + @override + MiraiSlider getModel(Map json) => MiraiSlider.fromJson(json); + + @override + Widget parse(BuildContext context, MiraiSlider model) { + return _MiraiSlider(model, MiraiFormScope.of(context)); + } +} + +class _MiraiSlider extends StatefulWidget { + const _MiraiSlider(this.model, this.formScope); + + final MiraiSlider model; + final MiraiFormScope? formScope; + + @override + State<_MiraiSlider> createState() => __MiraiSliderState(); +} + +class __MiraiSliderState extends State<_MiraiSlider> { + late double selectedValue; + + @override + void initState() { + selectedValue = widget.model.value; + if (widget.model.id != null) { + widget.formScope?.formData[widget.model.id!] = selectedValue; + } + super.initState(); + } + + void _onChanged(double value) { + selectedValue = value; + if (widget.model.onChanged != null) { + Mirai.onCallFromJson(widget.model.onChanged, context); + } + if (widget.model.id != null) { + widget.formScope?.formData[widget.model.id!] = value; + } + setState(() {}); + } + + void _onChangeStart(double value) { + if (widget.model.onChangeStart != null) { + Mirai.onCallFromJson(widget.model.onChangeStart, context); + } + } + + void _onChangeEnd(double value) { + if (widget.model.onChangeEnd != null) { + Mirai.onCallFromJson(widget.model.onChangeEnd, context); + } + } + + @override + Widget build(BuildContext context) { + final MiraiSlider model = widget.model; + final FocusNode focusNode = FocusNode(); + + switch (model.sliderType) { + case MiraiSliderType.material: + return _buildMaterialSlider(model, focusNode, selectedValue); + case MiraiSliderType.adaptive: + return _buildAdaptiveSlider(model, focusNode, selectedValue); + case MiraiSliderType.cupertino: + return _buildCupertinoSlider(model, focusNode, selectedValue); + } + } + + Widget _buildMaterialSlider( + MiraiSlider model, + FocusNode focusNode, + double value, + ) { + return Slider( + value: value, + secondaryTrackValue: model.secondaryTrackValue, + onChanged: (value) => _onChanged(value), + onChangeStart: (value) => _onChangeStart(value), + onChangeEnd: (value) => _onChangeEnd(value), + min: model.min, + max: model.max, + divisions: model.divisions, + label: model.label, + activeColor: model.activeColor?.toColor(context), + inactiveColor: model.inactiveColor?.toColor(context), + secondaryActiveColor: model.secondaryActiveColor?.toColor(context), + thumbColor: model.thumbColor?.toColor(context), + overlayColor: WidgetStateProperty.all( + model.overlayColor?.toColor(context), + ), + mouseCursor: model.mouseCursor?.value, + focusNode: focusNode, + autofocus: model.autofocus, + allowedInteraction: model.allowedInteraction, + ); + } + + Widget _buildAdaptiveSlider( + MiraiSlider model, + FocusNode focusNode, + double value, + ) { + return Slider.adaptive( + value: value, + secondaryTrackValue: model.secondaryTrackValue, + onChanged: (value) => _onChanged(value), + onChangeStart: (value) => _onChangeStart(value), + onChangeEnd: (value) => _onChangeEnd(value), + min: model.min, + max: model.max, + divisions: model.divisions, + label: model.label, + activeColor: model.activeColor?.toColor(context), + inactiveColor: model.inactiveColor?.toColor(context), + secondaryActiveColor: model.secondaryActiveColor?.toColor(context), + thumbColor: model.thumbColor?.toColor(context), + overlayColor: WidgetStateProperty.all( + model.overlayColor?.toColor(context), + ), + mouseCursor: model.mouseCursor?.value, + focusNode: focusNode, + autofocus: model.autofocus, + allowedInteraction: model.allowedInteraction, + ); + } + + Widget _buildCupertinoSlider( + MiraiSlider model, + FocusNode focusNode, + double value, + ) { + return CupertinoSlider( + value: value, + onChanged: (value) => _onChanged(value), + onChangeStart: (value) => _onChangeStart(value), + onChangeEnd: (value) => _onChangeEnd(value), + min: model.min, + max: model.max, + divisions: model.divisions, + activeColor: model.activeColor?.toColor(context), + thumbColor: model.thumbColor?.toColor(context) ?? CupertinoColors.white, + ); + } +} diff --git a/packages/mirai/lib/src/parsers/parsers.dart b/packages/mirai/lib/src/parsers/parsers.dart index 7d334330..b5574f8f 100644 --- a/packages/mirai/lib/src/parsers/parsers.dart +++ b/packages/mirai/lib/src/parsers/parsers.dart @@ -84,3 +84,4 @@ export 'package:mirai/src/parsers/mirai_hero/mirai_hero.dart'; export 'package:mirai/src/parsers/mirai_rect_tween/mirai_rect_tween.dart'; export 'package:mirai/src/parsers/mirai_radio/mirai_radio.dart'; export 'package:mirai/src/parsers/mirai_radio_group/mirai_radio_group.dart'; +export 'package:mirai/src/parsers/mirai_slider/mirai_slider.dart'; diff --git a/packages/mirai/lib/src/utils/widget_type.dart b/packages/mirai/lib/src/utils/widget_type.dart index f553434a..3d285381 100644 --- a/packages/mirai/lib/src/utils/widget_type.dart +++ b/packages/mirai/lib/src/utils/widget_type.dart @@ -59,4 +59,5 @@ enum WidgetType { hero, radio, radioGroup, + slider, } diff --git a/website/docs/widgets/circular_progress_indicator.md b/website/docs/widgets/circular_progress_indicator.md index 4781c0d3..85cfcfe0 100644 --- a/website/docs/widgets/circular_progress_indicator.md +++ b/website/docs/widgets/circular_progress_indicator.md @@ -1,7 +1,7 @@ # CircularProgressIndicator Mirai circularProgressIndicator allows you to build the Flutter CircularProgressIndicator widget using JSON. -To know more about the container widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/CircularProgressIndicator-class.html). +To know more about the CircularProgressIndicator widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/CircularProgressIndicator-class.html). ## Properties diff --git a/website/docs/widgets/linear_progress_indicator.md b/website/docs/widgets/linear_progress_indicator.md index e7a13a9c..4a96b01e 100644 --- a/website/docs/widgets/linear_progress_indicator.md +++ b/website/docs/widgets/linear_progress_indicator.md @@ -1,7 +1,7 @@ # LinearProgressIndicator Mirai linearProgressIndicator allows you to build the Flutter LinearProgressIndicator widget using JSON. -To know more about the container widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/LinearProgressIndicator-class.html). +To know more about the LinearProgressIndicator widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/LinearProgressIndicator-class.html). ## Properties diff --git a/website/docs/widgets/radio_group.md b/website/docs/widgets/radio_group.md index eeae7208..ef4f3f0b 100644 --- a/website/docs/widgets/radio_group.md +++ b/website/docs/widgets/radio_group.md @@ -1,7 +1,7 @@ # RadioGroup Mirai radioGroup allows you to build the Flutter Radio widgets using JSON. -To know more about the container widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/Radio-class.html). +To know more about the Radio widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/Radio-class.html). ## RadioGroup Properties diff --git a/website/docs/widgets/slider.md b/website/docs/widgets/slider.md new file mode 100644 index 00000000..bc07a18f --- /dev/null +++ b/website/docs/widgets/slider.md @@ -0,0 +1,57 @@ +# Slider + +Mirai slider allows you to build the Flutter Slider widget using JSON. +To know more about the Slider widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/Slider-class.html). + +## Properties + +| Property | Type | Description | +| --- |-------------------|---------------------------------------------------| +| id | `String` | The id will be used to save the selected value of slider. | +| sliderType | `MiraiSliderType` | The type of slider. | +| value | `MiraiSliderType` | The currently selected value for this slider. | +| secondaryTrackValue | `double` | The secondary track used to draw progress between the thumb and this value, over the inactive track. | +| onChanged | `Map` | Called during a drag when the user is selecting a new value for the slider. | +| onChangeStart | `Map` | Called when the user starts selecting a new value for the slider. | +| onChangeEnd | `Map` | Called when the user is done selecting a new value for the slider. | +| min | `double` | The minimum value the user can select. | +| max | `double` | The maximum value the user can select. | +| divisions | `int` | The number of discrete divisions. | +| label | `String` | A label to show above the slider when the slider is active | +| activeColor | `String` | The color to use for the portion of the slider track that is active. | +| inactiveColor | `String` | The color for the inactive portion of the slider track. | +| secondaryActiveColor | `String` | The color to use for the portion of the slider track between the thumb and secondaryTrackValue | +| thumbColor | `String` | The color of the thumb. | +| overlayColor | `String` | The highlight color that's typically used to indicate that the slider thumb is focused, hovered, or dragged. | +| mouseCursor | `MiraiMouseCursor` | The cursor for a mouse pointer when it enters or is hovering over the widget. | +| autofocus | `bool` | True if this widget will be selected as the initial focus when no other node in its scope is currently focused. | +| allowedInteraction | `SliderInteraction` | Allowed way for the user to interact with the slider. | + +## Example JSON + +```json +{ + "type": "scaffold", + "appBar": { + "type": "appBar", + "title": { + "type": "text", + "data": "Mirai Slider" + } + }, + "body": { + "type": "form", + "child": { + "type": "center", + "child": { + "id": "example_slider", + "type": "slider", + "sliderType": "material", + "value": 20, + "max": 100, + "divisions": 5 + } + } + } +} +``` \ No newline at end of file