216 lines
6.0 KiB
Dart
216 lines
6.0 KiB
Dart
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
@immutable
|
|
class RTextField extends StatefulWidget {
|
|
RTextField(
|
|
{super.key,
|
|
this.maxLines,
|
|
this.maxLength,
|
|
this.hintText,
|
|
this.padding,
|
|
this.onChanged,
|
|
this.onSubmitted,
|
|
this.keyboardType,
|
|
this.showCounter = false,
|
|
this.isDense,
|
|
this.initText,
|
|
this.isForNumber = false,
|
|
this.style,
|
|
this.hintStyle,
|
|
this.suffixIcon,
|
|
this.prefixIcon,
|
|
this.validator,
|
|
this.readonly = false,
|
|
this.boxConstraints,
|
|
this.minLines,
|
|
this.radius,
|
|
this.filled,
|
|
this.enabled,
|
|
this.errorStyle,
|
|
this.labelStyle,
|
|
this.label}) {
|
|
filled = filled ?? false;
|
|
obscure = false;
|
|
_inputBorder = OutlineInputBorder(
|
|
borderSide: BorderSide(color: Colors.grey.shade300),
|
|
borderRadius: BorderRadius.circular(radius ?? 16));
|
|
}
|
|
|
|
RTextField.noBorder(
|
|
{super.key,
|
|
this.maxLines,
|
|
this.maxLength,
|
|
this.hintText,
|
|
this.padding,
|
|
this.onChanged,
|
|
this.onSubmitted,
|
|
this.keyboardType,
|
|
this.showCounter = false,
|
|
this.isDense,
|
|
this.initText,
|
|
this.style,
|
|
this.hintStyle,
|
|
this.suffixIcon,
|
|
this.radius,
|
|
this.validator,
|
|
this.boxConstraints,
|
|
this.minLines,
|
|
this.isForNumber = false,
|
|
this.readonly = false,
|
|
this.label,
|
|
this.filled,
|
|
this.errorStyle,
|
|
this.labelStyle,
|
|
this.enabled}) {
|
|
_inputBorder = OutlineInputBorder(
|
|
borderSide: BorderSide.none,
|
|
borderRadius: BorderRadius.circular(radius ?? 16));
|
|
obscure = false;
|
|
filled = filled ?? true;
|
|
}
|
|
|
|
RTextField.password(
|
|
{super.key,
|
|
this.maxLines = 1,
|
|
this.maxLength,
|
|
this.hintText,
|
|
this.padding,
|
|
this.onChanged,
|
|
this.onSubmitted,
|
|
this.keyboardType,
|
|
this.showCounter = false,
|
|
this.isDense,
|
|
this.initText,
|
|
this.style,
|
|
this.hintStyle,
|
|
this.suffixIcon,
|
|
this.prefixIcon,
|
|
this.radius,
|
|
this.validator,
|
|
this.boxConstraints,
|
|
this.minLines,
|
|
this.isForNumber = false,
|
|
this.readonly = false,
|
|
this.label,
|
|
this.filled,
|
|
this.errorStyle,
|
|
this.labelStyle,
|
|
this.enabled}) {
|
|
_inputBorder = OutlineInputBorder(
|
|
borderSide: BorderSide.none,
|
|
borderRadius: BorderRadius.circular(radius ?? 16));
|
|
filled = filled ?? true;
|
|
obscure = true;
|
|
_isPassword = true;
|
|
prefixIcon = prefixIcon ?? const Icon(CupertinoIcons.person);
|
|
}
|
|
|
|
final int? maxLines;
|
|
final int? minLines;
|
|
final int? maxLength;
|
|
final String? hintText;
|
|
final String? label;
|
|
final EdgeInsets? padding;
|
|
final TextStyle? style;
|
|
final TextStyle? errorStyle;
|
|
final TextStyle? hintStyle;
|
|
final TextStyle? labelStyle;
|
|
final bool showCounter;
|
|
final bool? isDense;
|
|
final bool? isForNumber;
|
|
final bool readonly;
|
|
bool? obscure;
|
|
final bool? enabled;
|
|
final double? radius;
|
|
final TextInputType? keyboardType;
|
|
final Function(String)? onChanged;
|
|
final Function(String)? onSubmitted;
|
|
final FormFieldValidator? validator;
|
|
final String? initText;
|
|
Widget? suffixIcon;
|
|
Widget? prefixIcon;
|
|
bool? filled;
|
|
bool _isPassword = false;
|
|
|
|
final BoxConstraints? boxConstraints;
|
|
late final InputBorder? _inputBorder;
|
|
|
|
@override
|
|
State<RTextField> createState() => _RTextFieldState();
|
|
}
|
|
|
|
class _RTextFieldState extends State<RTextField> {
|
|
final TextEditingController _controller = TextEditingController();
|
|
bool? obscure;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.initText != null) {
|
|
_controller.text = widget.initText!;
|
|
}
|
|
obscure = widget.obscure;
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant RTextField oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.initText != oldWidget.initText) {
|
|
_controller.text = widget.initText ?? '';
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: widget.padding ?? const EdgeInsets.symmetric(vertical: 6.0),
|
|
child: TextFormField(
|
|
controller: _controller,
|
|
readOnly: widget.readonly,
|
|
minLines: widget.minLines,
|
|
maxLines: widget.maxLines,
|
|
onChanged: widget.onChanged,
|
|
validator: widget.validator,
|
|
enabled: widget.enabled,
|
|
obscureText: obscure ?? false,
|
|
onTapOutside: (event) {
|
|
FocusScope.of(context).unfocus();
|
|
},
|
|
onFieldSubmitted: widget.onSubmitted,
|
|
maxLength: widget.maxLength,
|
|
textDirection: TextDirection.rtl,
|
|
style: widget.style ,
|
|
keyboardType: widget.keyboardType,
|
|
decoration: InputDecoration(
|
|
errorStyle: widget.errorStyle,
|
|
errorMaxLines: 1,
|
|
isDense: widget.isDense,
|
|
suffixIcon: widget.suffixIcon ??
|
|
(widget._isPassword
|
|
? IconButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
obscure = !obscure!;
|
|
});
|
|
},
|
|
icon: Icon(!obscure!
|
|
? CupertinoIcons.eye_slash
|
|
: CupertinoIcons.eye))
|
|
: null),
|
|
suffixIconConstraints: widget.boxConstraints,
|
|
prefixIcon: widget.prefixIcon,
|
|
prefixIconConstraints: widget.boxConstraints,
|
|
hintText: widget.hintText,
|
|
labelText: widget.label,
|
|
labelStyle: widget.labelStyle,
|
|
filled: widget.filled,
|
|
counter: widget.showCounter ? null : const SizedBox(),
|
|
hintStyle: widget.hintStyle,
|
|
enabledBorder: widget._inputBorder,
|
|
focusedBorder: widget._inputBorder,
|
|
border: widget._inputBorder),
|
|
));
|
|
}
|
|
} |