4

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!

1
  • Please provide more context Commented Dec 23, 2018 at 15:50

2 Answers 2

6

You can use

onTap: () => onTap('foo')

instead of

onTap: onTap

if you want to pass additional parameters

Sign up to request clarification or add additional context in comments.

Comments

3

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');
  },
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.