Created
December 3, 2020 18:09
-
-
Save dhruvilp/fde34798be8ec2b96881ce28c4fd1da5 to your computer and use it in GitHub Desktop.
SwitchListTile Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Flutter code sample for SwitchListTile | |
| //  | |
| // | |
| // This widget shows a switch that, when toggled, changes the state of a [bool] | |
| // member field called `_lights`. | |
| import 'package:flutter/material.dart'; | |
| void main() => runApp(MyApp()); | |
| /// This is the main application widget. | |
| class MyApp extends StatelessWidget { | |
| static const String _title = 'Flutter Code Sample'; | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: _title, | |
| home: MyStatefulWidget(), | |
| ); | |
| } | |
| } | |
| /// This is the stateful widget that the main application instantiates. | |
| class MyStatefulWidget extends StatefulWidget { | |
| MyStatefulWidget({Key key}) : super(key: key); | |
| @override | |
| _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); | |
| } | |
| /// This is the private State class that goes with MyStatefulWidget. | |
| class _MyStatefulWidgetState extends State<MyStatefulWidget> { | |
| var _lights = false; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| backgroundColor: !_lights ? Colors.white : Colors.grey.shade900, | |
| appBar: AppBar(title: const Text(MyApp._title)), | |
| body: Center( | |
| child: Padding( | |
| padding: const EdgeInsets.symmetric(horizontal: 15.0), | |
| child: SwitchListTile.adaptive( | |
| contentPadding: EdgeInsets.all(10.0), | |
| title: Text( | |
| 'Lights', | |
| style: TextStyle(color: _lights ? Colors.white : Colors.grey.shade700), | |
| ), | |
| value: _lights, | |
| onChanged: (bool value){ | |
| setState(() { | |
| _lights = value; | |
| }); | |
| }, | |
| secondary: Icon( | |
| Icons.lightbulb_outline, | |
| color: _lights ? Colors.white : Colors.grey.shade700, | |
| ), | |
| inactiveThumbImage: NetworkImage( 'https://images.vexels.com/media/users/3/145131/isolated/preview/d2ba09d9b4856df5b15cdc5636a45b37-sun-large-wavy-beams-icon-by-vexels.png', | |
| ), | |
| activeThumbImage: NetworkImage( | |
| 'https://i.pinimg.com/564x/1c/22/43/1c22433b8d3aec799fd8187ca3030193.jpg', | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment