I am trying to create an overlay widget for TextFormField suffix Icon. Normally we would be using ToolTip, but just trying something new because the overlay widget can be customized. I want to change the suffix Icon Color of TextFormField if it is not validated from Grey to Red. So when the Icon becomes red it alerts the user that something is wrong, when the user clicks on it overlay widget will be shown.
My Code for OverLay widget.
void _showOverlay(BuildContext context) async {
OverlayState? overlayState = Overlay.of(context);
OverlayEntry overlayEntry;
overlayEntry = OverlayEntry(builder: (context) {
return Positioned(
left: MediaQuery.of(context).size.width * 0.1,
top: MediaQuery.of(context).size.height * 0.23,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Material(
child: Container(
alignment: Alignment.center,
color: Colors.grey.shade200,
padding:
EdgeInsets.all(MediaQuery.of(context).size.height * 0.02),
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.height * 0.06,
child: const Text(
'Name should be more than 2 characters',
style: TextStyle(color: Colors.black),
),
),
),
),
);
});
overlayState!.insert(overlayEntry);
await Future.delayed(const Duration(seconds: 3));
overlayEntry.remove();
}
My Submit Button method:
void _submitForm() {
setState(() {
_autoValidateMode = AutovalidateMode.always;
});
final form = _formKey.currentState;
if (form == null || !form.validate()) return;
form.save();
print(_name);
}
My TextFormField widget:
TextFormField(
controller: nameController,
keyboardType: TextInputType.name,
textInputAction: TextInputAction.next,
textCapitalization: TextCapitalization.words,
validator: (String? value) {
if (value == null || value.trim().isEmpty) {
return;
}
return null;
},
onSaved: (String? value) {
_name = value;
},
decoration: kTextInputDecoration.copyWith(
labelText: 'Full Name',
prefixIcon: const Icon(Icons.person),
suffixIcon: IconButton(
padding: EdgeInsets.zero,
onPressed: () {
_showOverlay(context);
},
icon: const Icon(
Icons.info,
color: Colors.grey //change icon color according to form validation
))),
My submit button.
ElevatedButton(
onPressed: () {
_submitForm();
},
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(10)),
child: const Text(
'Submit',
style: TextStyle(fontSize: 20),
)),
I want to change the color of the suffix icon color when the submit button is pressed. If the form is not validated the color should change to red or the default is grey. Thank you very much in advance for your help.