I have function inside my Inherited widget, I would like to use this function with parameters, but I don't get how that can be done. for instance the function below works fine to have inside the inherited widget:
final Function onTap;
However I would like to use something like below:
final Function onTap(String name);
Anyone know if this can be done somehow and in that case how? any help or input appreciated, thanks!
You can use
onTap: () => onTap('foo')
instead of
onTap: onTap
if you want to pass additional parameters
Not sure if that's what you want to do:
You can use typedef to define a callback interface.
import 'package:flutter/material.dart';
typedef void NameTapCallback(String name);
class FooWidget extends StatelessWidget {
final NameTapCallback onTap;
const FooWidget({Key key, this.onTap}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
RaisedButton(
onPressed: () {
onTap('Peter');
},
child: Text('Peter'),
),
RaisedButton(
onPressed: () {
onTap('Paul');
},
child: Text('Paul'),
)
],
);
}
}
// Usage:
var myFooWidget = FooWidget(
onTap: (name) {
print('$name tapped');
},
);
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