import 'package:comwell_key_app/themes/comwell_colors.dart';
import 'package:comwell_key_app/themes/light_theme.dart' hide colorDivider, colorTertiary;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ComwellTextField extends StatefulWidget {
final String fieldName;
final String initialValue;
final bool readOnly;
final TextInputType? textInputType;
final TextEditingController controller;
final String? errorMessage;
final void Function(String)? onChanged;
final List<TextInputFormatter>? inputFormatters;
const ComwellTextField({
super.key,
required this.fieldName,
required this.initialValue,
required this.readOnly,
required this.controller,
this.errorMessage,
this.textInputType,
this.onChanged,
this.inputFormatters,
});
@override
ComwellTextFieldState createState() => ComwellTextFieldState();
}
class ComwellTextFieldState extends State<ComwellTextField> {
late final FocusNode _focusNode;
bool _isFocused = false;
@override
void initState() {
super.initState();
_focusNode = FocusNode();
_focusNode.addListener(() {
setState(() {
_isFocused = _focusNode.hasFocus;
});
});
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final errorMessage = _isFocused ? null : widget.errorMessage;
final labelTextColor =
errorMessage == null ? colorTertiary : const Color(0xFFEB0026);
final textStyle = widget.readOnly
? theme.textTheme.headlineSmall?.copyWith(color: colorDivider)
: _isFocused
? theme.textTheme.bodySmall
?.copyWith(color: colorHeadlineText)
: theme.textTheme.headlineSmall?.copyWith(color: labelTextColor);
return Column(
children: [
TextField(
readOnly: widget.readOnly,
controller: widget.controller,
focusNode: _focusNode,
keyboardType: widget.textInputType,
onChanged: widget.onChanged,
inputFormatters: widget.inputFormatters,
decoration: InputDecoration(
errorText: errorMessage,
errorStyle: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: colorError),
label: Text(widget.fieldName, style: textStyle),
errorBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.red),
borderRadius: BorderRadius.circular(8)),
focusedErrorBorder: OutlineInputBorder(
borderSide: const BorderSide(color: colorDivider),
borderRadius: BorderRadius.circular(8)),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(color: colorDivider),
borderRadius: BorderRadius.circular(8)),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: colorDivider),
borderRadius: BorderRadius.circular(8)),
contentPadding:
const EdgeInsets.symmetric(vertical: 20, horizontal: 12),
),
style: Theme.of(context).textTheme.headlineSmall,
)
],
);
}
}