postalCodeFormEntry method

Widget postalCodeFormEntry({
  1. String title = 'Postal code',
  2. String subTitle = 'the postal code',
  3. dynamic onSaved(
    1. String?
    )?,
  4. String? defaultValue,
})

Implementation

Widget postalCodeFormEntry({
  String title = 'Postal code',
  String subTitle = 'the postal code',
  Function(String?)? onSaved,
  String? defaultValue,
}) {
  return formEntry(
    title: title,
    subTitle: subTitle,
    inputWidget: TextFormField(
      style: Theme.of(context).textTheme.bodyLarge,
      keyboardType: TextInputType.number,
      inputFormatters: [
        FilteringTextInputFormatter.digitsOnly,
        LengthLimitingTextInputFormatter(10), // Limit to 10 digits
      ],
      onChanged: (_) {
        widget.formKey.currentState!.save();
        if (widget.onModified != null) {
          widget.onModified!();
        }
      },
      enabled: isEdit,
      controller: TextEditingController(text: defaultValue),
      decoration: elegantInputDecoration(
        hintText: 'Postal Code (numbers only)',
        prefix: const Icon(Icons.post_add),
      ),
      validator: (value) => value!.isEmpty
          ? 'Postal code is required'
          : (value.length < 5 || value.length > 10
                ? 'Postal code must be 5-10 digits'
                : null),
      onSaved: onSaved,
    ),
  );
}