Flutter Create Call Function From Same Class On Click Button

admin_img Posted By Bajarangi soft , Posted On 03-11-2020

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.

Flutter Create Call Function From Same Class On Click Button

Call Function From Same Class On Click Button
Complete Code For Call Function From Same Class On Click Button In Flutter
main.dart

import 'package:flutter/material.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  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 Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red[900],
        title: Text('Call Function From Same Class'),
      ),
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[

                      Container(
                          child: RaisedButton(
                            onPressed: () => sampleFunction_1(),
                            child: Text(' Function Without Argument '),
                            textColor: Colors.white,
                            color: Colors.red[900],
                          )
                      ),

                      Container(
                          child: RaisedButton(
                            onPressed: () => sampleFunction_2('Hello Word'),
                            child: Text(' Function With String Argument '),
                            textColor: Colors.white,
                            color: Colors.red[400],
                          )
                      ),

                      Container(
                          child: RaisedButton(
                            onPressed: () => sampleFunction_3(145.123),
                            child: Text(' Function With Double Float Argument '),
                            textColor: Colors.white,
                            color: Colors.red[900],
                          )
                      )

                    ])
            )
    );
  }
}

Related Post