Created
September 9, 2022 10:33
-
-
Save ymkp/9b9dcbc70f2029b5d02c7377fee6cbcf to your computer and use it in GitHub Desktop.
Revisions
-
ymkp created this gist
Sep 9, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,140 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:eppid/const/theme.dart'; class CustomFormTextField extends StatefulWidget { const CustomFormTextField({ required this.name, required this.validator, this.hintText = '', this.onSaved, this.isPassword = false, this.textType = TextInputType.text, this.iconButton, this.readOnly = false, this.onTap, this.initialValue, this.enabled = true, this.inputFormatters, this.onEditingComplete, this.onChanged, this.validatorColor = kRed, this.lines, }); final String name; final String hintText; final String validator; final Function(String? s)? onSaved; final String? initialValue; final bool isPassword; final TextInputType textType; final IconButton? iconButton; final bool readOnly; final Function()? onTap; final bool enabled; final List<TextInputFormatter>? inputFormatters; final Function()? onEditingComplete; final Function(String? s)? onChanged; final Color validatorColor; final int? lines; @override _CustomFormTextFieldState createState() => _CustomFormTextFieldState(); String getValue() => _CustomFormTextFieldState().controller.text; } class _CustomFormTextFieldState extends State<CustomFormTextField> { bool _showPassword = false; TextEditingController controller = TextEditingController(); final FocusNode _focusNode = FocusNode(); String? Function(String? s)? _validator; @override void initState() { // ? bikin validator di sini super.initState(); _setValidator(); } _setValidator() { if (widget.validator == 'email') { _validator = (val) { if (val == '') { return 'Email tidak boleh kosong'; } // ? ganti fungsi untuk ngecek string ini // ? email atau bukan, pake get yg isEmail mungkin // ? !val.toString().isEmail // ? kalo bukan email, tampilin email tdk valid if (1 + 1 == 3) { return 'Email tidak valid'; } else { return null; } }; } } @override Widget build(BuildContext context) { return TextFormField( focusNode: _focusNode, onSaved: widget.onSaved, minLines: widget.lines, maxLines: widget.lines != null ? null : 1, decoration: InputDecoration( hintText: widget.hintText, hintStyle: const TextStyle( color: Colors.grey, fontSize: 14, height: 1.5, ), filled: true, fillColor: widget.enabled ? Colors.white : kGrey1, suffixIcon: !widget.isPassword ? widget.iconButton : IconButton( onPressed: () { setState(() { _showPassword = !_showPassword; }); }, icon: Icon( _showPassword ? Icons.visibility : Icons.visibility_off, color: _showPassword ? kBlue : kGrey1, ), ), contentPadding: EdgeInsets.symmetric( horizontal: 13, vertical: (widget.lines != null) ? 8 : 0), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: kGrey1), ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: kGrey1), ), errorStyle: TextStyle( fontSize: 12, color: widget.validatorColor, fontStyle: FontStyle.italic), ), obscureText: widget.isPassword ? !_showPassword : false, validator: _validator, keyboardType: widget.textType, cursorColor: kBlue, readOnly: widget.readOnly, controller: controller, onTap: widget.onTap, enabled: widget.enabled, inputFormatters: widget.inputFormatters, onEditingComplete: widget.onEditingComplete, onChanged: widget.onChanged, initialValue: widget.initialValue, onFieldSubmitted: widget.onSaved, ); } }