123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- import 'dart:async';
- import 'dart:core';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:cs_resources/constants/color_constants.dart';
- import 'ext/ex_widget.dart';
- import 'utils/dark_theme_util.dart';
- class MyTextField extends StatelessWidget {
- String formKey;
- String value;
- bool? enabled;
- TextInputType inputType;
- FocusNode? focusNode;
- String? labelText;
- TextStyle? labelStyle;
- String? errorText;
- double cursorWidth;
- Color? cursorColor = ColorConstants.appBlue;
- String? hintText;
- TextStyle? hintStyle;
- TextStyle? style;
- final String? subHintText;
- final TextStyle? subHintStyle;
- bool? autofocus;
- int? maxLines = 1;
- InputBorder? border;
- BoxBorder? boxBorder;
- bool? showLeftIcon;
- Widget? leftWidget;
- bool? showRightIcon;
- Widget? rightWidget;
- bool? showDivider;
- Color? dividerColor;
- bool obscureText;
- double height;
- Color? fillBackgroundColor;
- double? fillCornerRadius;
- EdgeInsetsGeometry padding;
- EdgeInsetsGeometry margin;
- InputDecoration? decoration;
- TextEditingController? controller;
- TextInputAction textInputAction = TextInputAction.done;
- List<TextInputFormatter>? inputFormatters;
- Function? onChanged;
- Function? onSubmit;
- final ClickType changeActionType;
- final int changeActionMilliseconds;
- final ClickType submitActionType;
- final int submitActionMilliseconds;
- MyTextField(
- this.formKey,
- this.value, {
- Key? key,
- this.enabled = true,
- this.inputType = TextInputType.text,
- this.focusNode,
- this.labelText,
- this.labelStyle,
- this.errorText,
- this.cursorWidth = 2.0,
- this.cursorColor,
- this.hintText,
- this.hintStyle,
- this.subHintText,
- this.subHintStyle,
- this.style,
- this.autofocus = false,
- this.maxLines = 1,
- this.border = InputBorder.none,
- this.boxBorder,
- this.showLeftIcon = false,
- this.leftWidget,
- this.showRightIcon = false,
- this.rightWidget,
- this.showDivider = true,
- this.dividerColor = const Color.fromARGB(255, 212, 212, 212),
- this.obscureText = false,
- this.height = 50.0,
- this.fillBackgroundColor,
- this.fillCornerRadius = 5.0,
- this.padding = EdgeInsets.zero,
- this.margin = EdgeInsets.zero,
- this.decoration,
- this.controller,
- this.inputFormatters,
- this.textInputAction = TextInputAction.done,
- this.onChanged,
- this.onSubmit,
- this.changeActionType = ClickType.none,
- this.changeActionMilliseconds = 500,
- this.submitActionType = ClickType.none,
- this.submitActionMilliseconds = 500,
- }) : super(key: key);
- @override
- Widget build(BuildContext context) {
-
- changeAction(value) {
- onChanged?.call(formKey, value);
- }
-
- submitAction(value) {
- onSubmit?.call(formKey, value);
- }
- return Container(
- margin: margin,
- decoration: BoxDecoration(
- color: fillBackgroundColor ?? Colors.transparent,
- borderRadius: BorderRadius.all(Radius.circular(fillCornerRadius ?? 0)),
- border: boxBorder,
- ),
- padding: padding,
- child: ConstrainedBox(
- constraints: BoxConstraints(minHeight: height),
- child: Column(
- mainAxisAlignment: maxLines == null ? MainAxisAlignment.start : MainAxisAlignment.center,
- children: [
- TextField(
- enabled: enabled,
- style: style,
- maxLines: maxLines,
- keyboardType: inputType,
- focusNode: focusNode,
- obscureText: obscureText,
- cursorWidth: cursorWidth,
- cursorColor: DarkThemeUtil.multiColors(context, cursorColor, darkColor: Colors.white),
- autofocus: autofocus!,
- controller: controller,
- inputFormatters: inputFormatters,
- decoration: decoration ??
- InputDecoration(
- hintText: hintText,
- hintStyle: hintStyle,
- icon: showLeftIcon == true ? leftWidget : null,
- border: border,
- suffixIcon: showRightIcon == true ? rightWidget : null,
- labelText: labelText,
- errorText: errorText,
- errorStyle: const TextStyle(color: Colors.redAccent, fontSize: 11.5, height: 0.5),
- errorBorder: const OutlineInputBorder(
- borderSide: BorderSide(color: Colors.redAccent),
- ),
- ),
- onChanged: changeActionType == ClickType.debounce
- ? debounce(changeAction, changeActionMilliseconds)
- : changeActionType == ClickType.throttle
- ? throttle(changeAction, changeActionMilliseconds)
- : changeAction,
- onSubmitted: submitActionType == ClickType.debounce
- ? debounce(submitAction, submitActionMilliseconds)
- : submitActionType == ClickType.throttle
- ? throttle(submitAction, submitActionMilliseconds)
- : submitAction,
- textInputAction: textInputAction,
- ),
- showDivider == true
- ? Divider(
- height: 0.3,
- color: dividerColor!,
- ).marginOnly(top: errorText == null ? 0 : 10)
- : const SizedBox.shrink(),
- ],
- ),
- ),
- );
- }
-
- void Function(String value) debounce(void Function(String value) callback, [int milliseconds = 500]) {
- Timer? _debounceTimer;
- return (value) {
- if (_debounceTimer?.isActive ?? false) _debounceTimer?.cancel();
- _debounceTimer = Timer(Duration(milliseconds: milliseconds), () {
- callback(value);
- });
- };
- }
-
- void Function(String value) throttle(void Function(String value) callback, [int milliseconds = 500]) {
- bool _isAllowed = true;
- Timer? _throttleTimer;
- return (value) {
- if (!_isAllowed) return;
- _isAllowed = false;
- callback(value);
- _throttleTimer?.cancel();
- _throttleTimer = Timer(Duration(milliseconds: milliseconds), () {
- _isAllowed = true;
- });
- };
- }
- }
|