Use VoidCallback type
Check the line comments on the code as well for more information:
class Button extends StatelessWidget {
final String text;
final VoidCallback callback; // Notice the variable type
Button(this.text, this.callback);
@override
Widget build(BuildContext context) {
return Container(
height: 50,
child: SizedBox(
width: double.infinity,
child: RaisedButton(
onPressed: callback, // Simply put the function name here, DON'T use ()
child: Padding(
padding: EdgeInsets.symmetric(vertical: 13),
child: Text(
text,
style: TextStyle(fontWeight: FontWeight.bold),
)),
color: COLOR_BLUE,
textColor: Colors.white,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
),
),
);
}
}
- By omitting the parentheses, you are passing the function itself as a parameter. If you added the parentheses, the function would be executed and you would pass the return of the function to the widget.
Then, initialize your widget:
Button("Log in", myMethod)
or
Button("Log in", (){
print('something');
})