Hi i have myWidget
and i pass the focusNode var to it from EditPopUpParent()
class, when i am focusing TextField
value of focusNode.hasFocus
is changing and i can see it by printing it out by ElevatedButton but Container do not show up, why is that?
import 'package:flutter/material.dart';
import 'package:prakty/providers/edituser.dart';
import 'package:provider/provider.dart';
class EditPopUpParent extends StatefulWidget {
const EditPopUpParent({super.key});
@override
State<EditPopUpParent> createState() => _EditPopUpParentState();
}
class _EditPopUpParentState extends State<EditPopUpParent> {
@override
Widget build(BuildContext context) {
FocusNode focusNode = FocusNode();
List<Widget> editWidgetTypes = [
Container(),
editSkillSet(context, focusNode),
];
return Container(child: editWidgetTypes[1]);
}
Widget editSkillSet(context, FocusNode focusNode) {
var skillBoxes = Provider.of<EditUser>(context).skillBoxes;
final TextEditingController skillCont =
TextEditingController(text: skillBoxes[1].keys.single);
return Column(
children: [
Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Visibility(
visible: focusNode.hasFocus,
child: Container(
width: 100,
height: 100,
color: Colors.red,
child: const Icon(Icons.abc))),
TextField(
controller: skillCont,
textInputAction: TextInputAction.done,
focusNode: focusNode,
decoration:
const InputDecoration.collapsed(hintText: 'Type Something')),
ElevatedButton(
onPressed: () {
print(focusNode.hasFocus);
},
child: const Icon(Icons.check))
]),
],
);
}
}
2
Answers
Seems like you are expecting to counter the
Visibility
widget on press ofElevatedButton
The issue is that you are creating the
FocusNode
locally within the build method of_EditPopUpParentState
. This means a newFocusNode
is created each time the build runs.Try: