Open In App

Flutter – Cupertino Switch

Last Updated : 06 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The CupertinoSwitch widget in Flutter is part of the Cupertino-themed widgets, which copy the design and behaviour of iOS widgets. It is used to create a switch or toggle control with an iOS-style appearance. In this article, we are going to implement the CupertinoSwitch widget. A sample video is given below to get an idea about what we are going to do in this article.

Basic Syntax of CupertinoSwitch Widget

CupertinoSwitch(
value: _switchValue, // Boolean value indicating the current state of the switch
onChanged: (bool value) {
// Callback function called when the switch is toggled
setState(() {
_switchValue = value; // Update the state based on the new value
});
},
activeColor: CupertinoColors.activeGreen, // Color when the switch is ON
)

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/cupertino.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart




void main() {
  runApp(MyApp());
}


Step 4: Create MyApp Class

In this class we are going to implement the CupertinoApp, here we can also set the Theme of our App. Here we call MyHomePage class where the CupertinoSwitch Widget is implemented.

Dart




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}


Step 5: Create MyHomePage Class

In this class we are going to Implement the CupertinoSwitch widget that help to create a switch or toggle control with an iOS-style appearance and the State of the CupertinoSwitch is Displayed by a TextView Widget. Comments are added for better understanding.

CupertinoSwitch(
value: switchValue, // Current state of the switch
onChanged: (value) {
setState(() {
switchValue = value; // Update the switch state
});
},
),

Dart




class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
  bool switchValue = false; // Variable to track the switch state
  
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Cupertino Switch Example'), // App bar title
      ),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Switch Value:', // Text indicating the purpose of the CupertinoSwitch
              style: TextStyle(fontSize: 20.0),
            ),
            CupertinoSwitch(
              value: switchValue, // Current state of the CupertinoSwitch
              onChanged: (value) {
                setState(() {
                  switchValue = value; // Update the CupertinoSwitch state
                });
              },
            ),
            SizedBox(height: 20.0), 
  
            Text(
              'Switch is ${switchValue ? 'on' : 'off'}', // Display the CupertinoSwitch state
              style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
            ),
          ],
        ),
      ),
    );
  }
}


Here is the full Code of main.dart file

Dart




import 'package:flutter/cupertino.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}
  
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
  bool switchValue = false; // Variable to track the switch state
  
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Cupertino Switch Example'), // App bar title
      ),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Switch Value:', // Text indicating the purpose of the CupertinoSwitch
              style: TextStyle(fontSize: 20.0),
            ),
            CupertinoSwitch(
              value: switchValue, // Current state of the CupertinoSwitch
              onChanged: (value) {
                setState(() {
                  switchValue = value; // Update the CupertinoSwitch state
                });
              },
            ),
            SizedBox(height: 20.0), 
  
            Text(
              'Switch is ${switchValue ? 'on' : 'off'}', // Display the CupertinoSwitch state
              style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
            ),
          ],
        ),
      ),
    );
  }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads