1
votes

In main, the body of my scaffold is a custom stateful widget. This custom widget has a function inside its state class. Is it possible to call this function from the floating action button in the main file's scaffold?

I don't see how 'wire' the onPressed function of the floating action button to call the function inside the state class of the widget in the scaffold's body.

1
Hey I don't think that is how you should be implementing onPressed methods. Can you give a little more info of what you are trying to achieveMigalv

1 Answers

0
votes

You can use function callback like this

class Screen extends StatefulWidget {
  Screen({Key key}) : super(key: key);

  @override
  _ScreenState createState() => _ScreenState(methodCaller: myMethod);

  String myMethod(int value) {
    return 'example';
  }
}

class _ScreenState extends State<Screen> {
  final String Function(int value) methodCaller;

  _ScreenState({this.methodCaller});
  @override
  Widget build(BuildContext context) {
    var value = methodCaller(12);
    return Container();
  }
}

Hope this is helpful!