Hellow i have fetched data frome firestore to listTiles using listview,now i want to set the particular list tile value into the textField as i tap on it. this is what i have ringht now;
ListView.separated(
shrinkWrap: true,
itemCount: snapshot.data!['Categories'] == null
? 0
: snapshot
.data![
'Categories']
?.length,
separatorBuilder:
(BuildContext context,
int index) =>
Divider(
color:
Colors.grey,
),
itemBuilder:
(BuildContext context,
int index) =>
Container(
child: Padding(
padding: const EdgeInsets
.only(
left: 20,
right:
20),
child:
ListTile(
leading:
CircleAvatar(
radius:
15,
child: Text(
'${index 1}'),
),
trailing: IconButton(
onPressed: () {
//DELETE CATEGORY
},
icon: Icon(
Icons
.delete_forever,
color:
Colors.red,
size:
30,
)),
onTap: (){
// set textfield value
},
title:
Center(
child:
Text(
snapshot.data!["Categories"][index]
[
"name"],
style: TextStyle(
color:
Colors.black),
),
),
),
),
))
CodePudding user response:
Add a TextEditingController to your TextField Or TextFormField like so
class SomeWidget extends StatefulWidget {
@override
_SomeWidgetState createState() => _SomeWidgetState();
}
class _SomeWidgetState extends State<SomeWidget> {
final TextEditingController textFieldController = TextEditingController();
//..
}
TextFormField(
controller: textFieldController,
//...
)
And when you tab on list item, run:
textFieldController.text = 'Your value'; // your list item's value
CodePudding user response:
Try this solution, Hope it will work for you. Thanks.
class ClassName extends StatefulWidget {
const ClassName({Key? key}) : super(key: key);
@override
_ClassNameState createState() => _ClassNameState();
}
class _ClassNameState extends State<ClassName> {
final TextEditingController dataController = TextEditingController();
String value = "";
@override
Widget build(BuildContext context) {
return Container(
child: ListView.separated(
shrinkWrap: true,
itemCount: snapshot.data!['Categories'] == null
? 0
: snapshot.data!['Categories']?.length,
separatorBuilder: (BuildContext context, int index) => Divider(
color: Colors.grey,
),
itemBuilder: (BuildContext context, int index) => Container(
child: Padding(
padding: const EdgeInsets.only(left: 20, right: 20),
child: TextField(
onChanged: (text) {
value = text;
},
),
///OR
child: TextField(
controller: dataController,
onChanged: (text){
controller.text = snapshot.data!["Categories"][index]["name"];
},
),
// child: ListTile(
// leading: CircleAvatar(
// radius: 15,
// child: Text('${index 1}'),
// ),
// trailing: IconButton(
// onPressed: () {
// //DELETE CATEGORY
// },
// icon: Icon(
// Icons.delete_forever,
// color: Colors.red,
// size: 30,
// )),
// onTap: () {
// // set textfield value
// },
// title: Center(
// child: Text(
// snapshot.data!["Categories"][index]["name"],
// style: TextStyle(color: Colors.black),
// ),
// ),
// ),
),
)),
);
}
}
