You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
uco-mobile-poc/lib/app/custom_widgets/custom_date_time_picker.dart

299 lines
10 KiB
Dart

import 'dart:ui';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:get/get.dart';
import '../core/constants/translation_keys.dart';
import '../res/app_colors.dart';
import '../res/app_theme.dart';
import 'custom_text.dart';
class CustomDateTimePicker extends GetView {
final FormFieldValidator<String>? validator;
GlobalKey<FormFieldState>? refKey;
String labelText = "";
String hint = "";
RxString errorTextMessage = "".obs;
String errorText = "";
bool isRequired = true;
final bool enabled;
final AutovalidateMode autovalidateMode;
final String? restorationId;
InputDecoration? decoration;
TextEditingController? controller;
TextInputType keyboardType;
final TextInputAction? textInputAction;
final TextCapitalization textCapitalization;
final TextStyle? style;
final StrutStyle? strutStyle;
TextAlign textAlign;
final TextAlignVertical? textAlignVertical;
final TextDirection? textDirection;
final bool autofocus;
final String obscuringCharacter;
bool isPassword = false;
bool autocorrect = false;
final SmartDashesType? smartDashesType;
final SmartQuotesType? smartQuotesType;
final bool enableSuggestions;
int maxLines;
int minLines;
bool expands;
TextStyle? labelStyle;
final int? maxLength;
final MaxLengthEnforcement? maxLengthEnforcement;
final VoidCallback? onEditingComplete;
final ValueChanged<String?>? onSubmitted;
final List<TextInputFormatter>? inputFormatters;
bool showCursor = true;
double cursorWidth = 5.0;
double cursorHeight = 15.0;
Radius cursorRadius = const Radius.circular(10.0);
Color cursorColor = AppThemeData.inputTextColor;
final BoxHeightStyle selectionHeightStyle;
final BoxWidthStyle selectionWidthStyle;
final Brightness? keyboardAppearance;
final EdgeInsets scrollPadding;
final bool enableInteractiveSelection;
final DragStartBehavior dragStartBehavior;
final GestureTapCallback? onTap;
final TapRegionCallback? onTapOutside;
final MouseCursor? mouseCursor;
final InputCounterWidgetBuilder? buildCounter;
final ScrollPhysics? scrollPhysics;
final ScrollController? scrollController;
final Iterable<String>? autofillHints;
final TextMagnifierConfiguration? magnifierConfiguration;
final bool readOnly;
final ContentInsertionConfiguration? contentInsertionConfiguration;
SpellCheckConfiguration? spellCheckConfiguration;
final Rx<Color> fillColor = Rx<Color>(Colors.transparent);
final Rx<Color> inputErrorColor = Rx<Color>(Colors.black);
final Rx<Color> inputErrorBorderColor = Rx<Color>(Colors.black);
RxBool isError = false.obs;
CustomDateTimePicker({
this.refKey,
this.controller,
this.labelText = "",
this.hint = "",
this.errorText = "",
this.labelStyle,
this.isRequired = true,
this.validator,
this.decoration,
this.enabled = true,
this.autovalidateMode = AutovalidateMode.disabled,
this.restorationId,
this.readOnly = false,
this.isPassword = false,
this.textCapitalization = TextCapitalization.none,
this.scrollPadding = const EdgeInsets.all(20.0),
this.enableInteractiveSelection = true,
this.maxLengthEnforcement,
this.textAlign = TextAlign.start,
this.autofocus = false,
this.autocorrect = false,
this.cursorWidth = 5.0,
this.cursorHeight = 20.0,
this.cursorRadius = const Radius.circular(10.0),
this.keyboardType = TextInputType.text,
this.style,
this.textInputAction,
this.strutStyle,
this.textDirection,
this.maxLength,
this.onEditingComplete,
this.onSubmitted,
this.inputFormatters,
this.cursorColor = AppThemeData.inputTextColor,
this.keyboardAppearance,
this.buildCounter,
this.expands = false,
this.minLines = 1,
this.maxLines = 4,
this.showCursor = true,
this.onTap,
this.onTapOutside,
this.enableSuggestions = true,
this.textAlignVertical,
this.dragStartBehavior = DragStartBehavior.start,
this.scrollController,
this.scrollPhysics,
this.selectionWidthStyle = BoxWidthStyle.tight,
this.smartDashesType,
this.smartQuotesType,
this.selectionHeightStyle = BoxHeightStyle.tight,
this.autofillHints,
this.obscuringCharacter = '',
this.mouseCursor,
this.magnifierConfiguration,
this.contentInsertionConfiguration,
this.spellCheckConfiguration,
}) : errorTextMessage = "".obs {
errorText = errorTextMessage.value;
}
void setError(String errorMessage) {
inputErrorBorderColor.value = AppThemeData.inputErrorBorderColor;
inputErrorColor.value = AppThemeData.inputErrorColor;
fillColor.value = AppThemeData.inputErrorColor;
errorTextMessage.value = errorMessage;
isError.value = true;
}
void setTest(String errorMessage) {
this.controller!.text = errorMessage;
// inputErrorBorderColor.value = AppThemeData.inputErrorBorderColor;
// inputErrorColor.value = AppThemeData.inputErrorColor;
// fillColor.value = AppThemeData.inputErrorColor;
// errorTextMessage.value = errorMessage;
// isError.value = true;
}
void markFieldAsValid() {
inputErrorBorderColor.value = AppThemeData.inputBackgroundColor;
inputErrorColor.value = AppThemeData.inputErrorColor;
errorTextMessage.value = "";
fillColor.value = Colors.transparent;
isError.value = false;
}
void markFieldAsInvalid() {
inputErrorBorderColor.value = AppThemeData.inputErrorBorderColor;
inputErrorColor.value = AppThemeData.inputErrorColor;
fillColor.value = AppThemeData.inputErrorColor;
errorTextMessage.value =
TranslationKeys.makeTranslation(TranslationKeys.textErrorRequired);
isError.value = true;
}
bool validate(String value) {
if (isRequired) {
if (value.isEmpty) {
markFieldAsInvalid();
return false;
}
markFieldAsValid();
return true;
}
markFieldAsValid();
return true;
}
@override
Widget build(BuildContext context) {
return Obx(() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Visibility(
visible: labelText.isNotEmpty,
child: Column(
children: [
CustomText(
labelText,
style: labelStyle ?? AppThemeData.labelStyle,
),
const SizedBox(height: 8),
],
),
),
FormBuilderDateTimePicker(
key: refKey,
name: "",
initialEntryMode: DatePickerEntryMode.calendar,
inputType: InputType.date,
initialDatePickerMode: DatePickerMode.day,
onChanged: (value) {
if (value.toString().isNotEmpty) {
markFieldAsValid();
}
},
// format: DateFormat('dd-yyyy-MM'),
currentDate: DateTime.now(),
firstDate: DateTime(1970),
lastDate: DateTime(2030),
decoration: InputDecoration(
suffixIcon: Container(
width: 30,
height: 30,
margin: const EdgeInsets.all(5),
child: const Icon(size: 18, Icons.calendar_month_rounded),
),
filled: true,
fillColor: AppColors.colorGrey50,
isDense: true,
contentPadding:
const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
labelText: null,
// labelStyle: AppThemeData.labelStyle,
errorStyle: AppThemeData.errorStyle
.copyWith(color: inputErrorColor.value),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: AppThemeData.inputEnabledBorderColor),
borderRadius: BorderRadius.circular(50)),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: AppThemeData.inputFocusedBorderColor),
borderRadius: BorderRadius.circular(50)),
border: OutlineInputBorder(
borderSide:
const BorderSide(color: AppThemeData.inputBorderColor),
borderRadius: BorderRadius.circular(50),
),
errorBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: AppThemeData.inputErrorBorderColor),
borderRadius: BorderRadius.circular(50)),
errorText: errorTextMessage.value.isEmpty
? null
: errorTextMessage.value,
hintText: hint,
hintStyle: AppThemeData.hintStyle,
),
enabled: enabled,
autovalidateMode: autovalidateMode,
restorationId: restorationId,
textCapitalization: textCapitalization,
scrollPadding: scrollPadding,
enableInteractiveSelection: enableInteractiveSelection,
textAlign: textAlign,
autofocus: autofocus,
autocorrect: autocorrect,
keyboardType: keyboardType,
style: AppThemeData.inputStyle.copyWith(height: 1),
controller: controller,
textInputAction: textInputAction,
strutStyle: strutStyle,
textDirection: textDirection,
maxLength: maxLength,
onEditingComplete: onEditingComplete,
inputFormatters: inputFormatters,
cursorColor: cursorColor,
keyboardAppearance: keyboardAppearance,
buildCounter: buildCounter,
expands: expands,
maxLines: isPassword ? maxLines = 1 : maxLines,
obscureText: isPassword,
minLines: isPassword ? minLines = 1 : minLines,
showCursor: showCursor,
mouseCursor: mouseCursor,
),
],
);
});
}
String getText() {
return this.controller!.text;
}
}