Switch case statement is used to compare single value with multiple values and give result in return. Basically switch case statement executes itself one time and given result to app user. Switch case statement evaluates single with other and as per information matched executes code. Switch case is used to execute large conditions code with single data and as per condition execute a fix block of code. So in this tutorial we would Use Switch Case Conditional Statement in Dart Flutter Android iOS Example Tutorial.
Contents in this project Use Switch Case Conditional Statement in Dart Flutter Android iOS Example Tutorial:
1. Import material.dart package in your app’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and now we would call main MyApp root View class.
1 |
void main() => runApp(MyApp()); |
3. Create our main Root MyApp class extends with StatelessWidget.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
4. Create 2 variables String and Integer data type named as day and number.
1 2 3 |
String day = 'Sunday' ; int number = 1 ; |
5. Creating a function named as switchWithString(). In this function we would use the Switch case statement with day String. Using the matched day it will print result on screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
void switchWithString() { switch(day){ case 'Sunday' : print('Today is Sunday'); break; case 'Monday' : print('Today is Monday'); break; case 'Tuesday' : print('Today is Tuesday'); break; default: print('Day Not Found'); } } |
6. Creating another function named as switchWithInt(). In this function we would use the Switch case with Integer value.
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 |
void switchWithInt(){ switch(number){ case 1 : print('ONE'); break; case 2 : print('TWO'); break; case 3 : print('THREE'); break; case 4 : print('FOUR'); break; default : print('Number Not Found'); } } |
7. Creating Widget Build area -> Creating 2 Raised Button and call above both functions.
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 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 0, 20, 0), child: RaisedButton( onPressed: () => switchWithString(), child: Text('Call Switch Case With String'), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 0, 20, 0), child: RaisedButton( onPressed: () => switchWithInt(), child: Text('Call Switch Case With Int Value'), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } |
8. 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 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { String day = 'Sunday' ; int number = 1 ; void switchWithString() { switch(day){ case 'Sunday' : print('Today is Sunday'); break; case 'Monday' : print('Today is Monday'); break; case 'Tuesday' : print('Today is Tuesday'); break; default: print('Day Not Found'); } } void switchWithInt(){ switch(number){ case 1 : print('ONE'); break; case 2 : print('TWO'); break; case 3 : print('THREE'); break; case 4 : print('FOUR'); break; default : print('Number Not Found'); } } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 0, 20, 0), child: RaisedButton( onPressed: () => switchWithString(), child: Text('Call Switch Case With String'), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 0, 20, 0), child: RaisedButton( onPressed: () => switchWithInt(), child: Text('Call Switch Case With Int Value'), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
Screenshots:

NIce
thanks for help