@@ -3,35 +3,161 @@ library flutter_switch;
33import 'package:flutter/material.dart' ;
44
55class FlutterSwitch extends StatefulWidget {
6- final bool value, showOnOff;
6+ /// Creates a material design switch.
7+ ///
8+ /// The following arguments are required:
9+ ///
10+ /// * [value] determines whether this switch is on or off.
11+ /// * [onToggle] is called when the user toggles the switch on or off.
12+ ///
13+
14+ const FlutterSwitch ({
15+ Key key,
16+ @required this .value,
17+ @required this .onToggle,
18+ this .activeColor = Colors .blue,
19+ this .inactiveColor = Colors .grey,
20+ this .activeTextColor = Colors .white70,
21+ this .inactiveTextColor = Colors .white70,
22+ this .toggleColor = Colors .white,
23+ this .width = 70.0 ,
24+ this .height = 35.0 ,
25+ this .toggleSize = 25.0 ,
26+ this .valueFontSize = 16.0 ,
27+ this .borderRadius = 20.0 ,
28+ this .padding = 4.0 ,
29+ this .showOnOff = false ,
30+ this .activeText,
31+ this .inactiveText,
32+ this .activeTextFontWeight,
33+ this .inactiveTextFontWeight,
34+ }) : super (key: key);
35+
36+ /// Determines if the switch is on or off.
37+ ///
38+ /// This property is required.
39+ final bool value;
40+
41+ /// Called when the user toggles the switch.
42+ ///
43+ /// This property is required.
44+ ///
45+ /// [onToggle] should update the state of the parent [StatefulWidget]
46+ /// using the [setState] method, so that the parent gets rebuilt; for example:
47+ ///
48+ /// ```dart
49+ /// FlutterSwitch(
50+ /// value: _status,
51+ /// width: 110,
52+ /// borderRadius: 30.0,
53+ /// onToggle: (val) {
54+ /// setState(() {
55+ /// _status = val;
56+ /// });
57+ /// },
58+ /// ),
59+ /// ```
760 final ValueChanged <bool > onToggle;
8- final Color activeColor,
9- inactiveColor,
10- activeTextColor,
11- inactiveTextColor,
12- toggleColor;
13- final double width, height, toggleSize, valueFontSize, borderRadius, padding;
14- final String activeText, inactiveText;
15-
16- const FlutterSwitch (
17- {Key key,
18- this .value,
19- this .onToggle,
20- this .activeColor = Colors .blue,
21- this .inactiveColor = Colors .grey,
22- this .activeTextColor = Colors .white70,
23- this .inactiveTextColor = Colors .white70,
24- this .toggleColor = Colors .white,
25- this .width = 70.0 ,
26- this .height = 35.0 ,
27- this .toggleSize = 25.0 ,
28- this .valueFontSize = 16.0 ,
29- this .borderRadius = 20.0 ,
30- this .padding = 4.0 ,
31- this .showOnOff = false ,
32- this .activeText,
33- this .inactiveText})
34- : super (key: key);
61+
62+ /// Displays an on or off text.
63+ ///
64+ /// Text value can be override by the [activeText] and
65+ /// [inactiveText] properties.
66+ ///
67+ /// Defaults to 'false' if no value was given.
68+ final bool showOnOff;
69+
70+ /// The text to display when the switch is on.
71+ /// This parameter is only necessary when [showOnOff] property is true.
72+ ///
73+ /// Defaults to 'On' if no value was given.
74+ ///
75+ /// To change value style, the following properties are available
76+ ///
77+ /// [activeTextColor] - The color to use on the text value when the switch is on.
78+ /// [activeTextFontWeight] - The font weight to use on the text value when the switch is on.
79+ final String activeText;
80+
81+ /// The text to display when the switch is off.
82+ /// This parameter is only necessary when [showOnOff] property is true.
83+ ///
84+ /// Defaults to 'Off' if no value was given.
85+ ///
86+ /// To change value style, the following properties are available
87+ ///
88+ /// [inactiveTextColor] - The color to use on the text value when the switch is off.
89+ /// [inactiveTextFontWeight] - The font weight to use on the text value when the switch is off.
90+ final String inactiveText;
91+
92+ /// The color to use on the switch when the switch is on.
93+ ///
94+ /// Defaults to [Colors.blue] .
95+ final Color activeColor;
96+
97+ /// The color to use on the switch when the switch is off.
98+ ///
99+ /// Defaults to [Colors.grey] .
100+ final Color inactiveColor;
101+
102+ /// The color to use on the text value when the switch is on.
103+ /// This parameter is only necessary when [showOnOff] property is true.
104+ ///
105+ /// Defaults to [Colors.white70] .
106+ final Color activeTextColor;
107+
108+ /// The color to use on the text value when the switch is off.
109+ /// This parameter is only necessary when [showOnOff] property is true.
110+ ///
111+ /// Defaults to [Colors.white70] .
112+ final Color inactiveTextColor;
113+
114+ /// The font weight to use on the text value when the switch is on.
115+ /// This parameter is only necessary when [showOnOff] property is true.
116+ ///
117+ /// Defaults to [FontWeight.w900] .
118+ final FontWeight activeTextFontWeight;
119+
120+ /// The font weight to use on the text value when the switch is off.
121+ /// This parameter is only necessary when [showOnOff] property is true.
122+ ///
123+ /// Defaults to [FontWeight.w900] .
124+ final FontWeight inactiveTextFontWeight;
125+
126+ /// The color to use on the toggle of the switch.
127+ ///
128+ /// Defaults to [Colors.white] .
129+ final Color toggleColor;
130+
131+ /// The given width of the switch.
132+ ///
133+ /// Defaults to a width of 70.0.
134+ final double width;
135+
136+ /// The given height of the switch.
137+ ///
138+ /// Defaults to a height of 35.0.
139+ final double height;
140+
141+ /// The size of the toggle of the switch.
142+ ///
143+ /// Defaults to a size of 25.0.
144+ final double toggleSize;
145+
146+ /// The font size of the values of the switch.
147+ /// This parameter is only necessary when [showOnOff] property is true.
148+ ///
149+ /// Defaults to a size of 16.0.
150+ final double valueFontSize;
151+
152+ /// The border radius of the switch.
153+ ///
154+ /// Defaults to the value of 20.0.
155+ final double borderRadius;
156+
157+ /// The padding of the switch.
158+ ///
159+ /// Defaults to the value of 4.0.
160+ final double padding;
35161
36162 @override
37163 _FlutterSwitchState createState () => _FlutterSwitchState ();
@@ -135,13 +261,21 @@ class _FlutterSwitchState extends State<FlutterSwitch>
135261 );
136262 }
137263
264+ FontWeight get _activeTextFontWeight => widget.activeTextFontWeight != null
265+ ? widget.activeTextFontWeight
266+ : FontWeight .w900;
267+ FontWeight get _inactiveTextFontWeight =>
268+ widget.inactiveTextFontWeight != null
269+ ? widget.inactiveTextFontWeight
270+ : FontWeight .w900;
271+
138272 Widget get _activeText {
139273 if (widget.showOnOff) {
140274 return Text (
141275 (widget? .activeText != null ) ? widget.activeText : "On" ,
142276 style: TextStyle (
143277 color: widget.activeTextColor,
144- fontWeight: FontWeight .w900 ,
278+ fontWeight: _activeTextFontWeight ,
145279 fontSize: widget.valueFontSize,
146280 ),
147281 );
@@ -156,9 +290,10 @@ class _FlutterSwitchState extends State<FlutterSwitch>
156290 (widget? .inactiveText != null ) ? widget.inactiveText : "Off" ,
157291 style: TextStyle (
158292 color: widget.inactiveTextColor,
159- fontWeight: FontWeight .w900 ,
293+ fontWeight: _inactiveTextFontWeight ,
160294 fontSize: widget.valueFontSize,
161295 ),
296+ textAlign: TextAlign .right,
162297 );
163298 }
164299
0 commit comments