Call Function From Another Class In Main Using Flutter

admin_img Posted By Bajarangi soft , Posted On 10-10-2020

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.

Call Function From Another Class In Main Using Flutter

Call Function From Another Class In Main

Complete Code For Call Function From Another Class In Main In Flutter
main.dart

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 number ;
    number = a + b ;
    return number ;
  }
}
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(
      debugShowCheckedModeBanner: false,
        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.indigo,
                            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.indigo[300],
                            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.indigo,
                            padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
                          )
                      ),
                    ])
            )
        )
    );
  }
}

Related Post