import 'package:flutter/material.dart'; class MyTextField extends StatefulWidget { final controller; final String hintText; final bool editable; final bool required; const MyTextField({ super.key, required this.controller, required this.hintText, required this.editable, required this.required, }); @override State createState() => _MyTextFieldState(); } class _MyTextFieldState extends State { bool startup = true; FocusNode _focus = FocusNode(); bool makeEditable() { if (widget.editable) { return false; } else { return true; } } String? get _errorText { final text = widget.controller.text; if (startup) { return null; } if (!widget.required) { return null; } if (text.isEmpty) { return "${widget.hintText} is required"; } return null; } void _onFocusChange() { setState(() { startup = false; }); } Widget setRequiredText() { if (widget.required) { return Row( mainAxisSize: MainAxisSize.min, children: [ const Text( "*", style: TextStyle(color: Colors.red), ), const SizedBox( width: 8.0, ), Text(widget.hintText, style: const TextStyle(color: Colors.blueAccent)), ], ); } else { return Text(widget.hintText, style: const TextStyle(color: Colors.blueAccent)); } } @override void initState() { _focus.addListener(_onFocusChange); super.initState(); } @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 25.0), child: TextField( controller: widget.controller, focusNode: _focus, readOnly: makeEditable(), obscureText: false, onChanged: (_) => setState(() { startup = false; }), decoration: InputDecoration( label: setRequiredText(), //labelStyle: const TextStyle(color: Colors.blueAccent), fillColor: Colors.white, filled: true, errorText: _errorText, //hintText: hintText, //hintStyle: TextStyle(color: Colors.blueGrey[400]), enabledBorder: const OutlineInputBorder( borderSide: BorderSide( color: Colors.blueAccent, width: 2.0, ), ), focusedBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.blue), ), ), ), ); } }