import 'package:flutter/material.dart'; import 'package:flutter/services.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 InputField 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; InputField( { this.controller, @required this.placeholder, this.enterPressed, this.fieldFocusNode, this.nextFocusNode, this.additionalNote, this.onChanged, this.formatter, 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 _InputFieldState createState() => _InputFieldState(); } class _InputFieldState extends State { bool isPassword; bool isSearch; double fieldHeight = 55; @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: [ if (widget.labelText != null) NoteText(widget.labelText), Container( //height: widget.smallVersion ? 40 : fieldHeight, constraints: BoxConstraints(minHeight: widget.smallVersion ? 40 : fieldHeight), alignment: Alignment.centerLeft, padding: fieldPadding, decoration: widget.isReadOnly ? disabledFieldDecoration : fieldDecoration, child: Row( children: [ Expanded( child: TextFormField( style: TextStyle( color: textColor ), 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: Colors.white, border: InputBorder.none, hintStyle: TextStyle(fontSize: widget.smallVersion ? 12 : 15, color: textColorLight)), ), ), 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(), ), GestureDetector( onTap: () { if(isSearch) { widget.fieldFocusNode.requestFocus(); } else { FocusScope.of(context).requestFocus(new FocusNode()); //remove focus WidgetsBinding.instance.addPostFrameCallback((_) => widget.controller.clear()); // clear content } setState(() { isSearch = !isSearch; }); }, child: widget.search ? Container( width: fieldHeight, height: fieldHeight, alignment: Alignment.center, child: Icon(isSearch ? Icons.search : Icons.search_off, color: textColor)) : Container(), ), ], ), ), if (widget.validationMessage != null) NoteText( widget.validationMessage, color: Colors.red, ), if (widget.additionalNote != null) verticalSpace(5), if (widget.additionalNote != null) NoteText(widget.additionalNote), verticalSpaceSmall ], ); } }