xxxxxxxxxx
void main() {
String fruit = 'apple';
switch (fruit) {
case 'apple':
print('Selected fruit is apple');
break;
case 'banana':
print('Selected fruit is banana');
break;
case 'orange':
print('Selected fruit is orange');
break;
default:
print('Selected fruit is unknown');
}
}
xxxxxxxxxx
bool isChecked = false;
Switch(
value: isChecked,
onChanged: (bool value) {
setState(() {
isChecked = value;
});
},
),
xxxxxxxxxx
var command = 'OPEN';
switch (command) {
case 'CLOSED':
executeClosed();
break;
case 'DENIED':
executeDenied();
break;
case 'OPEN':
executeOpen();
break;
default:
executeUnknown();
}
xxxxxxxxxx
switch(variable_expression) {
case constant_expr1: {
// statements;
}
break;
case constant_expr2: {
//statements;
}
break;
default: {
//statements;
}
break;
}
xxxxxxxxxx
int value = 0;
switch (value) {
case 0:
// do something
break;
case 1:
// do something else
break;
default :
// something if anything not match
}
xxxxxxxxxx
// switch case example in dart
void main() {
// you can change value of the luckyNumber..
int luckyNumber = 70;
switch(luckyNumber) {
case 10: {
print("You got number 10!");
break;
}
case 30: {
print("You got number 20");
break;
}
case 60: {
print("You got number 20");
break;
}
default:
print("You got nothing!");
}
}
// output: You got nothing!
xxxxxxxxxx
String commentMark(int mark) {
switch (mark) {
case 0 : // Enter this block if mark == 0
return "mark is 0" ;
case 1:
case 2:
case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
return "mark is either 1, 2 or 3" ;
// etc.
default :
return "mark is not 0, 1, 2 or 3" ;
}
}
xxxxxxxxxx
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool switchValue = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Switch Example'),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Switch'),
SizedBox(width: 16.0),
Switch(
value: switchValue,
onChanged: (newValue) {
setState(() {
switchValue = newValue;
});
},
),
],
),
),
),
);
}
}