If Else And Nested If Else Conditional Statement In Flutter

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

If condition is works based on Boolean True and False values. In normal If condition there are two parts If part and Else part. If the given condition is True then it will execute the If body part statements. If the given condition is False then it will automatically execute the Else part.

If Else And Nested If Else Conditional Statement In Flutter

IF Else And Nested IF Else Conditional Statement
Complete Code For IF Else And Nested IF Else Conditional Statement 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 checkMethodSimple(int value) {
    if(value > 200)
    {
      print('Value is Bigger than 200');
    }
    else{
      print('Value is Smaller than 200');
    }
  }

  void checkMethodNested(int value){
    if( value > 200 )
    {
      print("Value is Grater than 200.");
    }
    else if(value < 200)
    {
      print("Value is Less than 200.");
    }
    else if(value == 200)
    {
      print("Entered Value is 200");
    }
    else
    {
      print("Sorry, Value not matched.");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.lightGreen,
        title: Text('If Else & Nested IF Else Conditional Statement'),
      ),
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                          child: RaisedButton(
                            onPressed: () => checkMethodSimple(7),
                            child: Text('Call Simple IF-Condition'),
                            textColor: Colors.white,
                            color: Colors.green,
                          )
                      ),
                      Container(
                          child: RaisedButton(
                            onPressed: () => checkMethodNested(200),
                            child: Text('Call Nested IF-Condition'),
                            textColor: Colors.white,
                            color: Colors.indigo,
                          )
                      )

                    ])
            )
    );
  }
}

Related Post