There are multiple type of functions available in Flutter like other languages. Like Function Without Argument, Function with single argument and Function with multiple argument. The arguments are the values which we have send along with function calling time inside function scope. The argument values automatically send along the function and using them we would perform certain task. There are main two types of functions in function category like Void and non-Void functions. We are only creating Void non value returnable functions in our tutorial and all the methods are created inside same class. When we call function from same class then there is no need to create object for calling functions. We can call the functions directly from their names. So in this tutorial we would Flutter Create Call Function From Same Class on Button Click Android iOS Example Tutorial.
Contents in this project Flutter Create Call Function From Same Class on Button Click Android iOS Example Tutorial:
1. Open your project’s main.dart file and import material.dart package.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call our main MyApp parent class.
1 |
void main() => runApp(MyApp()); |
3. Create our main class named as MyApp extends StatelessWidget.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
4. Now we would create a single void function named as sampleFunction_1(). Inside the function i am only printing a message on Terminal screen using Print method. One more thing i am using void keyword with function but in flutter latest version there is no need to use the void keyword you can directly declare the function with function name.
1 2 3 |
void sampleFunction_1() { print('Function Called'); } |
5. Creating another function named as sampleFunction_2() with String argument. We would send a Sting value with function on function calling time from Button.
1 2 3 |
void sampleFunction_2(String holder) { print(holder); } |
6. Creating another function named as sampleFunction_3() with Float double value.
1 2 3 |
void sampleFunction_3(double value){ print(value); } |
7. Creating Widget build area in MyApp class. Now first we would create a Center widget in Body section of Scaffold widget and Create a Column widget.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ ]) ) ) ); } |
8. Creating 3 Raised button widgets in Container widgets in Column widgets. We would call each function on Button onPressed event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => sampleFunction_1(), child: Text(' Function Without Argument '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => sampleFunction_2('Hello_Text'), child: Text(' Function With String Argument '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => sampleFunction_3(123.123), child: Text(' Function With Double Float Argument '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ) |
9. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { void sampleFunction_1() { print('Function Called'); } void sampleFunction_2(String holder) { print(holder); } void sampleFunction_3(double value){ print(value); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => sampleFunction_1(), child: Text(' Function Without Argument '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => sampleFunction_2('Hello_Text'), child: Text(' Function With String Argument '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => sampleFunction_3(123.123), child: Text(' Function With Double Float Argument '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ) ]) ) ) ); } } |
Screenshots:
