How To Use Switch Case Conditional Statement Using Flutter

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

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.

How To Use Switch Case Conditional Statement Using Flutter

Switch Case Conditional Statement
Complete Code For Switch Case 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 {
  String day = 'Tuesday' ;
  int number = 5 ;

  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('1');
        break;

      case 2 :
        print('2');
        break;

      case 3 :
        print('3');
        break;

      case 4 :
        print('4');
        break;

      default :
        print('Number Not Found');
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.orange[600],
        title: Text('Switch Case Conditional Statement'),
      ),
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[

                      Container(
                          child: RaisedButton(
                            onPressed: () => switchWithString(),
                            child: Text('Call Switch Case With String'),
                            textColor: Colors.white,
                            color: Colors.orange,
                          )
                      ),

                      Container(
                          child: RaisedButton(
                            onPressed: () => switchWithInt(),
                            child: Text('Call Switch Case With Int Value'),
                            textColor: Colors.white,
                            color: Colors.orange,
                          )
                      ),

                    ])
            )
    );
  }
}

Related Post