Flutter TextField Loses Focus with setState. I know it’s been a while, maybe something was fixed since then, but I’m not seeing your issue anymore. At least with the way I’m setting up your widget (slight difference between your data.add() vs my fields.add()).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | List<Widget> _fields; ScrollController controller = new ScrollController(); _buildData() { _fields = new List(); for (int i = 0; i < 3; i++) { _fields.add(_createMyTextField('hello$i', i)); } } Widget _createMyTextField(String text, int index) { return MyTextField( text: text, hintText: 'hello hint$index', onChanged: (val) { setState(() { //if last item in list, add an extra field below if (val.length > 0 && index == (_fields.length-1)) { _fields.add(_createMyTextField("", index+1)); } }); }, ); } @override void initState() { super.initState(); _buildData(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(widget.title)), body: ListView( controller: controller, children: _fields.toList() ) ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.