From 8de0ca6960da7417b731ea1dc510f0ff35448bd6 Mon Sep 17 00:00:00 2001 From: Anthony Date: Fri, 6 Dec 2024 10:28:48 +0700 Subject: [PATCH] v6.8.0 --- CHANGELOG.md | 7 + example/pubspec.lock | 14 +- lib/widgets/fields/field_base_state.dart | 21 ++ lib/widgets/fields/form_checkbox.dart | 79 +++---- lib/widgets/fields/form_chips.dart | 3 +- lib/widgets/fields/form_date_time_picker.dart | 3 +- lib/widgets/fields/form_picker.dart | 6 +- lib/widgets/fields/form_switch_box.dart | 219 ++++++++++++++++++ lib/widgets/form/casts.dart | 92 ++++++++ lib/widgets/form/form_item.dart | 31 +++ lib/widgets/ny_form.dart | 100 +++++++- pubspec.lock | 12 +- pubspec.yaml | 4 +- 13 files changed, 532 insertions(+), 59 deletions(-) create mode 100644 lib/widgets/fields/form_switch_box.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index bf109c6..2d14787 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [6.8.0] - 2024-12-06 + +* New `Field.switchBox` added to `NyForm` to create a switch box field +* Localization support for picker and datetime fields +* `currency.toLowerCase()` in Field.currency() +* Update pubspec.yaml + ## [6.7.1] - 2024-11-29 * Fix BadgeTab diff --git a/example/pubspec.lock b/example/pubspec.lock index 62227c5..06566f6 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -317,6 +317,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + http: + dependency: transitive + description: + name: http + sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 + url: "https://pub.dev" + source: hosted + version: "1.2.2" http_parser: dependency: transitive description: @@ -403,7 +411,7 @@ packages: path: ".." relative: true source: path - version: "6.5.0" + version: "6.7.2" path: dependency: transitive description: @@ -653,10 +661,10 @@ packages: dependency: transitive description: name: timezone - sha256: "2236ec079a174ce07434e89fcd3fcda430025eb7692244139a9cf54fdcf1fc7d" + sha256: ffc9d5f4d1193534ef051f9254063fa53d588609418c84299956c3db9383587d url: "https://pub.dev" source: hosted - version: "0.9.4" + version: "0.10.0" typed_data: dependency: transitive description: diff --git a/lib/widgets/fields/field_base_state.dart b/lib/widgets/fields/field_base_state.dart index 5396432..3d8a79f 100644 --- a/lib/widgets/fields/field_base_state.dart +++ b/lib/widgets/fields/field_base_state.dart @@ -23,6 +23,27 @@ abstract class FieldBaseState extends State { return field.cast.metaData![name]; } + /// Get the widget state property for color + WidgetStateProperty? getWidgetStatePropertyColor(String key, + {Color? defaultValue}) { + Color? colorMetaData = getFieldMeta(key, defaultValue); + if (colorMetaData == null) { + return null; + } + Color? thumbColorMetaData = color(light: colorMetaData, dark: Colors.black); + return WidgetStateProperty.all(thumbColorMetaData); + } + + /// Get the widget state property for icon + WidgetStateProperty? getWidgetStatePropertyIcon(String key, + {Icon? defaultValue}) { + Icon? iconMetaData = getFieldMeta(key, defaultValue); + if (iconMetaData == null) { + return null; + } + return WidgetStateProperty.all(iconMetaData); + } + /// Get the headerSpacing from the field double getHeaderSpacing() => getFieldMeta("headerSpacing", 5); diff --git a/lib/widgets/fields/form_checkbox.dart b/lib/widgets/fields/form_checkbox.dart index 793bfe3..2e5d0c5 100644 --- a/lib/widgets/fields/form_checkbox.dart +++ b/lib/widgets/fields/form_checkbox.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:nylo_support/localization/app_localization.dart'; import '/widgets/fields/field_base_state.dart'; import '/widgets/ny_form.dart'; import 'package:recase/recase.dart'; @@ -115,17 +116,17 @@ class _NyFormCheckboxState extends FieldBaseState { @override Widget view(BuildContext context) { - Widget? title = getMetaData('title'); + Widget? title = getFieldMeta('title', null); title ??= Text( - widget.field.name.titleCase, + widget.field.name.titleCase.tr(), style: TextStyle( color: color(light: Colors.black, dark: Colors.white), ), ); if (title is Text && (title.data == null || title.data!.isEmpty)) { title = Text( - widget.field.name.titleCase, + widget.field.name.titleCase.tr(), style: TextStyle( color: color(light: Colors.black, dark: Colors.white), ), @@ -133,19 +134,19 @@ class _NyFormCheckboxState extends FieldBaseState { } Color? fillColorMetaData = color( - light: getMetaData('fillColor') ?? Colors.transparent, + light: getFieldMeta('fillColor', null) ?? Colors.transparent, dark: Colors.black); WidgetStateProperty? fillColor = WidgetStateProperty.all(fillColorMetaData); - Color? overlayColorMetaData = getMetaData('overlayColor'); + Color? overlayColorMetaData = getFieldMeta('overlayColor', null); WidgetStateProperty? overlayColor; if (overlayColorMetaData != null) { overlayColor = WidgetStateProperty.all(overlayColorMetaData); } return CheckboxListTile( - mouseCursor: getMetaData('mouseCursor'), + mouseCursor: getFieldMeta('mouseCursor', null), title: title, value: currentValue, onChanged: (value) { @@ -156,52 +157,42 @@ class _NyFormCheckboxState extends FieldBaseState { } }); }, - controlAffinity: ListTileControlAffinity.leading, + controlAffinity: + getFieldMeta("controlAffinity", ListTileControlAffinity.platform), activeColor: color( - light: getMetaData('activeColor') ?? Colors.black, + light: getFieldMeta('activeColor', null) ?? Colors.black, dark: Colors.black), fillColor: fillColor, checkColor: color( - light: getMetaData('checkColor') ?? Colors.black, dark: Colors.white), - hoverColor: getMetaData('hoverColor'), + light: getFieldMeta('checkColor', null) ?? Colors.black, + dark: Colors.white), + hoverColor: getFieldMeta('hoverColor', null), overlayColor: overlayColor, - splashRadius: getMetaData('splashRadius'), - materialTapTargetSize: getMetaData('materialTapTargetSize'), - visualDensity: getMetaData('visualDensity'), - focusNode: getMetaData('focusNode'), - autofocus: getMetaData('autofocus'), - shape: getMetaData('shape'), + splashRadius: getFieldMeta('splashRadius', null), + materialTapTargetSize: getFieldMeta('materialTapTargetSize', null), + visualDensity: getFieldMeta('visualDensity', null), + focusNode: getFieldMeta('focusNode', null), + autofocus: getFieldMeta('autofocus', false), + shape: getFieldMeta('shape', null), side: whenTheme( - light: () => getMetaData('side'), + light: () => getFieldMeta('side', null), dark: () => BorderSide( width: 2, color: color(light: Colors.black, dark: Colors.white))), - isError: getMetaData('isError'), - enabled: getMetaData('enabled'), - tileColor: getMetaData('tileColor'), - subtitle: getMetaData('subtitle'), - isThreeLine: getMetaData('isThreeLine'), - dense: getMetaData('dense'), - secondary: getMetaData('secondary'), - selected: getMetaData('selected'), - contentPadding: getMetaData('contentPadding'), - tristate: getMetaData('tristate'), - checkboxShape: getMetaData('checkboxShape'), - selectedTileColor: getMetaData('selectedTileColor'), - onFocusChange: getMetaData('onFocusChange'), - enableFeedback: getMetaData('enableFeedback'), - checkboxSemanticLabel: getMetaData('checkboxSemanticLabel'), + isError: getFieldMeta('isError', false), + enabled: getFieldMeta('enabled', null), + tileColor: getFieldMeta('tileColor', null), + subtitle: getFieldMeta('subtitle', null), + isThreeLine: getFieldMeta('isThreeLine', false), + dense: getFieldMeta('dense', null), + secondary: getFieldMeta('secondary', null), + selected: getFieldMeta('selected', false), + contentPadding: getFieldMeta('contentPadding', null), + tristate: getFieldMeta('tristate', false), + checkboxShape: getFieldMeta('checkboxShape', null), + selectedTileColor: getFieldMeta('selectedTileColor', null), + onFocusChange: getFieldMeta('onFocusChange', null), + enableFeedback: getFieldMeta('enableFeedback', null), + checkboxSemanticLabel: getFieldMeta('checkboxSemanticLabel', null), ); } - - /// Get the metadata from the field - getMetaData(String key) { - dynamic metaData = widget.field.cast.metaData; - if (metaData is! Map) { - return null; - } - if (!metaData.containsKey(key)) { - return null; - } - return widget.field.cast.metaData[key]; - } } diff --git a/lib/widgets/fields/form_chips.dart b/lib/widgets/fields/form_chips.dart index 45d7a6b..8f72d6e 100644 --- a/lib/widgets/fields/form_chips.dart +++ b/lib/widgets/fields/form_chips.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:nylo_support/localization/app_localization.dart'; import '/helpers/extensions.dart'; import '/widgets/fields/field_base_state.dart'; import '/widgets/ny_form.dart'; @@ -86,7 +87,7 @@ class _NyFormChipState extends FieldBaseState { isSelected ? getSelectedSide() : getUnselectedSide(), dark: () => BorderSide(color: Colors.transparent)), shape: getShape(), - label: Text(option, + label: Text(option.tr(), style: whenTheme( light: () => isSelected ? getSelectedTextStyle() diff --git a/lib/widgets/fields/form_date_time_picker.dart b/lib/widgets/fields/form_date_time_picker.dart index fcf7556..804119d 100644 --- a/lib/widgets/fields/form_date_time_picker.dart +++ b/lib/widgets/fields/form_date_time_picker.dart @@ -1,6 +1,7 @@ import 'package:date_field/date_field.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; +import 'package:nylo_support/localization/app_localization.dart'; import '/widgets/fields/field_base_state.dart'; import '/widgets/ny_form.dart'; import 'package:recase/recase.dart'; @@ -102,7 +103,7 @@ class _NyFormDateTimePickerState extends FieldBaseState { border: InputBorder.none, filled: true, suffixIconColor: color(light: Colors.black, dark: Colors.white), - labelText: widget.field.name.titleCase, + labelText: widget.field.name.titleCase.tr(), labelStyle: TextStyle( fontSize: 16, color: color(light: Colors.grey, dark: Colors.white)), diff --git a/lib/widgets/fields/form_picker.dart b/lib/widgets/fields/form_picker.dart index 6595d3e..0e9ebff 100644 --- a/lib/widgets/fields/form_picker.dart +++ b/lib/widgets/fields/form_picker.dart @@ -102,7 +102,7 @@ class _NyFormPickerState extends FieldBaseState { left: 0, top: 5, child: Text( - widget.field.name.titleCase, + widget.field.name.titleCase.tr(), style: TextStyle( fontSize: 10, color: color( @@ -120,7 +120,7 @@ class _NyFormPickerState extends FieldBaseState { children: [ Flexible( child: Text( - "${"Select".tr()} ${widget.field.name}", + "${"Select".tr()} ${widget.field.name.tr()}", textAlign: width < 200 ? TextAlign.left : TextAlign.center, maxLines: 2, @@ -181,7 +181,7 @@ class _NyFormPickerState extends FieldBaseState { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( - widget.field.name, + widget.field.name.tr(), textAlign: TextAlign.center, style: TextStyle( fontWeight: FontWeight.bold, diff --git a/lib/widgets/fields/form_switch_box.dart b/lib/widgets/fields/form_switch_box.dart new file mode 100644 index 0000000..19d48f7 --- /dev/null +++ b/lib/widgets/fields/form_switch_box.dart @@ -0,0 +1,219 @@ +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:nylo_support/localization/app_localization.dart'; +import '/widgets/fields/field_base_state.dart'; +import '/widgets/ny_form.dart'; +import 'package:recase/recase.dart'; + +/// A [NyFormSwitchBox] widget for Form Fields +class NyFormSwitchBox extends StatefulWidget { + /// Creates a [NyFormSwitchBox] widget + NyFormSwitchBox( + {super.key, + required String name, + bool? value, + MouseCursor? mouseCursor, + Color? activeColor, + Color? fillColor, + Color? checkColor, + Color? hoverColor, + Color? overlayColor, + double? splashRadius, + MaterialTapTargetSize? materialTapTargetSize, + VisualDensity? visualDensity, + FocusNode? focusNode, + bool autofocus = false, + ShapeBorder? shape, + BorderSide? side, + bool isError = false, + bool? enabled, + Color? tileColor, + Widget? title, + Widget? subtitle, + bool isThreeLine = false, + bool? dense, + Widget? secondary, + bool selected = false, + ListTileControlAffinity controlAffinity = + ListTileControlAffinity.platform, + EdgeInsetsGeometry? contentPadding, + bool tristate = false, + ShapeBorder? checkboxShape, + Color? selectedTileColor, + ValueChanged? onFocusChange, + bool? enableFeedback, + String? checkboxSemanticLabel, + Color? activeTrackColor, + Color? inactiveThumbColor, + Color? inactiveTrackColor, + ImageProvider? activeThumbImage, + ImageErrorListener? onActiveThumbImageError, + ImageProvider? inactiveThumbImage, + ImageErrorListener? onInactiveThumbImageError, + Color? thumbColor, + Color? trackColor, + Color? trackOutlineColor, + Widget? thumbIcon, + DragStartBehavior dragStartBehavior = DragStartBehavior.start, + this.onChanged}) + : field = Field(name, value: value) + ..cast = FormCast.switchBox( + activeTrackColor: activeTrackColor, + inactiveThumbColor: inactiveThumbColor, + inactiveTrackColor: inactiveTrackColor, + activeThumbImage: activeThumbImage, + onActiveThumbImageError: onActiveThumbImageError, + inactiveThumbImage: inactiveThumbImage, + onInactiveThumbImageError: onInactiveThumbImageError, + thumbColor: thumbColor, + trackColor: trackColor, + trackOutlineColor: trackOutlineColor, + thumbIcon: thumbIcon, + dragStartBehavior: dragStartBehavior, + mouseCursor: mouseCursor, + activeColor: activeColor, + fillColor: fillColor, + checkColor: checkColor, + hoverColor: hoverColor, + overlayColor: overlayColor, + splashRadius: splashRadius, + materialTapTargetSize: materialTapTargetSize, + visualDensity: visualDensity, + focusNode: focusNode, + autofocus: autofocus, + shape: shape, + side: side, + isError: isError, + enabled: enabled, + tileColor: tileColor, + subtitle: subtitle, + isThreeLine: isThreeLine, + dense: dense, + secondary: secondary, + selected: selected, + controlAffinity: controlAffinity, + contentPadding: contentPadding, + tristate: tristate, + checkboxShape: checkboxShape, + selectedTileColor: selectedTileColor, + onFocusChange: onFocusChange, + enableFeedback: enableFeedback, + checkboxSemanticLabel: checkboxSemanticLabel, + ); + + /// Creates a [NyFormSwitchBox] widget from a [Field] + const NyFormSwitchBox.fromField(this.field, this.onChanged, {super.key}); + + /// The field to be rendered + final Field field; + + /// The callback function to be called when the value changes + final Function(dynamic value)? onChanged; + + @override + // ignore: no_logic_in_create_state + createState() => _NyFormSwitchBoxState(field); +} + +class _NyFormSwitchBoxState extends FieldBaseState { + dynamic currentValue; + + _NyFormSwitchBoxState(super.field); + + @override + void initState() { + super.initState(); + dynamic fieldValue = widget.field.value; + + if (fieldValue is String) { + if (fieldValue.toLowerCase() == "true") { + currentValue = true; + } else if (fieldValue.toLowerCase() == "false") { + currentValue = false; + } + } + + if (fieldValue is bool) { + currentValue = fieldValue; + } + + currentValue ??= false; + } + + @override + Widget view(BuildContext context) { + Widget? title = getFieldMeta('title', null); + + title ??= Text( + widget.field.name.titleCase.tr(), + style: TextStyle( + color: color(light: Colors.black, dark: Colors.white), + ), + ); + if (title is Text && (title.data == null || title.data!.isEmpty)) { + title = Text( + widget.field.name.titleCase.tr(), + style: TextStyle( + color: color(light: Colors.black, dark: Colors.white), + ), + ); + } + + Color? overlayColorMetaData = getFieldMeta('overlayColor', null); + WidgetStateProperty? overlayColor; + if (overlayColorMetaData != null) { + overlayColor = WidgetStateProperty.all(overlayColorMetaData); + } + + return SwitchListTile.adaptive( + title: title, + value: currentValue, + onChanged: (value) { + setState(() { + currentValue = value; + if (widget.onChanged != null) { + widget.onChanged!(value); + } + }); + }, + controlAffinity: + getFieldMeta("controlAffinity", ListTileControlAffinity.platform), + activeColor: color( + light: getFieldMeta('activeColor', null) ?? Color(0xFF0766ff), + dark: Colors.black), + hoverColor: getFieldMeta('hoverColor', null), + overlayColor: overlayColor, + splashRadius: getFieldMeta('splashRadius', null), + materialTapTargetSize: getFieldMeta('materialTapTargetSize', null), + visualDensity: getFieldMeta('visualDensity', null), + focusNode: getFieldMeta('focusNode', null), + autofocus: getFieldMeta('autofocus', false), + shape: getFieldMeta('shape', null), + tileColor: getFieldMeta('tileColor', null), + subtitle: getFieldMeta('subtitle', null), + isThreeLine: getFieldMeta('isThreeLine', false), + dense: getFieldMeta('dense', null), + secondary: getFieldMeta('secondary', null), + selected: getFieldMeta('selected', false), + contentPadding: getFieldMeta('contentPadding', null), + selectedTileColor: getFieldMeta('selectedTileColor', null), + onFocusChange: getFieldMeta('onFocusChange', null), + enableFeedback: getFieldMeta('enableFeedback', null), + activeThumbImage: getFieldMeta('activeThumbImage', null), + onActiveThumbImageError: getFieldMeta('onActiveThumbImageError', null), + inactiveThumbImage: getFieldMeta('inactiveThumbImage', null), + onInactiveThumbImageError: + getFieldMeta('onInactiveThumbImageError', null), + thumbColor: getWidgetStatePropertyColor('thumbColor'), + trackColor: getWidgetStatePropertyColor('trackColor'), + trackOutlineColor: getWidgetStatePropertyColor('trackOutlineColor'), + thumbIcon: getWidgetStatePropertyIcon('thumbIcon'), + activeTrackColor: getFieldMeta('activeTrackColor', null), + inactiveThumbColor: getFieldMeta('inactiveThumbColor', null), + inactiveTrackColor: getFieldMeta('inactiveTrackColor', null), + dragStartBehavior: + getFieldMeta('dragStartBehavior', DragStartBehavior.start), + mouseCursor: getFieldMeta('mouseCursor', null), + ); + } +} diff --git a/lib/widgets/form/casts.dart b/lib/widgets/form/casts.dart index d09173a..6456469 100644 --- a/lib/widgets/form/casts.dart +++ b/lib/widgets/form/casts.dart @@ -1,4 +1,5 @@ import 'package:date_field/date_field.dart'; +import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '/widgets/ny_form.dart'; @@ -139,6 +140,97 @@ class FormCast { metaData['checkboxSemanticLabel'] = checkboxSemanticLabel; } + /// Cast to a switchBox + FormCast.switchBox({ + MouseCursor? mouseCursor, + Color? activeColor, + Color? fillColor, + Color? checkColor, + Color? hoverColor, + Color? overlayColor, + double? splashRadius, + MaterialTapTargetSize? materialTapTargetSize, + VisualDensity? visualDensity, + FocusNode? focusNode, + bool autofocus = false, + ShapeBorder? shape, + BorderSide? side, + bool isError = false, + bool? enabled, + Color? tileColor, + Widget? title, + Widget? subtitle, + bool isThreeLine = false, + bool? dense, + Widget? secondary, + bool selected = false, + ListTileControlAffinity controlAffinity = ListTileControlAffinity.platform, + EdgeInsetsGeometry? contentPadding, + bool tristate = false, + ShapeBorder? checkboxShape, + Color? selectedTileColor, + ValueChanged? onFocusChange, + bool? enableFeedback, + String? checkboxSemanticLabel, + Color? activeTrackColor, + Color? inactiveThumbColor, + Color? inactiveTrackColor, + ImageProvider? activeThumbImage, + ImageErrorListener? onActiveThumbImageError, + ImageProvider? inactiveThumbImage, + ImageErrorListener? onInactiveThumbImageError, + Color? thumbColor, + Color? trackColor, + Color? trackOutlineColor, + Widget? thumbIcon, + DragStartBehavior dragStartBehavior = DragStartBehavior.start, + }) { + type = "switchBox"; + metaData = {}; + metaData['mouseCursor'] = mouseCursor; + metaData['activeColor'] = activeColor; + metaData['fillColor'] = fillColor; + metaData['checkColor'] = checkColor; + metaData['hoverColor'] = hoverColor; + metaData['overlayColor'] = overlayColor; + metaData['splashRadius'] = splashRadius; + metaData['materialTapTargetSize'] = materialTapTargetSize; + metaData['visualDensity'] = visualDensity; + metaData['focusNode'] = focusNode; + metaData['autofocus'] = autofocus; + metaData['shape'] = shape; + metaData['side'] = side; + metaData['isError'] = isError; + metaData['enabled'] = enabled; + metaData['tileColor'] = tileColor; + metaData['title'] = title; + metaData['subtitle'] = subtitle; + metaData['isThreeLine'] = isThreeLine; + metaData['dense'] = dense; + metaData['secondary'] = secondary; + metaData['selected'] = selected; + metaData['controlAffinity'] = controlAffinity; + metaData['contentPadding'] = contentPadding; + metaData['tristate'] = tristate; + metaData['checkboxShape'] = checkboxShape; + metaData['selectedTileColor'] = selectedTileColor; + metaData['onFocusChange'] = onFocusChange; + metaData['enableFeedback'] = enableFeedback; + metaData['checkboxSemanticLabel'] = checkboxSemanticLabel; + metaData['activeTrackColor'] = activeTrackColor; + metaData['inactiveThumbColor'] = inactiveThumbColor; + metaData['inactiveTrackColor'] = inactiveTrackColor; + metaData['activeThumbImage'] = activeThumbImage; + metaData['onActiveThumbImageError'] = onActiveThumbImageError; + metaData['inactiveThumbImage'] = inactiveThumbImage; + metaData['onInactiveThumbImageError'] = onInactiveThumbImageError; + metaData['thumbColor'] = thumbColor; + metaData['trackColor'] = trackColor; + metaData['trackOutlineColor'] = trackOutlineColor; + metaData['thumbIcon'] = thumbIcon; + metaData['dragStartBehavior'] = dragStartBehavior; + } + /// Cast to capitalize words FormCast.capitalizeWords({ Widget? prefixIcon, diff --git a/lib/widgets/form/form_item.dart b/lib/widgets/form/form_item.dart index 90ebe28..08a559a 100644 --- a/lib/widgets/form/form_item.dart +++ b/lib/widgets/form/form_item.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_multi_formatter/formatters/phone_input_formatter.dart'; import 'package:mask_text_input_formatter/mask_text_input_formatter.dart'; +import 'package:nylo_support/widgets/fields/form_switch_box.dart'; import 'package:recase/recase.dart'; import '/helpers/currency_input_matcher.dart'; @@ -159,6 +160,36 @@ class NyFormItem extends StatelessWidget { ); } + // check if the field is a checkbox field + if (field.cast.type == "switchBox") { + FormStyleSwitchBox? styles = formStyle?.switchBox(context, field); + if ((styles ?? {}).containsKey('default')) { + FormCast formCast = styles!['default']!(); + formCast.metaData.forEach((key, value) { + if (key == 'title' && field.cast.metaData[key] is Text) { + Text? title = field.cast.metaData[key]; + if (title?.data == null) { + formCast.metaData[key] = Text(field.name); + } else { + formCast.metaData[key] = title; + } + } else { + field.cast.metaData[key] = value; + } + }); + field.cast = formCast; + } else if ((styles ?? {}).containsKey(fieldStyle)) { + FormCast formCast = styles![fieldStyle]!(); + formCast.metaData + .forEach((key, value) => field.cast.metaData[key] = value); + field.cast = formCast; + } + return NyFormSwitchBox.fromField( + field, + onChanged, + ); + } + FormStyleTextField? textFieldStyles = formStyle?.textField(context, field); TextCapitalization textCapitalization = TextCapitalization.none; diff --git a/lib/widgets/ny_form.dart b/lib/widgets/ny_form.dart index 89610b0..a4987b4 100644 --- a/lib/widgets/ny_form.dart +++ b/lib/widgets/ny_form.dart @@ -20,16 +20,22 @@ typedef FormStyleTextField = Map; /// FormStyleCheckbox is a typedef that helps in managing form style checkboxes typedef FormStyleCheckbox = Map; +/// FormStyleSwitchBox is a typedef that helps in managing form style switch boxes +typedef FormStyleSwitchBox = Map; + /// TextAreaSize is an enum that helps in managing textarea sizes enum TextAreaSize { sm, md, lg } /// NyFormStyle is a class that helps in managing form styles class NyFormStyle { - /// TextField styles for the form + /// FormStyleTextField styles for the form FormStyleTextField textField(BuildContext context, Field field) => {}; - /// Checkbox styles for the form + /// FormStyleCheckbox styles for the form FormStyleCheckbox checkbox(BuildContext context, Field field) => {}; + + /// FormStyleSwitchBox styles for the form + FormStyleSwitchBox switchBox(BuildContext context, Field field) => {}; } /// DecoratorTextField is a class that helps in managing form text fields @@ -342,7 +348,7 @@ class Field { this.style, this.metaData = const {}, this.hidden = false, - }) : cast = FormCast.currency(currency) { + }) : cast = FormCast.currency(currency.toLowerCase()) { if (style == null) return; metaData = {}; @@ -713,6 +719,94 @@ class Field { } } + /// Field.switchBox is a constructor that helps in managing switch fields + Field.switchBox( + this.key, { + this.value, + this.validate, + this.autofocus = false, + this.dummyData, + this.header, + this.footer, + this.titleStyle, + this.style, + this.metaData = const {}, + this.hidden = false, + MouseCursor? mouseCursor, + Color? activeColor, + Color? fillColor, + Color? checkColor, + Color? hoverColor, + Color? overlayColor, + double? splashRadius, + MaterialTapTargetSize? materialTapTargetSize, + VisualDensity? visualDensity, + FocusNode? focusNode, + ShapeBorder? shape, + BorderSide? side, + bool isError = false, + bool? enabled, + Color? tileColor, + Widget? title, + Widget? subtitle, + bool isThreeLine = false, + bool? dense, + Widget? secondary, + bool selected = false, + ListTileControlAffinity controlAffinity = ListTileControlAffinity.platform, + EdgeInsetsGeometry? contentPadding, + bool tristate = false, + ShapeBorder? checkboxShape, + Color? selectedTileColor, + ValueChanged? onFocusChange, + bool? enableFeedback, + String? checkboxSemanticLabel, + }) : cast = FormCast.switchBox( + mouseCursor: mouseCursor, + activeColor: activeColor, + fillColor: fillColor, + checkColor: checkColor, + hoverColor: hoverColor, + overlayColor: overlayColor, + splashRadius: splashRadius, + materialTapTargetSize: materialTapTargetSize, + visualDensity: visualDensity, + focusNode: focusNode, + autofocus: autofocus, + shape: shape, + side: side, + isError: isError, + enabled: enabled, + tileColor: tileColor, + title: title, + subtitle: subtitle, + isThreeLine: isThreeLine, + dense: dense, + secondary: secondary, + selected: selected, + controlAffinity: controlAffinity, + contentPadding: contentPadding, + tristate: tristate, + checkboxShape: checkboxShape, + selectedTileColor: selectedTileColor, + onFocusChange: onFocusChange, + enableFeedback: enableFeedback, + checkboxSemanticLabel: checkboxSemanticLabel) { + if (style == null) return; + + metaData = {}; + if (style is String) { + style = style; + return; + } + if (style is Map) { + style as Map; + metaData!["decoration_style"] = + (style as Map).entries.first.value; + style = (style as Map).entries.first.key; + } + } + /// Field.datetime is a constructor that helps in managing datetime fields Field.datetime( this.key, { diff --git a/pubspec.lock b/pubspec.lock index aab47cb..371239f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -325,6 +325,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + http: + dependency: transitive + description: + name: http + sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 + url: "https://pub.dev" + source: hosted + version: "1.2.2" http_parser: dependency: transitive description: @@ -662,10 +670,10 @@ packages: dependency: "direct main" description: name: timezone - sha256: "2236ec079a174ce07434e89fcd3fcda430025eb7692244139a9cf54fdcf1fc7d" + sha256: ffc9d5f4d1193534ef051f9254063fa53d588609418c84299956c3db9383587d url: "https://pub.dev" source: hosted - version: "0.9.4" + version: "0.10.0" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index a685539..8e9a3c5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: nylo_support description: Support library for the Nylo framework. This library supports routing, widgets, localization, cli, storage and more. -version: 6.7.1 +version: 6.8.0 homepage: https://nylo.dev repository: https://github.com/nylo-core/support/tree/6.x issue_tracker: https://github.com/nylo-core/support/issues @@ -43,7 +43,7 @@ dependencies: flutter_multi_formatter: ^2.13.0 flutter_local_notifications: ^18.0.1 path_provider: ^2.1.5 - timezone: ^0.9.4 + timezone: ^0.10.0 flutter_timezone: ^3.0.1 app_badge_plus: ^1.1.6 mask_text_input_formatter: ^2.9.0