Flutter Custom Toggle Switch Example



Have you ever noticed that turning a light switch on or off makes the room feel colder or warmer for a brief period of time? As the same as a flutter toggle switch.

Flutter Toggle Switch

Here is an example of a flutter toggle switch:

Add Dependencies:

dependencies:
  flutter:
    sdk: flutter
  toggle_switch: ^1.3.0

import package:

import 'package:toggle_switch/toggle_switch.dart';

Custom Toggle Switch:

ToggleSwitch(
              minWidth: 200,
              minHeight: 80,
              cornerRadius: 10,
              fontSize: 30,
              iconSize: 25,
              activeBgColors: [[Colors.green],[Colors.amber]],
              activeFgColor: Colors.white,
              inactiveBgColor:  Colors.black26,
              inactiveFgColor: Colors.white,
              totalSwitches: 2,
              labels: ['Male','Female'],
              icons: [Icons.male, Icons.female],
              onToggle: (index) {
                print('Selected item Position: $index');
              },
            ),
Flutter toggle switch
Flutter toggle switch

Toggle switch flutter example

Flutter Toggle Switch

Read also, Flutter Table Calendar Example.

Flutter Toggle Switch Full Source Code

import 'package:flutter/material.dart';
import 'package:toggle_switch/toggle_switch.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}
  @override
class _MyAppState extends State<MyApp> {

  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: Center(
            child: ToggleSwitch(
              minWidth: 200,
              minHeight: 80,
              cornerRadius: 10,
              fontSize: 30,
              iconSize: 25,
              activeBgColors: [[Colors.green],[Colors.amber]],
              activeFgColor: Colors.white,
              inactiveBgColor:  Colors.black26,
              inactiveFgColor: Colors.white,
              totalSwitches: 2,
              labels: ['Male','Female'],
              icons: [Icons.male, Icons.female],
              onToggle: (index) {
                print('Selected item Position: $index');
              },
            ),
          ),
          ),
        );
  }
}
  • How to Conduct YouTube Keyword Research Easily?
    Introduction YouTube is the second largest search engine in the world, making it a valuable platform for content creators and businesses alike. To maximize your visibility and reach on YouTube, it is crucial to conduct proper keyword research. By identifying the right keywords, you can optimize your videos for search and increase your chances of …

    Read more …

  • How to upgrade the Dart SDK version in flutter
    If you’re a Flutter developer, you know how important it is to stay up-to-date with the latest version of the Dart SDK. Upgrading the Dart SDK ensures that you have access to the latest features, bug fixes, and performance improvements. Step 1: Check Your Current Dart SDK Version Before you begin the upgrade process, it’s …

    Read more …

  • Why Do Advertisers Use Testimonials?
    In the ever-competitive business landscape, nurturing strong connections with your customers remains paramount for enhancing your credibility and positioning yourself as an industry authority. Establishing credibility is the bedrock upon which trust is built, ultimately translating into increased sales. To bolster your company’s credibility, integrating testimonial advertising into your marketing strategy is essential. So, what …

    Read more …

  • How To Earn $5 Fast Method!
    Looking to boost your income by $25 in less than an hour? This isn’t the start of a clichéd sales pitch; it’s about discovering several quick and simple ways to earn $5 online within minutes. For instance, you can make five bucks by completing tasks such as taking brief surveys, participating in short website usability …

    Read more …

  • What Does BMF Mean On TikTok? Latest Social Media Acronyms
    In the fast-paced world of social media, acronyms are the language of choice, enabling users to communicate swiftly without the need for lengthy phrases. While classics like LOL and OMG are still in use, the younger generation on TikTok has introduced a new acronym into the mix: BMF. So, what exactly does BMF mean, and …

    Read more …

Leave a Comment