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:
value (bool):
Indicates whether the switch is on or off.
Example:
SwitchListTile( value: true, onChanged: (value) { // Handle switch state changes }, title: Text('Enable Feature'), ),
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'), ),
title (Widget):
The primary title of the ListTile.
Example:
SwitchListTile( value: true, onChanged: (value) { // Handle switch state changes }, title: Text('Enable Feature'), ),
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'), ),
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), ),
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, ),
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.