Skip to content

Commit e9e7fcf

Browse files
committed
added comments and font weight property
1 parent 5c8a494 commit e9e7fcf

3 files changed

Lines changed: 189 additions & 89 deletions

File tree

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ packages:
6868
path: ".."
6969
relative: true
7070
source: path
71-
version: "0.1.2"
71+
version: "0.1.1"
7272
flutter_test:
7373
dependency: "direct dev"
7474
description: flutter

lib/flutter_switch.dart

Lines changed: 165 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,161 @@ library flutter_switch;
33
import 'package:flutter/material.dart';
44

55
class 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

pubspec.lock

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,55 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4-
archive:
5-
dependency: transitive
6-
description:
7-
name: archive
8-
url: "https://pub.dartlang.org"
9-
source: hosted
10-
version: "2.0.13"
11-
args:
12-
dependency: transitive
13-
description:
14-
name: args
15-
url: "https://pub.dartlang.org"
16-
source: hosted
17-
version: "1.6.0"
184
async:
195
dependency: transitive
206
description:
217
name: async
228
url: "https://pub.dartlang.org"
239
source: hosted
24-
version: "2.4.1"
10+
version: "2.4.2"
2511
boolean_selector:
2612
dependency: transitive
2713
description:
2814
name: boolean_selector
2915
url: "https://pub.dartlang.org"
3016
source: hosted
3117
version: "2.0.0"
18+
characters:
19+
dependency: transitive
20+
description:
21+
name: characters
22+
url: "https://pub.dartlang.org"
23+
source: hosted
24+
version: "1.0.0"
3225
charcode:
3326
dependency: transitive
3427
description:
3528
name: charcode
3629
url: "https://pub.dartlang.org"
3730
source: hosted
3831
version: "1.1.3"
39-
collection:
32+
clock:
4033
dependency: transitive
4134
description:
42-
name: collection
35+
name: clock
4336
url: "https://pub.dartlang.org"
4437
source: hosted
45-
version: "1.14.12"
46-
convert:
38+
version: "1.0.1"
39+
collection:
4740
dependency: transitive
4841
description:
49-
name: convert
42+
name: collection
5043
url: "https://pub.dartlang.org"
5144
source: hosted
52-
version: "2.1.1"
53-
crypto:
45+
version: "1.14.13"
46+
fake_async:
5447
dependency: transitive
5548
description:
56-
name: crypto
49+
name: fake_async
5750
url: "https://pub.dartlang.org"
5851
source: hosted
59-
version: "2.1.4"
52+
version: "1.1.0"
6053
flutter:
6154
dependency: "direct main"
6255
description: flutter
@@ -67,20 +60,13 @@ packages:
6760
description: flutter
6861
source: sdk
6962
version: "0.0.0"
70-
image:
71-
dependency: transitive
72-
description:
73-
name: image
74-
url: "https://pub.dartlang.org"
75-
source: hosted
76-
version: "2.1.12"
7763
matcher:
7864
dependency: transitive
7965
description:
8066
name: matcher
8167
url: "https://pub.dartlang.org"
8268
source: hosted
83-
version: "0.12.6"
69+
version: "0.12.8"
8470
meta:
8571
dependency: transitive
8672
description:
@@ -94,21 +80,7 @@ packages:
9480
name: path
9581
url: "https://pub.dartlang.org"
9682
source: hosted
97-
version: "1.6.4"
98-
petitparser:
99-
dependency: transitive
100-
description:
101-
name: petitparser
102-
url: "https://pub.dartlang.org"
103-
source: hosted
104-
version: "2.4.0"
105-
quiver:
106-
dependency: transitive
107-
description:
108-
name: quiver
109-
url: "https://pub.dartlang.org"
110-
source: hosted
111-
version: "2.1.3"
83+
version: "1.7.0"
11284
sky_engine:
11385
dependency: transitive
11486
description: flutter
@@ -127,7 +99,7 @@ packages:
12799
name: stack_trace
128100
url: "https://pub.dartlang.org"
129101
source: hosted
130-
version: "1.9.3"
102+
version: "1.9.5"
131103
stream_channel:
132104
dependency: transitive
133105
description:
@@ -155,27 +127,20 @@ packages:
155127
name: test_api
156128
url: "https://pub.dartlang.org"
157129
source: hosted
158-
version: "0.2.15"
130+
version: "0.2.17"
159131
typed_data:
160132
dependency: transitive
161133
description:
162134
name: typed_data
163135
url: "https://pub.dartlang.org"
164136
source: hosted
165-
version: "1.1.6"
137+
version: "1.2.0"
166138
vector_math:
167139
dependency: transitive
168140
description:
169141
name: vector_math
170142
url: "https://pub.dartlang.org"
171143
source: hosted
172144
version: "2.0.8"
173-
xml:
174-
dependency: transitive
175-
description:
176-
name: xml
177-
url: "https://pub.dartlang.org"
178-
source: hosted
179-
version: "3.6.1"
180145
sdks:
181-
dart: ">=2.6.0 <3.0.0"
146+
dart: ">=2.9.0-14.0.dev <3.0.0"

0 commit comments

Comments
 (0)