Reputation: 33
I want to retrieve and calculate input values from the textfield in listview .. how to achieve that?
Upvotes: 2
Views: 2705
Reputation: 591
List<TextEditingController> machPresent=[];
machPresent.add(TextEditingController(text: ''));
Container(
height: mainScreenHeight * 0.06,
width: mainScreenWidth * 0.3,
child: TextFormField(
keyboardType: TextInputType.number,
controller: machPresent[index],
enabled: true,
textAlign: TextAlign.center,
),
),
),
Press an action from a button
Container(
height: mainScreenHeight * 0.06,
width: mainScreenWidth * 0.3,
child: RaisedButton(
onPressed: () {
submitForm(index);},
child: Text('Submit',textAlign: TextAlign.center),
),
),
dynamic submitForm(int index) {
var machValue=machPresent[index].text;
var f= machValue;
print(f);
}
Result
I/flutter ( 7914): fff888- -- -- -Select Status
I/flutter ( 7914): sourav- -- -- -Select Status
Upvotes: 0
Reputation: 453
Brilliant. Added some stuff below to suit my use case.
First define a List
like so List<TextEditingController> textFieldControllers=[];
then iterate through the the list used in creating the widgets in the List.builder
like so.
for (var i = 0; i < typesItem.length; i++) {
textFieldControllers.add(TextEditingController());
}
where typesItem
is the List item used in creating the Listbuilder widgets so as to maintain same lenght with your widgets dynamically.You can run this code on your initState
method
there after goto your List.builder widget and initialise your controller like so.
textFieldControllers[index] = TextEditingController() ;
and set your TextField controllers in your return method like so
textFieldControllers[index]
You can therefore get all your value by using
textFieldControllers[index].text;
You can manipulate to suit use your case as you want
Upvotes: 1
Reputation: 7889
You should define a list of TextEditingController
with the same length of your listview
, then assign each TextField
in your list an object of this array, which will enable you to retrieve the value whether when editing or when completed. This is an example of what I've said :
List<TextEditingController> textFieldControllers ;
in your list :
textFieldControllers[index] = new TextEditingController() ;
new TextField(controller: textFieldControllers[index]);
after that you can retrieve the value by :
textFieldControllers[index].text
Upvotes: 2