I have created a class for new Transaction entry form.. where 2 TextField and a Button..
I have created a Function named myf(), I am almost successful but to improve my knowledge and better coding.... I wish to call function like pointer..but it does not work...where I am getting wrong?
I can solve by repeating code inside onsubmit but its not good level job...
POINTER WORKS WITH TEXTBUTTON AND DOES NOT WORK WITH TEXTFIELD ONSUBMIT..THIS IS MY CONFUSION...
here is my coding
import 'package:flutter/material.dart';
class NewTransaction extends StatelessWidget {
final Function func;
NewTransaction(
this.func
);
void myf() {
func(txttitle.text,double.parse(txtamount.text));
}
final txttitle = TextEditingController();
final txtamount = TextEditingController();
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0,horizontal: 8.0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
elevation: 10,
child: Container(
padding: EdgeInsets.all(10),
child: Column(
children: [
TextField(
controller: txttitle,
onSubmitted: (_){
myf();
},
decoration: InputDecoration(labelText: 'Enter Title'),
),
TextField(
onSubmitted: (_)=>myf, // this one is not working
keyboardType: TextInputType.number,
controller: txtamount,
decoration: InputDecoration(labelText: 'Enter Amount')),
TextButton(
onPressed: myf,
child: Text('Add Record'))
],
),
),
),
);
}
}
CodePudding user response:
While we do ()=>MyFunc()
we create an anonymous(/inline) function.
onSubmitted: (_)=>myf,
means , when onSubmitted
will trigger, just return myf
, here myf
is just a variable, But we need to call the method, that's why you need to use onSubmitted: (_)=>myf()
A better way will be creating myf with parameter,
void myf(String value){}
And use
onSubmitted: myf,
Here is a great answer about inline function and you can check others answers.