I am trying to call a function upon an onPressed in Flutter.
I've tried
onPressed: (){
_showDialog;
},
and
onPressed: _showDialog,
and
onPressed: () => _showDialog,
This is my function.
void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Title"),
content: Text("Body"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
I keep getting "invalid constant value".
EDIT:
This is where I'm calling onPressed:
secondary: const IconButton(
icon: Icon(Icons.domain),
onPressed: (){
_showDialog();
},
),
UPDATE
Easy fix: remove const keyword
secondary: IconButton(
icon: Icon(Icons.domain),
onPressed: (){
_showDialog();
},
),
Old Answer
You should try like these ways:
onPressed: (){
_showDialog();
},
or
onPressed: _showDialog,
or
onPressed: () => _showDialog(),
You need to check and remove contanier(widget) const
like this
child: TextFormField(
decoration: **const** InputDecoration(
prefixIcon: Icon(Icons.person),
suffixIcon: IconButton(
icon: Icon(Icons.remove_red_eye),
onPressed: showPassword,
),
labelText: "Name *",
hintText: "Your Github account username",
),
)
child: TextFormField(
decoration:InputDecoration(
...
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With