-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from kushalmahapatro/feature/add-switch
feat: Add Mirai Switch
- Loading branch information
Showing
9 changed files
with
1,189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/mirai/lib/src/parsers/mirai_switch/mirai_switch.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.