How To Create Default Optional Arguments Using Flutter App

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

Functions are basically a set of statements which allows user to perform a certain task when needed. Basically function should be same on defining time and calling time like their arguments should be same. But we can easily manage them in Dart and assign default values to function. So when app user dose not pass certain value with function it’ll call the default value.

How To Create Default Optional Arguments Using Flutter App

Default Optional Arguments
Complete Code For In Default Optional Arguments 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  getArea(int height, [int width = 50]) {

    print( height * width  );

  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.indigo,
        title: Text('Default Optional Arguments'),
      ),
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                          margin: const EdgeInsets.fromLTRB(20, 10, 20, 10),
                          child:
                          RaisedButton(
                            onPressed: () => getArea(2),
                            child: Text(' Find Area Using Height '),
                            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: () => getArea(2, 10),
                            child: Text(' Find Area Using Both Height Width '),
                            textColor: Colors.white,
                            color: Colors.green,
                            padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
                          )
                      ),

                    ])
            )
    );
  }
}

Related Post