Dart is like other programming languages which supports all type of typical methods. Like using another class functions from default class by creating their Object. Object works as Class reference or instance which created by assigning class directly to a Variable in dart. Object is a real time entity used to access all the parent class members in calling child class. So in this tutorial we would Flutter Create Call Function From Another Class in Main Class in Dart Android iOS Example Tutorial.
Contents in this project Flutter Create Call Function From Another Class in Main Class Dart Android iOS Example Tutorial:
1. Import material.dart package in your app’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Create a standalone Class named as Second. This is our class in which we are making 3 different functions.
- randomMSG : Used to print some random message on Terminal screen using print method.
- printText : Using this method first user would pass some string message as argument and then we would print this message on Terminal screen using print.
- returnMSG : At the message calling time we would pass two different values as function argument and then we would calculate these values and return total sum of these.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Second { void randomMSG() { print('Second Class Function Called.'); } void printText(String holder) { print(holder); } double returnMSG(double a, double b){ double c ; c = a + b ; return c ; } } |
5. Create our main class named as MyApp extends with State less widget.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
5. Create a final type variable named as second and here we would assign the class instance to second. Now the second works as Second class object.
1 |
final second = Second(); |
6. Create a function named as fn_1(). Inside this function we would call randomMSG() function of Second class using second object with Dot operator.
1 2 3 4 5 |
void fn_1() { second.randomMSG(); } |
7. Create a function named as fn_2(). Inside the function we would call the second.printText(‘ ‘) function with some string argument. We would also call this function using second object.
1 2 3 4 5 |
void fn_2() { second.printText('Function With Argument Called'); } |
8. Create a function named as fn_3(). Inside the function we would call the returnMSG() function using second object and pass 2 double values along with it as argument. When the sum is done by second class function it will return us the total and we would store the total in holder variable and than print the holder on screen.
1 2 3 4 5 6 7 8 9 |
void fn_3(){ double holder ; holder = second.returnMSG(12, 13); print(holder); } |
9. Creating Widget build area in MyApp class. Now we would make 3 Raised buttons and call above all three functions on button onPress 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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: () => fn_1(), child: Text('Call Another Class Function Without Argument', textAlign: TextAlign.center,), 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: () => fn_2(), child: Text('Call Another Class Function With Argument', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => fn_3(), child: Text('Call Another Class Function With Return Value', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } |
10. 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class Second { void randomMSG() { print('Second Class Function Called.'); } void printText(String holder) { print(holder); } double returnMSG(double a, double b){ double c ; c = a + b ; return c ; } } class MyApp extends StatelessWidget { final second = Second(); void fn_1() { second.randomMSG(); } void fn_2() { second.printText('Function With Argument Called'); } void fn_3(){ double holder ; holder = second.returnMSG(12, 13); print(holder); } @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: () => fn_1(), child: Text('Call Another Class Function Without Argument', textAlign: TextAlign.center,), 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: () => fn_2(), child: Text('Call Another Class Function With Argument', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => fn_3(), child: Text('Call Another Class Function With Return Value', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
Screenshots: