Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to use parameters inside defined function inside inherited widget

Tags:

flutter

dart

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!

like image 939
Mattias Avatar asked Oct 20 '25 14:10

Mattias


2 Answers

You can use

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

instead of

onTap: onTap

if you want to pass additional parameters

like image 123
Günter Zöchbauer Avatar answered Oct 23 '25 05:10

Günter Zöchbauer


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');
  },
);
like image 20
boformer Avatar answered Oct 23 '25 03:10

boformer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!