aman-satu-flutter/lib/widgets/fields/input_field_rounded.dart

196 lines
6.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/shared_styles.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'note_text.dart';
class InputFieldRounded extends StatefulWidget {
final TextEditingController controller;
final TextInputType textInputType;
final bool password;
final bool search;
final bool isReadOnly;
final String placeholder;
final String? validationMessage;
final Function? enterPressed;
final bool smallVersion;
final FocusNode? fieldFocusNode;
final FocusNode? nextFocusNode;
final TextInputAction textInputAction;
final bool multiline;
final String? additionalNote;
final Function(String)? onChanged;
final TextInputFormatter? formatter;
final String? initialValue;
final String? labelText;
final String? suffixText;
InputFieldRounded(
{required this.controller,
required this.placeholder,
this.enterPressed,
this.fieldFocusNode,
this.nextFocusNode,
this.additionalNote,
this.onChanged,
this.formatter,
this.suffixText,
this.initialValue,
this.validationMessage,
this.textInputAction = TextInputAction.next,
this.textInputType = TextInputType.text,
this.password = false,
this.search = false,
this.isReadOnly = false,
this.multiline = false,
this.smallVersion = false,
this.labelText});
@override
_InputFieldRoundedState createState() => _InputFieldRoundedState();
}
class _InputFieldRoundedState extends State<InputFieldRounded> {
late bool isPassword;
late bool isSearch;
double fieldHeight = 40;
@override
void initState() {
super.initState();
isPassword = widget.password;
isSearch = widget.search;
if (widget.search == true) {
widget.fieldFocusNode!.addListener(() {
if (widget.fieldFocusNode!.hasFocus) {
setState(() {
isSearch = !isSearch;
});
}
});
}
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (widget.labelText != null)
NoteText(
widget.labelText ?? '',
fontSize: ScreenUtil().setSp(14),
),
if (widget.labelText != null) verticalSpace(5),
Container(
constraints: BoxConstraints(
minHeight: widget.smallVersion ? 40.h : fieldHeight),
alignment: Alignment.centerLeft,
padding: fieldPadding,
decoration:
widget.isReadOnly ? disabledFieldDecoration : BoxDecoration( color: whiteColor , borderRadius: BorderRadius.circular(6.0), boxShadow: [
inputShadowBox
]),
child: Row(
children: <Widget>[
GestureDetector(
onTap: () {
if (isSearch) {
widget.fieldFocusNode!.requestFocus();
} else {
setState(() {
isSearch = !isSearch;
});
FocusScope.of(context)
.requestFocus(new FocusNode()); //remove focus
WidgetsBinding.instance!.addPostFrameCallback(
(_) => widget.controller.clear()); // clear content
}
},
child: widget.search
? Container(
width: fieldHeight,
height: fieldHeight,
alignment: Alignment.center,
child: Icon(isSearch ? Icons.search : Icons.search_off,
color: placeholderColor))
: Container(),
),
Expanded(
child: TextFormField(
style: TextStyle(
color: textColor,
fontSize: widget.smallVersion
? 12
: 14),
controller: widget.controller,
keyboardType: widget.textInputType,
focusNode: widget.fieldFocusNode,
textInputAction: widget.textInputAction,
maxLines: widget.multiline ? null : 1,
onChanged: widget.onChanged,
initialValue: widget.initialValue,
inputFormatters:
widget.formatter != null ? [widget.formatter!] : null,
onEditingComplete: () {
if (widget.enterPressed != null) {
FocusScope.of(context).requestFocus(FocusNode());
widget.enterPressed!();
}
},
onFieldSubmitted: (value) {
if (widget.nextFocusNode != null) {
widget.nextFocusNode!.requestFocus();
}
},
obscureText: isPassword,
readOnly: widget.isReadOnly,
decoration: InputDecoration(
hintText: widget.placeholder,
filled: true,
fillColor: whiteColor,
border: InputBorder.none,
suffixText: widget.suffixText,
hintStyle: TextStyle(
fontSize: widget.smallVersion
? ScreenUtil().setSp(12)
: ScreenUtil().setSp(15),
color: placeholderColor)),
),
),
GestureDetector(
onTap: () => setState(() {
isPassword = !isPassword;
}),
child: widget.password
? Container(
width: fieldHeight,
height: fieldHeight,
alignment: Alignment.center,
child: Icon(
isPassword
? Icons.visibility
: Icons.visibility_off,
color: textColor))
: Container(),
),
],
),
),
if (widget.validationMessage != null)
NoteText(
widget.validationMessage ?? '',
color: dangerColor,
),
if (widget.additionalNote != null) verticalSpace(5),
if (widget.additionalNote != null)
NoteText(widget.additionalNote ?? ''),
verticalSpaceSmall
],
);
}
}