Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.6 kB
2
Indexable
Never
import 'package:flutter/material.dart';
import 'package:sanad_flutter/core/config/app_localization.dart';
import 'package:sanad_flutter/core/config/sanad_colors.dart';
import 'package:sanad_flutter/core/config/validation.dart';
import 'package:sanad_flutter/core/navigation/custom_navigation.dart';
import 'package:sanad_flutter/core/widgets/custom_text.dart';
import 'package:sanad_flutter/core/widgets/custom_text_field.dart';
import 'package:flutter/cupertino.dart' as cupertino;
import 'package:intl/intl.dart' as intl;

import 'custom_date_picker.dart';
import 'custom_text_button.dart';

class CustomDateTextField extends StatelessWidget {
  CustomDateTextField({
    super.key,
    this.fillColor,
    this.margin,
    this.customTextButtonPadding,
    required this.controller,
    this.maximumYear,
    this.format,
    required this.label,
    this.enabled = true,
    this.validator,
    this.onComplete,
    this.initalDate,
    this.maximumDate,
  });
  final TextEditingController controller;
  final String label;
  final Color? fillColor;
  final EdgeInsetsGeometry? customTextButtonPadding;
  final EdgeInsetsGeometry? margin;
  final bool enabled;
  final int? maximumYear;
  void Function()? onComplete;
  final String? format;
  DateTime? initalDate;
  DateTime? maximumDate;
  String? Function(String?)? validator;
  DateTime _selectDate = DateTime.now();
  onTabField(TextEditingController controller) async {
    FocusScope.of(currentContext!).requestFocus(FocusNode());
    if (initalDate == null) {
      if (controller.text == "") {
        if (initalDate != null) {
          controller.text =
              intl.DateFormat(format ?? 'd/M/yyyy').format(initalDate!);
        } else {
          controller.text =
              intl.DateFormat(format ?? 'd/M/yyyy').format(DateTime.now());
        }
      }
    } else {
      controller.text =
          intl.DateFormat(format ?? 'd/M/yyyy').format(initalDate!);
    }
    cupertino
        .showCupertinoModalPopup(
      context: currentContext!,
      builder: (context) => Container(
        color: gray,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(
              height: 250,
              child: CustomCupertinoDatePicker(
                backgroundColor: gray,
                mode: CupertinoDatePickerMode.date,
                maximumYear: maximumYear ?? DateTime.now().year + 20,
                minimumYear: 1900,
                maximumDate: maximumDate,
                minimumDate: initalDate,
                initialDateTime: initalDate ?? _selectDate,
                dateOrder: cupertino.DatePickerDateOrder.dmy,
                onDateTimeChanged: (DateTime newDateTime) {
                  if (initalDate == null) {
                    _selectDate = newDateTime;
                    controller.text = intl.DateFormat(format ?? 'd/M/yyyy')
                        .format(newDateTime);
                  } else {
                    initalDate = newDateTime;
                    controller.text = intl.DateFormat(format ?? 'd/M/yyyy')
                        .format(initalDate!);
                  }
                },
              ),
            ),
            CustomTextButton(
              margin: const EdgeInsets.only(bottom: 8),
              padding: customTextButtonPadding,
              child: CustomText(
                AppLocalization.of(context).getTranslatedValues("select_date"),
                style: Theme.of(context)
                    .textTheme
                    .bodyText2!
                    .copyWith(color: Colors.black),
              ),
              onPressed: () {
                FocusScope.of(context).unfocus();

                Navigator.of(context).pop();
              },
            ),
          ],
        ),
      ),
    )
        .whenComplete(() {
      if (onComplete != null) onComplete!();
    });
  }

  @override
  Widget build(BuildContext context) {
    return CustomTextField(
      enabled: enabled,
      fillColor:
          fillColor ?? selectColor(context, Colors.transparent, textFieldDark),
      controller: controller,
      margin: margin,
      keyboardType: TextInputType.none,
      label: CustomText(AppLocalization.of(context).getTranslatedValues(label)),
      onTap: () {
        onTabField(controller);
      },
      validator: validator ??
          (value) {
            return Validation.isValid(context, value!);
          },
    );
  }
}