birzha_mobile/lib/components/TextInputCustom.dart

128 lines
4.7 KiB
Dart
Raw Permalink Normal View History

2023-02-27 07:23:37 +00:00
import 'package:birzha/core/adaptix/adaptix.dart';
import 'package:flutter/material.dart';
import 'package:birzha/services/textMetaData.dart';
class TextInputCustom extends StatefulWidget {
final double? fontSize;
final EdgeInsets? padding;
final EdgeInsets? margin;
final TextEditingController controller;
final TextInputMetaData fieldStandard;
final bool? showPassword;
final void Function(String?)? onChanged;
final Widget? suffix;
final Widget? prefix;
TextInputCustom({
Key? key,
this.showPassword,
this.fontSize,
this.padding,
this.suffix,
this.prefix,
this.margin,
required this.fieldStandard,
this.onChanged,
required this.controller,
}) : super(key: key);
@override
_TextInputCustomState createState() => _TextInputCustomState();
}
class _TextInputCustomState extends State<TextInputCustom> {
late FocusNode _focusNode;
bool hasFocus = false;
@override
void initState() {
super.initState();
_focusNode = FocusNode()
..addListener(() {
setState(() {
hasFocus = _focusNode.hasFocus;
});
});
}
Widget? suffix() {
return widget.suffix ?? (widget.fieldStandard.pickerMode != null ? Icon(Icons.arrow_drop_down) : null);
}
@override
Widget build(BuildContext context) {
final double textSize = 14.adaptedPx();
final border = OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey.shade300,
width: 0.9.adaptedPx(),
),
);
return Container(
margin: widget.margin ?? EdgeInsets.symmetric(vertical: 12.adaptedPx()),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GestureDetector(
onTap: widget.fieldStandard.pickerMode == null
? null
: () async {
var extracted = await widget.fieldStandard.pickerMode!(context);
if (extracted != null) widget.controller.text = extracted.toString();
},
child: Row(
children: [
Expanded(
child: TextFormField(
readOnly: widget.fieldStandard.readOnly,
cursorColor: Theme.of(context).accentColor,
keyboardType: widget.fieldStandard.type,
focusNode: _focusNode,
controller: widget.controller,
autofocus: widget.fieldStandard.autoFocus,
onChanged: widget.onChanged,
cursorHeight: 15.2.adaptedPx(),
style: TextStyle(
fontSize: widget.fontSize ?? textSize * 0.9, fontWeight: FontWeight.w500, color: Theme.of(context).textTheme.bodyText2?.color),
validator: (i) => widget.fieldStandard.validation.validate(i ?? ""),
inputFormatters: widget.fieldStandard.formatters,
obscureText: widget.fieldStandard.password && widget.showPassword == false,
decoration: InputDecoration(
isDense: true,
contentPadding: widget.padding ?? EdgeInsets.symmetric(horizontal: 15.adaptedPx(), vertical: 14.adaptedPx()),
errorMaxLines: 2,
suffixIcon: suffix(),
prefixIcon: widget.prefix,
disabledBorder: border,
enabled: widget.fieldStandard.pickerMode == null,
border: border,
enabledBorder: border,
filled: widget.fieldStandard.filled,
fillColor: widget.fieldStandard.fillColor,
focusedBorder: border.copyWith(borderSide: BorderSide(color: Theme.of(context).accentColor)),
focusedErrorBorder: border.copyWith(borderSide: BorderSide(color: Theme.of(context).accentColor)),
hintText: widget.fieldStandard.hint,
labelText: widget.fieldStandard.label,
errorStyle: TextStyle(color: Theme.of(context).errorColor),
errorBorder: border.copyWith(borderSide: BorderSide(color: Colors.redAccent)),
focusColor: Colors.red,
hintStyle: TextStyle(
color: Colors.grey.shade500,
),
labelStyle: TextStyle(
color: hasFocus ? Theme.of(context).accentColor : Colors.grey.shade400,
),
),
),
),
],
),
),
// widget.showBottomInfo ? widget.fieldStandard.bottomInfo ?? SizedBox.shrink() : SizedBox.shrink()
],
),
);
}
}