Skip to main content

Command Palette

Search for a command to run...

SwitchListTile and Attributes

Updated
3 min read
SwitchListTile and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

SwitchListTile is a Flutter widget that combines a ListTile with a Switch. It is particularly useful when you want to present a setting or an option that can be toggled on or off within a list-based user interface. This widget simplifies the creation of a visual representation of a switch accompanied by a title and subtitle in a material design style.

Here are the primary attributes of the SwitchListTile widget along with examples:

  1. value (bool):

    • Indicates whether the switch is on or off.

    • Example:

        SwitchListTile(
          value: true,
          onChanged: (value) {
            // Handle switch state changes
          },
          title: Text('Enable Feature'),
        ),
      
  2. onChanged (Function(bool)?):

    • Callback function triggered when the switch is toggled.

    • Example:

        SwitchListTile(
          value: true,
          onChanged: (value) {
            // Handle switch state changes
          },
          title: Text('Enable Feature'),
        ),
      
  3. title (Widget):

    • The primary title of the ListTile.

    • Example:

        SwitchListTile(
          value: true,
          onChanged: (value) {
            // Handle switch state changes
          },
          title: Text('Enable Feature'),
        ),
      
  4. subtitle (Widget?):

    • An optional subtitle displayed beneath the title.

    • Example:

        SwitchListTile(
          value: true,
          onChanged: (value) {
            // Handle switch state changes
          },
          title: Text('Enable Feature'),
          subtitle: Text('Toggle to enable or disable the feature'),
        ),
      
  5. secondary (Widget?):

    • An optional widget placed on the trailing side of the list tile.

    • Example:

        SwitchListTile(
          value: true,
          onChanged: (value) {
            // Handle switch state changes
          },
          title: Text('Enable Feature'),
          subtitle: Text('Toggle to enable or disable the feature'),
          secondary: Icon(Icons.settings),
        ),
      
  6. activeColor (Color?):

    • The color to use when the switch is turned on.

    • Example:

        SwitchListTile(
          value: true,
          onChanged: (value) {
            // Handle switch state changes
          },
          title: Text('Enable Feature'),
          activeColor: Colors.green,
        ),
      
  7. tileColor (Color?):

    • The background color of the entire list tile.

    • Example:

        SwitchListTile(
          value: true,
          onChanged: (value) {
            // Handle switch state changes
          },
          title: Text('Enable Feature'),
          tileColor: Colors.grey,
        ),
      

Example

  •   import 'package:flutter/material.dart';
    
      void main() {
        runApp(MyApp());
      }
    
      class MyApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            home: Scaffold(
              appBar: AppBar(
                title: Text('SwitchListTile Example'),
              ),
              body: MySwitchListTile(),
            ),
          );
        }
      }
    
      class MySwitchListTile extends StatefulWidget {
        @override
        _MySwitchListTileState createState() => _MySwitchListTileState();
      }
    
      class _MySwitchListTileState extends State<MySwitchListTile> {
        bool _isFeatureEnabled = false;
    
        @override
        Widget build(BuildContext context) {
          return ListTile(
            title: Text('Enable Feature'),
            subtitle: Text('Toggle to enable or disable the feature'),
            leading: Icon(Icons.home),
            //tileColor: _isFeatureEnabled ? Colors.green : Colors.red,
            trailing: Switch(
              value: _isFeatureEnabled,
              onChanged: (value) {
                setState(() {
                  _isFeatureEnabled = value;
                });
                // Handle switch state changes
                _handleFeatureToggle(value);
              },
              activeColor: Colors.green,
              inactiveThumbColor: Colors.red,
              inactiveTrackColor: Colors.red.withOpacity(0.5),
            ),
          );
        }
    
        void _handleFeatureToggle(bool value) {
          // Add your logic for handling feature toggle here
          if (value) {
            print('Feature is now enabled');
          } else {
            print('Feature is now disabled');
          }
        }
      }
    

This example creates a simple Flutter application with an AppBar and a body containing a custom MySwitchListTile widget. The MySwitchListTile widget defines a ListTile with a title, subtitle, and a Switch. The onChanged callback is used to update the _isFeatureEnabled state variable, and a custom _handleFeatureToggle method can be used to implement the desired logic when the switch is toggled.

Learn Flutter

Part 1 of 50

Explore Flutter's magic in crafting cross-platform apps effortlessly. Join the adventure!

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"