Here we will a function as parameter in Flutter to another function or widget. To pass a function, first of course you need to write your function. Actually you need two things to do
- Write a function that you want to pass
- Write a class or function or a widget that will receive the function
You may write the function anywhere inside the StatefulWidget class. And then you should also write a class or widget that will receive the function as parameter.
Let’s write a simple function that we want to pass. You may also write the function inside a custom widget or Elevated button.
Here you see between line 85-89 is a function. This function you could write anywhere else and just call from here.
So the function was passed inside appTextField widget. Let’s see how it looks like when we receive the function
Here inside the appTextField widget we have received the function. The function name is func. Now this could be used anywhere you need a function. You may use the passed function inside ElevatedButton or TextField function.