|
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +class CupertinoSwitchListTile extends StatelessWidget { |
| 5 | + /// This has been shamelessly copied from Material/SwitchListTile. |
| 6 | + /// The applicable license applies. |
| 7 | + /// source: StackOverflow |
| 8 | + const CupertinoSwitchListTile({ |
| 9 | + Key key, |
| 10 | + @required this.value, |
| 11 | + @required this.onChanged, |
| 12 | + this.activeColor, |
| 13 | + this.title, |
| 14 | + this.subtitle, |
| 15 | + this.isThreeLine: false, |
| 16 | + this.dense, |
| 17 | + this.secondary, |
| 18 | + this.selected: false, |
| 19 | + }) : assert(value != null), |
| 20 | + assert(isThreeLine != null), |
| 21 | + assert(!isThreeLine || subtitle != null), |
| 22 | + assert(selected != null), |
| 23 | + super(key: key); |
| 24 | + |
| 25 | + /// Whether this switch is checked. |
| 26 | + /// |
| 27 | + /// This property must not be null. |
| 28 | + final bool value; |
| 29 | + |
| 30 | + /// Called when the user toggles the switch on or off. |
| 31 | + /// |
| 32 | + /// The switch passes the new value to the callback but does not actually |
| 33 | + /// change state until the parent widget rebuilds the switch tile with the |
| 34 | + /// new value. |
| 35 | + /// |
| 36 | + /// If null, the switch will be displayed as disabled. |
| 37 | + /// |
| 38 | + /// The callback provided to [onChanged] should update the state of the parent |
| 39 | + /// [StatefulWidget] using the [State.setState] method, so that the parent |
| 40 | + /// gets rebuilt; for example: |
| 41 | + /// |
| 42 | + /// ```dart |
| 43 | + /// new SwitchListTile( |
| 44 | + /// value: _lights, |
| 45 | + /// onChanged: (bool newValue) { |
| 46 | + /// setState(() { |
| 47 | + /// _lights = newValue; |
| 48 | + /// }); |
| 49 | + /// }, |
| 50 | + /// title: new Text('Lights'), |
| 51 | + /// ) |
| 52 | + /// ``` |
| 53 | + final ValueChanged<bool> onChanged; |
| 54 | + |
| 55 | + /// The color to use when this switch is on. |
| 56 | + /// |
| 57 | + /// Defaults to accent color of the current [Theme]. |
| 58 | + final Color activeColor; |
| 59 | + |
| 60 | + /// The primary content of the list tile. |
| 61 | + /// |
| 62 | + /// Typically a [Text] widget. |
| 63 | + final Widget title; |
| 64 | + |
| 65 | + /// Additional content displayed below the title. |
| 66 | + /// |
| 67 | + /// Typically a [Text] widget. |
| 68 | + final Widget subtitle; |
| 69 | + |
| 70 | + /// A widget to display on the opposite side of the tile from the switch. |
| 71 | + /// |
| 72 | + /// Typically an [Icon] widget. |
| 73 | + final Widget secondary; |
| 74 | + |
| 75 | + /// Whether this list tile is intended to display three lines of text. |
| 76 | + /// |
| 77 | + /// If false, the list tile is treated as having one line if the subtitle is |
| 78 | + /// null and treated as having two lines if the subtitle is non-null. |
| 79 | + final bool isThreeLine; |
| 80 | + |
| 81 | + /// Whether this list tile is part of a vertically dense list. |
| 82 | + /// |
| 83 | + /// If this property is null then its value is based on [ListTileTheme.dense]. |
| 84 | + final bool dense; |
| 85 | + |
| 86 | + /// Whether to render icons and text in the [activeColor]. |
| 87 | + /// |
| 88 | + /// No effort is made to automatically coordinate the [selected] state and the |
| 89 | + /// [value] state. To have the list tile appear selected when the switch is |
| 90 | + /// on, pass the same value to both. |
| 91 | + /// |
| 92 | + /// Normally, this property is left to its default value, false. |
| 93 | + final bool selected; |
| 94 | + |
| 95 | + @override |
| 96 | + Widget build(BuildContext context) { |
| 97 | + var color = activeColor ?? Theme.of(context).accentColor; |
| 98 | + print("Active color: ${color.red} ${color.green} ${color.blue}"); |
| 99 | + final Widget control = new CupertinoSwitch( |
| 100 | + value: value, |
| 101 | + onChanged: onChanged, |
| 102 | + activeColor: activeColor ?? CupertinoColors.activeGreen, |
| 103 | + ); |
| 104 | + return new MergeSemantics( |
| 105 | + child: ListTileTheme.merge( |
| 106 | + selectedColor: activeColor ?? CupertinoColors.activeGreen, |
| 107 | + child: new ListTile( |
| 108 | + leading: secondary, |
| 109 | + title: title, |
| 110 | + subtitle: subtitle, |
| 111 | + trailing: control, |
| 112 | + isThreeLine: isThreeLine, |
| 113 | + dense: dense, |
| 114 | + enabled: onChanged != null, |
| 115 | + onTap: onChanged != null |
| 116 | + ? () { |
| 117 | + onChanged(!value); |
| 118 | + } |
| 119 | + : null, |
| 120 | + selected: selected, |
| 121 | + ), |
| 122 | + ), |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments