build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
context
using BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
String fieldNameSnakeCase = field.name.snakeCase;
// ignore: prefer_function_declarations_over_variables
Function(dynamic value)? onChanged = (dynamic value) {
updated?.add((fieldNameSnakeCase, value));
this.onChanged!(value);
};
if (!([
"datetime",
"text",
"url",
"email",
"checkbox",
"mask",
"password",
"password:viewable",
"textarea",
"textarea:sm",
"textarea:md",
"textarea:lg",
"number",
"number:decimal",
"phone_number",
...[
for (String currency in CurrencyInputMatcher.currencies)
"currency:$currency"
],
"capitalize-words",
"capitalize-sentences",
"uppercase",
"lowercase",
].contains(field.cast.type))) {
// check if field exists on Nylo instance
Map<String, dynamic> nyloFormTypes = Nylo.instance.getFormCasts();
if (nyloFormTypes.containsKey(field.cast.type)) {
return nyloFormTypes[field.cast.type]!(field, onChanged);
}
}
// check if the field is a datetime field
if (field.cast.type == "datetime") {
return NyFormDateTimePicker.fromField(
field,
onChanged,
);
}
// check if the field is a picker field
if (field.cast.type == "picker") {
return NyFormPicker.fromField(
field,
onChanged,
);
}
// check if the field is a chip field
if (field.cast.type == "chip") {
return match(
fieldStyle,
() => {
"compact": NyFormChip.compact(
field,
onChanged,
),
},
defaultValue: NyFormChip.fromField(
field,
onChanged,
));
}
// check if the field is a checkbox field
if (field.cast.type == "checkbox") {
FormStyleCheckbox? checkboxStyles = formStyle?.checkbox(context, field);
if ((checkboxStyles ?? {}).containsKey('default')) {
FormCast formCast = checkboxStyles!['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 ((checkboxStyles ?? {}).containsKey(fieldStyle)) {
FormCast formCast = checkboxStyles![fieldStyle]!();
formCast.metaData
.forEach((key, value) => field.cast.metaData[key] = value);
field.cast = formCast;
}
return NyFormCheckbox.fromField(
field,
onChanged,
);
}
// 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;
switch (field.cast.type) {
case "capitalize-words":
textCapitalization = TextCapitalization.words;
break;
case "capitalize-sentences":
textCapitalization = TextCapitalization.sentences;
break;
case "uppercase":
textCapitalization = TextCapitalization.characters;
break;
case "lowercase":
textCapitalization = TextCapitalization.none;
break;
default:
textCapitalization = TextCapitalization.none;
break;
}
NyTextField nyTextField;
switch (fieldStyle) {
case "compact":
nyTextField = NyTextField.compact(
controller: textEditingController,
hintText: field.name,
textCapitalization: textCapitalization,
onChanged:
((field.cast.type ?? "").contains("currency")) ? null : onChanged,
autoFocus: field.autofocus,
validationRules: validationRules,
customValidationRule: customValidationRule,
validationErrorMessage: validationMessage,
validateOnFocusChange: validateOnFocusChange,
dummyData: dummyData,
header: field.header,
footer: field.footer,
clearable: field.cast.getMetaData("clearable") ?? false,
clearIcon:
field.cast.getMetaData("clearIcon") ?? const Icon(Icons.close),
prefixIcon: field.cast.getMetaData("prefixIcon"),
);
break;
default:
{
nyTextField = NyTextField(
controller: textEditingController,
hintText: field.name,
textCapitalization: textCapitalization,
onChanged: ((field.cast.type ?? "").contains("currency"))
? null
: onChanged,
autoFocus: field.autofocus,
validationRules: validationRules,
validationErrorMessage: validationMessage,
customValidationRule: customValidationRule,
validateOnFocusChange: validateOnFocusChange,
dummyData: dummyData,
header: field.header,
footer: field.footer,
clearable: field.cast.getMetaData("clearable") ?? false,
clearIcon:
field.cast.getMetaData("clearIcon") ?? const Icon(Icons.close),
prefixIcon: field.cast.getMetaData("prefixIcon"),
);
if ((textFieldStyles ?? {}).containsKey('default')) {
nyTextField = textFieldStyles!['default']!(nyTextField);
}
}
}
if (fieldStyle != null && (textFieldStyles ?? {}).containsKey(fieldStyle)) {
nyTextField = textFieldStyles![fieldStyle]!(nyTextField);
}
// check if the field has a style
if (style != null &&
(field.metaData ?? {}).containsKey('decoration_style') == false) {
return style!(nyTextField);
}
if ((field.metaData ?? {}).containsKey('decoration_style')) {
nyTextField = field.metaData!['decoration_style']!(nyTextField);
}
// check if the field is a mask field
if (field.cast.type == "mask") {
nyTextField = nyTextField.copyWith(
maskedReturnValue: field.cast.getMetaData("maskedReturnValue"),
inputFormatters: [
MaskTextInputFormatter(
mask: field.cast.getMetaData("mask"),
filter: {"#": RegExp(field.cast.getMetaData("match"))},
type: MaskAutoCompletionType.lazy,
)
]);
}
// check if the field is an email field
if (field.cast.type == "email") {
nyTextField = nyTextField.copyWith(
type: "email-address", keyboardType: TextInputType.emailAddress);
}
// check if the field is a password field
if ((field.cast.type ?? "").contains("password")) {
nyTextField = nyTextField.copyWith(
type: "password",
obscureText: true,
keyboardType: (field.cast.type ?? "").contains("viewable")
? TextInputType.visiblePassword
: TextInputType.text,
passwordViewable: (field.cast.type ?? "").contains("viewable"),
);
}
// check if the field is a textarea field
if ((field.cast.type ?? "").contains("textarea")) {
nyTextField = nyTextField.copyWith(
minLines: match(
field.cast.type,
() => {
"textarea": 1,
"textarea:sm": 2,
"textarea:md": 3,
"textarea:lg": 5
},
defaultValue: 1),
maxLines: match(
field.cast.type,
() => {
"textarea": 3,
"textarea:sm": 4,
"textarea:md": 5,
"textarea:lg": 7
},
defaultValue: 3),
keyboardType: TextInputType.multiline,
);
}
// Update the nyTextField onChanged function
nyTextField = nyTextField.copyWith(
onChanged: (String? value) {
if (field.cast.type == "capitalize-words") {
value = value?.split(" ").map((String word) {
if (word.isEmpty) return word;
return word[0].toUpperCase() + word.substring(1);
}).join(" ");
}
if (field.cast.type == "capitalize-sentences") {
value = value?.split(".").map((String word) {
if (word.isEmpty) return word;
return word[0].toUpperCase() + word.substring(1);
}).join(".");
}
if (field.cast.type == "uppercase") {
value = value?.toUpperCase();
}
if (field.cast.type == "lowercase") {
value = value?.toLowerCase();
}
if (!((field.cast.type ?? "").contains("currency"))) {
onChanged(value);
}
},
);
// check if the field is a currency field
if (field.cast.type?.contains("currency") ?? false) {
CurrencyMeta currencyMeta = CurrencyInputMatcher.getCurrencyMeta(
field.cast.type!,
value: field.value ?? "0",
onChanged: onChanged);
textEditingController.text = currencyMeta.initialValue;
nyTextField = nyTextField.copyWith(
onChanged: null,
type: fieldStyle,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
currencyMeta.formatter,
],
);
}
// check if the field is a phone number field
if (field.cast.type == "phone_number") {
nyTextField = nyTextField.copyWith(
keyboardType: TextInputType.phone,
inputFormatters: [PhoneInputFormatter()],
onChanged: onChanged,
);
}
// check if the field is a number field
if (field.cast.type == "url") {
nyTextField = nyTextField.copyWith(
type: fieldStyle, keyboardType: TextInputType.url);
}
// check if the field is a number field
if (field.cast.type == "number") {
nyTextField = nyTextField.copyWith(
type: fieldStyle, keyboardType: TextInputType.number);
}
// check if the field is a number field
if (field.cast.type == "number:decimal") {
nyTextField = nyTextField.copyWith(
type: fieldStyle,
keyboardType: const TextInputType.numberWithOptions(decimal: true));
}
return nyTextField;
}