Skip to content

Commit

Permalink
Merge pull request #207 from kushalmahapatro/feature/add-switch
Browse files Browse the repository at this point in the history
feat: Add Mirai Switch
  • Loading branch information
divyanshub024 authored Sep 30, 2023
2 parents d23453b + 3f70210 commit 1591c7b
Show file tree
Hide file tree
Showing 9 changed files with 1,189 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/mirai_gallery/assets/json/home_screen.json
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,37 @@
}
}
},
{
"type": "listTile",
"leading": {
"type": "icon",
"iconType": "material",
"icon": "toggle_on"
},
"title": {
"type": "text",
"data": "Mirai Switch",
"style": {
"fontSize": 21
}
},
"subtitle": {
"type": "text",
"data": "A switch component that can be toggled via dragging or tapping on it.",
"style": {
"fontSize": 12
}
},
"isThreeLine": true,
"onTap": {
"actionType": "navigate",
"navigationStyle": "push",
"widgetJson": {
"type": "exampleScreen",
"assetPath": "assets/json/switch_example.json"
}
}
},
{
"type": "listTile",
"leading": {
Expand Down
63 changes: 63 additions & 0 deletions examples/mirai_gallery/assets/json/switch_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"type": "scaffold",
"appBar": {
"type": "appBar",
"title": {
"type": "text",
"data": "Mirai Switch"
}
},
"body": {
"type": "row",
"mainAxisAlignment": "center",
"crossAxisAlignment": "center",
"children": [
{
"type": "column",
"mainAxisAlignment": "center",
"crossAxisAlignment": "center",
"children": [
{
"type": "row",
"mainAxisAlignment": "center",
"crossAxisAlignment": "center",
"children": [
{
"type": "switchButton",
"switchType" : "cupertino",
"initialValue": true,
"onChanged": {}
},
{
"type": "sizedBox",
"width": 20
},
{
"type": "switchButton",
"switchType" : "adaptive",
"initialValue": false,
"onChanged": {},
"inActiveColor": "#FF0000",
"activeColor": "#0000FF"
},
{
"type": "sizedBox",
"width": 20
},
{
"type": "switchButton",
"switchType" : "material",
"initialValue": false,
"onChanged": {}
}
]
},
{
"type": "sizedBox",
"height": 12
}
]
}
]
}
}
2 changes: 2 additions & 0 deletions packages/mirai/lib/src/framework/mirai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:mirai/src/parsers/mirai_check_box_widget/mirai_check_box_widget_
import 'package:mirai/src/parsers/mirai_form/mirai_form_parser.dart';
import 'package:mirai/src/parsers/mirai_form_field/mirai_form_field_parser.dart';
import 'package:mirai/src/parsers/mirai_fractionally_sized_box/mirai_fractionally_sized_box_parser.dart';
import 'package:mirai/src/parsers/mirai_switch/mirai_switch_parser.dart';
import 'package:mirai/src/parsers/mirai_tab/mirai_tab_parser.dart';
import 'package:mirai/src/parsers/parsers.dart';
import 'package:mirai/src/utils/log.dart';
Expand Down Expand Up @@ -64,6 +65,7 @@ class Mirai {
const MiraiExpandedParser(),
const MiraiFlexibleParser(),
const MiraiSafeAreaParser(),
const MiraiSwitchParser(),
];

static final _actionParsers = <MiraiActionParser>[
Expand Down
97 changes: 97 additions & 0 deletions packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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';

part 'mirai_switch.freezed.dart';
part 'mirai_switch.g.dart';

enum MiraiSwitchType { adaptive, cupertino, material }

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

const factory MiraiSwitch({
@Default(MiraiSwitchType.material) MiraiSwitchType switchType,
@Default(false) initialValue,
Map<String, dynamic>? 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,
MiraiMaterialColor? overlayColor,
MiraiMaterialColor? thumbColor,
MiraiMaterialColor? trackColor,
String? materialTapTargetSize,
MiraiMaterialColor? trackOutlineColor,
double? trackOutlineWidth,
MiraiIcon? thumbIcon,
MiraiImage? inactiveThumbImage,
MiraiImage? activeThumbImage,
}) = _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? 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;

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 1591c7b

Please sign in to comment.