I want to add widgets (like extra border) when the text field is focused.
I’ve simplified my problem.
class CustomTextField extends StatefulWidget {
const CustomTextField({super.key});
@override
State<CustomTextField> createState() => _CustomTextFieldState();
}
class _CustomTextFieldState extends State<CustomTextField> {
final _widgetStatesController = WidgetStatesController();
bool isFocused = false;
@override
void initState() {
_widgetStatesController.addListener(() {
setState(() {
isFocused = _widgetStatesController.value.contains(WidgetState.focused);
});
});
super.initState();
}
@override
void dispose() {
_widgetStatesController.dispose();
super.dispose();
}
@override
Widget build(final BuildContext context) {
Widget textField = TextField(
statesController: _widgetStatesController,
);
if (isFocused) {
textField = Column(
children: [
textField,
const Text('isFocused'),
],
);
}
return textField;
}
}
When I tap on text field, I have this error:
The following assertion was thrown while rebuilding dirty elements:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 5212 pos 12: '!_dirty': is not true.
3
Answers
I don’t know how this simplifies your problem, have you tried using FocusNode though ? Below an example
Try this one
Try below code