In computer programming language Conditional Statement is important part for performing actions according to given condition. 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 condition is used to compare only 1 condition but if you have more than one condition then we will use Nested IF Else Conditional Statement in Flutter Dart Android iOS Example Tutorial. So let’s get started :).
IF Else Conditional Statement Syntax in Flutter Dart:
1 2 3 4 5 6 7 8 |
if (Condition) { code to be executed on condition true; } else { code to be executed on condition false; } |
Nested IF Else Conditional Statement Syntax in Flutter Dart:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
if( Condition 1 ) { code to be executed on condition true; } else if( Condition 2 ) { code to be executed on condition true; } else if( Condition 3 ) { code to be executed on condition true; } else { code to be executed if all above conditions is false; } |
Contents in this project IF Else & Nested IF Else Conditional Statement in Flutter Dart Android iOS Example Tutorial:
1. Import material.dart package in your project’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create our main Void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Create our main MyApp class extends with StatelessWidget.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Create a function named as checkMethodSimple() with integer argument. Inside the function we would use the If condition and compare the given integer number with 100 and than execute the result. We are printing a Console message using Print method of Dart.
1 2 3 4 5 6 7 8 9 10 |
void checkMethodSimple(int value) { if(value > 100) { print('Value is Bigger than 100'); } else{ print('Value is Smaller than 100'); } } |
5. Creating another function named as checkMethodNested() with integer parameter. Inside the function we would use the Nested If Else conditional statement and compare multiple conditions.
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 |
void checkMethodNested(int value){ if( value > 100 ) { print("Entered Value is Grater than 100."); } else if(value < 100) { print("Entered Value is Less than 100."); } else if(value == 100) { print("Entered Value is 100"); } else { print("Sorry, Value not matched."); } } |
6. Creating Widget build area in MyApp class. Now we would make a Scaffold widget and create a Column widget. We would also create 2 Raised Button widget wrap in Container widget in Column Widget. We would call each function 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 |
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: () => checkMethodSimple(7), child: Text('Call Simple IF-Condition'), 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: () => checkMethodNested(100), child: Text('Call Nested IF-Condition'), textColor: Colors.white, color: Colors.indigo, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ) ]) ) ) ); } |
6. 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 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { void checkMethodSimple(int value) { if(value > 100) { print('Value is Bigger than 100'); } else{ print('Value is Smaller than 100'); } } void checkMethodNested(int value){ if( value > 100 ) { print("Entered Value is Grater than 100."); } else if(value < 100) { print("Entered Value is Less than 100."); } else if(value == 100) { print("Entered Value is 100"); } else { print("Sorry, Value not matched."); } } @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: () => checkMethodSimple(7), child: Text('Call Simple IF-Condition'), 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: () => checkMethodNested(100), child: Text('Call Nested IF-Condition'), textColor: Colors.white, color: Colors.indigo, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ) ]) ) ) ); } } |
Screenshots:
