107 lines
2.9 KiB
Dart
107 lines
2.9 KiB
Dart
import 'package:didvan/config/design_config.dart';
|
|
import 'package:didvan/widgets/didvan/text.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class DidvanTextField extends StatefulWidget {
|
|
final void Function(String value)? onChanged;
|
|
final bool enabled;
|
|
final TextAlign textAlign;
|
|
final String? title;
|
|
final String? hintText;
|
|
final dynamic initialValue;
|
|
final String? Function(String? value)? validator;
|
|
final TextInputType? textInputType;
|
|
const DidvanTextField({
|
|
Key? key,
|
|
this.onChanged,
|
|
this.enabled = true,
|
|
this.title,
|
|
this.hintText,
|
|
this.initialValue,
|
|
this.validator,
|
|
this.textInputType,
|
|
this.textAlign = TextAlign.right,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<DidvanTextField> createState() => _DidvanTextFieldState();
|
|
}
|
|
|
|
class _DidvanTextFieldState extends State<DidvanTextField> {
|
|
final FocusNode _focusNode = FocusNode();
|
|
final TextEditingController _controller = TextEditingController();
|
|
|
|
bool _hasError = false;
|
|
|
|
@override
|
|
void initState() {
|
|
_focusNode.addListener(() {
|
|
setState(() {});
|
|
});
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (widget.title != null) DidvanText(widget.title!),
|
|
if (widget.title != null) const SizedBox(height: 8),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
decoration: BoxDecoration(
|
|
color: _fillColor(),
|
|
borderRadius: DesignConfig.lowBorderRadius,
|
|
border: Border.all(color: _borderColor()),
|
|
),
|
|
child: TextFormField(
|
|
textAlign: widget.textAlign,
|
|
keyboardType: widget.textInputType,
|
|
focusNode: _focusNode,
|
|
controller: _controller,
|
|
onChanged: _onChanged,
|
|
validator: _validator,
|
|
decoration: InputDecoration(
|
|
enabled: widget.enabled,
|
|
border: InputBorder.none,
|
|
hintText: widget.hintText,
|
|
hintStyle: Theme.of(context)
|
|
.textTheme
|
|
.bodyText1!
|
|
.copyWith(color: DesignConfig.greyColor5),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Color _borderColor() {
|
|
if (_focusNode.hasFocus) {
|
|
return Theme.of(context).primaryColor;
|
|
} else if (_hasError) {
|
|
return Theme.of(context).colorScheme.error;
|
|
}
|
|
return DesignConfig.greyColor4;
|
|
}
|
|
|
|
Color _fillColor() {
|
|
if (_focusNode.hasFocus) {
|
|
return DesignConfig.lightPrimaryColor3;
|
|
} else if (_hasError) {
|
|
return DesignConfig.lightRedColor;
|
|
}
|
|
return Theme.of(context).colorScheme.surface;
|
|
}
|
|
|
|
void _onChanged(String value) {
|
|
if (widget.onChanged != null) {
|
|
widget.onChanged!(value);
|
|
}
|
|
}
|
|
|
|
String? _validator(String? value) {}
|
|
}
|