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.
car_common_app/lib/widgets/txt_field.dart

297 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:sizer/sizer.dart';
class TxtField extends StatefulWidget {
String? value;
String? hint;
String? lable;
Color postFixDataColor;
IconData? prefixData;
IconData? postfixData;
bool isNeedFilterButton;
bool isNeedClickAll;
bool isButtonEnable;
final String errorValue;
double? elevation;
VoidCallback? onTap;
String? buttonTitle;
int? maxLines;
bool isSidePaddingZero;
bool isNeedBorder;
bool isSearchBar;
bool? isPasswordEnabled;
Function(String)? onChanged;
Function(String)? onSubmitted;
Function()? onPostFixPressed;
TextInputType? keyboardType;
bool isBackgroundEnabled = false;
Widget? postfixWidget;
Widget? preFixWidget;
bool numbersOnly;
bool allowOnlyOneDigit;
bool isNeedLabelOnTop;
FocusNode? focusNode;
TxtField({
super.key,
this.value,
this.lable,
this.hint,
this.prefixData,
this.postfixData,
this.isNeedClickAll = false,
this.postFixDataColor = Colors.white,
this.errorValue = "",
this.isNeedFilterButton = false,
this.elevation,
this.onTap,
this.isButtonEnable = false,
this.buttonTitle,
this.maxLines,
this.isSidePaddingZero = false,
this.isNeedBorder = true,
this.onChanged,
this.onSubmitted,
this.isPasswordEnabled,
this.isSearchBar = false,
this.keyboardType,
this.isBackgroundEnabled = false,
this.postfixWidget,
this.preFixWidget,
this.onPostFixPressed,
this.numbersOnly = false,
this.allowOnlyOneDigit = false,
this.isNeedLabelOnTop = true,
this.focusNode,
});
@override
State<TxtField> createState() => _TxtFieldState();
}
class _TxtFieldState extends State<TxtField> {
TextEditingController controller = TextEditingController();
bool _obscurePassword = true;
@override
void initState() {
super.initState();
controller.text = widget.value ?? "";
}
@override
void didUpdateWidget(TxtField oldWidget) {
super.didUpdateWidget(oldWidget);
// Only update if the value actually changed from parent
if (widget.value != oldWidget.value && widget.value != controller.text) {
controller.text = widget.value ?? "";
controller.selection = TextSelection.fromPosition(
TextPosition(offset: controller.text.length));
}
}
@override
Widget build(BuildContext context) {
return Column(
children: [
if (widget.isNeedLabelOnTop && widget.hint != null) ...[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
(widget.hint ?? "").toText(
color: borderColor,
fontSize: 13,
fontWeight: MyFonts.Medium,
),
],
),
4.height,
],
InkWell(
onTap: widget.isNeedClickAll == false
? null
: () {
widget.onTap!();
},
customBorder: Utils.inkWellCorner(),
child: Row(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
color: widget.isBackgroundEnabled
? MyColors.textFieldColor
: Colors.white,
borderRadius: const BorderRadius.all(Radius.circular(0)),
),
child: TextField(
style: const TextStyle(
color: MyColors.darkTextColor,
fontSize: 15,
fontWeight: MyFonts.Medium),
textInputAction:
widget.isSearchBar ? TextInputAction.search : null,
keyboardType: widget.keyboardType,
autofocus: false,
controller: controller,
focusNode: widget.focusNode,
enabled: widget.isNeedClickAll == true ? false : true,
maxLines:
widget.isPasswordEnabled == true ? 1 : widget.maxLines,
onTap: () {},
obscureText: widget.isPasswordEnabled == true
? _obscurePassword
: false,
onChanged: widget.onChanged,
onSubmitted: widget.onSubmitted,
inputFormatters: widget.allowOnlyOneDigit &&
widget.numbersOnly
? <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp('[0-9]')),
LengthLimitingTextInputFormatter(1)
]
: widget.numbersOnly
? <TextInputFormatter>[
FilteringTextInputFormatter.allow(
RegExp('[0-9]'))
]
: [],
decoration: InputDecoration(
labelText: widget.lable,
alignLabelWithHint: true,
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: widget.isNeedBorder
? MyColors.darkPrimaryColor
: Colors.transparent,
width: widget.isNeedBorder ? 2.0 : 0),
borderRadius: BorderRadius.circular(0.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: widget.isNeedBorder
? MyColors.darkPrimaryColor
: Colors.transparent,
width: widget.isNeedBorder ? 2.0 : 0),
borderRadius: BorderRadius.circular(0.0),
),
disabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: widget.isNeedBorder
? MyColors.darkPrimaryColor
: Colors.transparent,
width: widget.isNeedBorder ? 2.0 : 0),
borderRadius: BorderRadius.circular(0.0),
),
suffixIcon: widget.isPasswordEnabled == true
? IconButton(
icon: Icon(
_obscurePassword
? Icons.visibility_off
: Icons.visibility,
color: Colors.grey,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
)
: widget.postfixData != null
? InkWell(
onTap: widget.onPostFixPressed,
child: Icon(widget.postfixData,
color: widget.postFixDataColor),
)
: widget.postfixWidget,
prefixIcon: widget.prefixData != null
? Icon(widget.prefixData, color: borderColor)
: widget.preFixWidget,
labelStyle: const TextStyle(
color: borderColor,
fontSize: 13,
fontWeight: MyFonts.Medium),
hintStyle: const TextStyle(
color: borderColor,
fontSize: 13,
fontWeight: MyFonts.Medium),
hintText:
widget.isNeedLabelOnTop ? null : widget.hint ?? "",
contentPadding: widget.prefixData == null
? EdgeInsets.only(
left: 12,
right: 12,
top: widget.maxLines != null ? 12 : 0,
bottom: widget.maxLines != null ? 12 : 0,
)
: EdgeInsets.zero,
),
),
),
),
if (widget.isNeedFilterButton) 8.width,
if (widget.isNeedFilterButton)
InkWell(
onTap: widget.isNeedClickAll
? null
: () {
controller.clear();
},
child: SizedBox(
width: 55,
height: 55,
child: Card(
color: accentColor,
child: Icon(
widget.postfixData ?? Icons.filter_alt,
color: widget.postFixDataColor,
),
),
),
),
if (widget.isButtonEnable)
Material(
child: InkWell(
onTap: widget.onTap,
customBorder: Utils.inkWellCorner(),
child: SizedBox(
height: 55,
child: Card(
color: accentColor,
child: Center(
child: Padding(
padding: const EdgeInsets.only(left: 12, right: 12),
child: (widget.buttonTitle ?? "Search").toText(
color: Colors.white,
fontSize: 16,
letterSpacing: -0.64,
),
),
),
),
),
),
),
],
),
),
if (widget.errorValue != "")
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
widget.errorValue.toText(fontSize: 12, color: Colors.red),
],
).paddingOnly(right: 10),
],
);
}
}