Skip to content

Commit

Permalink
Refactor Mirai Switch Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
divyanshub024 committed Mar 2, 2024
1 parent 302719d commit a7c158c
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 396 deletions.
17 changes: 6 additions & 11 deletions examples/mirai_gallery/assets/json/switch_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,27 @@
"crossAxisAlignment": "center",
"children": [
{
"type": "switchButton",
"type": "switch",
"switchType": "cupertino",
"initialValue": true,
"onChanged": {}
"value": true
},
{
"type": "sizedBox",
"width": 20
},
{
"type": "switchButton",
"type": "switch",
"switchType": "adaptive",
"initialValue": false,
"onChanged": {},
"inActiveColor": "#FF0000",
"activeColor": "#0000FF"
"value": true
},
{
"type": "sizedBox",
"width": 20
},
{
"type": "switchButton",
"type": "switch",
"switchType": "material",
"initialValue": false,
"onChanged": {}
"value": false
}
]
},
Expand Down
78 changes: 10 additions & 68 deletions packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:mirai/mirai.dart';
import 'package:mirai/src/parsers/mirai_material_color/mirai_material_color.dart';

export 'package:mirai/src/parsers/mirai_switch/mirai_switch_parser.dart';
Expand All @@ -13,91 +12,34 @@ enum MiraiSwitchType { adaptive, cupertino, material }

@freezed
class MiraiSwitch with _$MiraiSwitch {
const MiraiSwitch._();

const factory MiraiSwitch({
@Default(MiraiSwitchType.material) MiraiSwitchType switchType,
@Default(false) initialValue,
@Default(false) value,
Map<String, dynamic>? onChanged,
@Default(false) bool autofocus,
@Default(false) bool disabled,
String? activeColor,
String? activeTrackColor,
String? focusColor,
String? hoverColor,
String? inactiveThumbColor,
String? inactiveTrackColor,
String? onLabelColor,
String? offLabelColor,
double? splashRadius,
String? dragStartBehavior,
@Default(DragStartBehavior.start) DragStartBehavior dragStartBehavior,
MiraiMaterialColor? overlayColor,
MiraiMaterialColor? thumbColor,
MiraiMaterialColor? trackColor,
String? materialTapTargetSize,
MaterialTapTargetSize? materialTapTargetSize,
MiraiMaterialColor? trackOutlineColor,
double? trackOutlineWidth,
MiraiIcon? thumbIcon,
MiraiImage? inactiveThumbImage,
MiraiImage? activeThumbImage,
Map<String, dynamic>? thumbIcon,
String? inactiveThumbImage,
String? activeThumbImage,
bool? applyTheme,
bool? applyCupertinoTheme,
}) = _MiraiSwitch;

factory MiraiSwitch.fromJson(Map<String, dynamic> json) =>
_$MiraiSwitchFromJson(json);

DragStartBehavior get dragStateBehaviorValue {
return DragStartBehavior.values.firstWhere(
(element) => element.name == dragStartBehavior,
orElse: () => DragStartBehavior.start,
);
}

Color? activeColorValue(BuildContext context) =>
activeColor?.toColor(context);

Color? activeTrackColorValue(BuildContext context) =>
activeTrackColor?.toColor(context);

Color? focusColorValue(BuildContext context) => focusColor?.toColor(context);

Color? hoverColorValue(BuildContext context) => hoverColor?.toColor(context);

Color? inactiveThumbColorValue(BuildContext context) =>
inactiveThumbColor?.toColor(context);

Color? inactiveTrackColorValue(BuildContext context) =>
inactiveTrackColor?.toColor(context);

MaterialTapTargetSize? get materialTapTargetSizeValue {
return MaterialTapTargetSize.values.firstWhere(
(element) => element.name == materialTapTargetSize,
orElse: () => MaterialTapTargetSize.padded,
);
}

Icon? thumbIconWidget(BuildContext context) {
if (thumbIcon == null) return null;

final Widget? widget = Mirai.fromJson(thumbIcon!.toJson(), context);
if (widget != null && widget is Icon) {
return widget;
}

return null;
}

ImageProvider? inactiveThumbImageWidget(BuildContext context) =>
_getImageProvider(context, image: inactiveThumbImage);

ImageProvider? activeThumbImageWidget(BuildContext context) =>
_getImageProvider(context, image: activeThumbImage);

ImageProvider? _getImageProvider(BuildContext context, {MiraiImage? image}) {
if (image == null && (image?.src ?? '').isEmpty) return null;

final Widget? widget = Mirai.fromJson(image!.toJson(), context);
if (widget != null && widget is Image) {
return widget.image;
}

return null;
}
}
Loading

0 comments on commit a7c158c

Please sign in to comment.