Skip to content

Commit e5fb4c8

Browse files
Merge pull request #31 from boringdeveloper/0.2.x
null safety migration
2 parents ad0bcad + 9492718 commit e5fb4c8

5 files changed

Lines changed: 47 additions & 94 deletions

File tree

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ packages:
157157
source: hosted
158158
version: "2.1.0"
159159
sdks:
160-
dart: ">=2.12.0-0.0 <3.0.0"
160+
dart: ">=2.12.0 <3.0.0"

lib/flutter_switch.dart

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class FlutterSwitch extends StatefulWidget {
1212
///
1313
1414
const FlutterSwitch({
15-
Key key,
16-
@required this.value,
17-
@required this.onToggle,
15+
Key? key,
16+
required this.value,
17+
required this.onToggle,
1818
this.activeColor = Colors.blue,
1919
this.inactiveColor = Colors.grey,
2020
this.activeTextColor = Colors.white70,
2121
this.inactiveTextColor = Colors.white70,
22-
this.toggleColor,
22+
this.toggleColor = Colors.white,
2323
this.activeToggleColor,
2424
this.inactiveToggleColor,
2525
this.width = 70.0,
@@ -43,11 +43,6 @@ class FlutterSwitch extends StatefulWidget {
4343
this.inactiveIcon,
4444
this.duration = const Duration(milliseconds: 200),
4545
}) : assert(
46-
(toggleColor == null || activeToggleColor == null) &&
47-
(toggleColor == null || inactiveToggleColor == null),
48-
'Cannot provide toggleColor when an activeToggleColor or inactiveToggleColor was given\n'
49-
'To give the toggle a color, use "activeToggleColor: color" and "inactiveToggleColor: color".'),
50-
assert(
5146
(switchBorder == null || activeSwitchBorder == null) &&
5247
(switchBorder == null || inactiveSwitchBorder == null),
5348
'Cannot provide switchBorder when an activeSwitchBorder or inactiveSwitchBorder was given\n'
@@ -102,7 +97,7 @@ class FlutterSwitch extends StatefulWidget {
10297
///
10398
/// [activeTextColor] - The color to use on the text value when the switch is on.
10499
/// [activeTextFontWeight] - The font weight to use on the text value when the switch is on.
105-
final String activeText;
100+
final String? activeText;
106101

107102
/// The text to display when the switch is off.
108103
/// This parameter is only necessary when [showOnOff] property is true.
@@ -113,7 +108,7 @@ class FlutterSwitch extends StatefulWidget {
113108
///
114109
/// [inactiveTextColor] - The color to use on the text value when the switch is off.
115110
/// [inactiveTextFontWeight] - The font weight to use on the text value when the switch is off.
116-
final String inactiveText;
111+
final String? inactiveText;
117112

118113
/// The color to use on the switch when the switch is on.
119114
///
@@ -141,13 +136,13 @@ class FlutterSwitch extends StatefulWidget {
141136
/// This parameter is only necessary when [showOnOff] property is true.
142137
///
143138
/// Defaults to [FontWeight.w900].
144-
final FontWeight activeTextFontWeight;
139+
final FontWeight? activeTextFontWeight;
145140

146141
/// The font weight to use on the text value when the switch is off.
147142
/// This parameter is only necessary when [showOnOff] property is true.
148143
///
149144
/// Defaults to [FontWeight.w900].
150-
final FontWeight inactiveTextFontWeight;
145+
final FontWeight? inactiveTextFontWeight;
151146

152147
/// The color to use on the toggle of the switch.
153148
///
@@ -160,13 +155,13 @@ class FlutterSwitch extends StatefulWidget {
160155
///
161156
/// If [inactiveToggleColor] is used and this property is null. the value of
162157
/// [Colors.white] will be used.
163-
final Color activeToggleColor;
158+
final Color? activeToggleColor;
164159

165160
/// The color to use on the toggle of the switch when the given value is false.
166161
///
167162
/// If [activeToggleColor] is used and this property is null. the value of
168163
/// [Colors.white] will be used.
169-
final Color inactiveToggleColor;
164+
final Color? inactiveToggleColor;
170165

171166
/// The given width of the switch.
172167
///
@@ -204,44 +199,44 @@ class FlutterSwitch extends StatefulWidget {
204199
/// This property will give a uniform border to both states of the toggle
205200
///
206201
/// If the [activeSwitchBorder] or [inactiveSwitchBorder] is used, this property must be null.
207-
final BoxBorder switchBorder;
202+
final BoxBorder? switchBorder;
208203

209204
/// The border of the switch when the given value is true.
210205
///
211206
/// This property is optional.
212-
final BoxBorder activeSwitchBorder;
207+
final BoxBorder? activeSwitchBorder;
213208

214209
/// The border of the switch when the given value is false.
215210
///
216211
/// This property is optional.
217-
final BoxBorder inactiveSwitchBorder;
212+
final BoxBorder? inactiveSwitchBorder;
218213

219214
/// The border of the toggle.
220215
///
221216
/// This property will give a uniform border to both states of the toggle
222217
///
223218
/// If the [activeToggleBorder] or [inactiveToggleBorder] is used, this property must be null.
224-
final BoxBorder toggleBorder;
219+
final BoxBorder? toggleBorder;
225220

226221
/// The border of the toggle when the given value is true.
227222
///
228223
/// This property is optional.
229-
final BoxBorder activeToggleBorder;
224+
final BoxBorder? activeToggleBorder;
230225

231226
/// The border of the toggle when the given value is false.
232227
///
233228
/// This property is optional.
234-
final BoxBorder inactiveToggleBorder;
229+
final BoxBorder? inactiveToggleBorder;
235230

236231
/// The icon inside the toggle when the given value is true.
237232
///
238233
/// This property is optional.
239-
final Icon activeIcon;
234+
final Icon? activeIcon;
240235

241236
/// The icon inside the toggle when the given value is false.
242237
///
243238
/// This property is optional.
244-
final Icon inactiveIcon;
239+
final Icon? inactiveIcon;
245240

246241
/// The duration in milliseconds to change the state of the switch
247242
///
@@ -254,8 +249,8 @@ class FlutterSwitch extends StatefulWidget {
254249

255250
class _FlutterSwitchState extends State<FlutterSwitch>
256251
with SingleTickerProviderStateMixin {
257-
Animation _toggleAnimation;
258-
AnimationController _animationController;
252+
late final Animation _toggleAnimation;
253+
late final AnimationController _animationController;
259254

260255
@override
261256
void initState() {
@@ -298,19 +293,23 @@ class _FlutterSwitchState extends State<FlutterSwitch>
298293
Widget build(BuildContext context) {
299294
Color _toggleColor = Colors.white;
300295
Color _switchColor = Colors.white;
301-
Border _switchBorder;
302-
Border _toggleBorder;
296+
Border? _switchBorder;
297+
Border? _toggleBorder;
303298

304299
if (widget.value) {
305300
_toggleColor = widget.activeToggleColor ?? widget.toggleColor;
306301
_switchColor = widget.activeColor;
307-
_switchBorder = widget.activeSwitchBorder ?? widget.switchBorder;
308-
_toggleBorder = widget.activeToggleBorder ?? widget.toggleBorder;
302+
_switchBorder = widget.activeSwitchBorder as Border? ??
303+
widget.switchBorder as Border?;
304+
_toggleBorder = widget.activeToggleBorder as Border? ??
305+
widget.toggleBorder as Border?;
309306
} else {
310307
_toggleColor = widget.inactiveToggleColor ?? widget.toggleColor;
311308
_switchColor = widget.inactiveColor;
312-
_switchBorder = widget.inactiveSwitchBorder ?? widget.switchBorder;
313-
_toggleBorder = widget.inactiveToggleBorder ?? widget.toggleBorder;
309+
_switchBorder = widget.inactiveSwitchBorder as Border? ??
310+
widget.switchBorder as Border?;
311+
_toggleBorder = widget.inactiveToggleBorder as Border? ??
312+
widget.toggleBorder as Border?;
314313
}
315314

316315
double _textSpace = widget.width - widget.toggleSize;
@@ -370,7 +369,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>
370369
height: widget.toggleSize,
371370
decoration: BoxDecoration(
372371
shape: BoxShape.circle,
373-
color: _toggleColor ?? Colors.white,
372+
color: _toggleColor,
374373
border: _toggleBorder,
375374
),
376375
child: Container(
@@ -405,18 +404,15 @@ class _FlutterSwitchState extends State<FlutterSwitch>
405404
);
406405
}
407406

408-
FontWeight get _activeTextFontWeight => widget.activeTextFontWeight != null
409-
? widget.activeTextFontWeight
410-
: FontWeight.w900;
407+
FontWeight get _activeTextFontWeight =>
408+
widget.activeTextFontWeight ?? FontWeight.w900;
411409
FontWeight get _inactiveTextFontWeight =>
412-
widget.inactiveTextFontWeight != null
413-
? widget.inactiveTextFontWeight
414-
: FontWeight.w900;
410+
widget.inactiveTextFontWeight ?? FontWeight.w900;
415411

416412
Widget get _activeText {
417413
if (widget.showOnOff) {
418414
return Text(
419-
(widget?.activeText != null) ? widget.activeText : "On",
415+
widget.activeText ?? "On",
420416
style: TextStyle(
421417
color: widget.activeTextColor,
422418
fontWeight: _activeTextFontWeight,
@@ -431,7 +427,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>
431427
Widget get _inactiveText {
432428
if (widget.showOnOff) {
433429
return Text(
434-
(widget?.inactiveText != null) ? widget.inactiveText : "Off",
430+
widget.inactiveText ?? "Off",
435431
style: TextStyle(
436432
color: widget.inactiveTextColor,
437433
fontWeight: _inactiveTextFontWeight,

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ packages:
143143
source: hosted
144144
version: "2.1.0"
145145
sdks:
146-
dart: ">=2.12.0-0.0 <3.0.0"
146+
dart: ">=2.12.0 <3.0.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 0.2.2
44
homepage: https://github.com/boringdeveloper/FlutterSwitch
55

66
environment:
7-
sdk: ">=2.8.0 <3.0.0"
7+
sdk: '>=2.12.0 <3.0.0'
88

99
dependencies:
1010
flutter:

test/flutter_switch_test.dart

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -74,40 +74,6 @@ void main() {
7474
},
7575
);
7676

77-
testWidgets(
78-
"throws an assertion error when both toggleColor and activeToggleColor property is not null",
79-
(WidgetTester tester) async {
80-
await tester.pumpWidget(
81-
_FlutterSwitchTest(
82-
status: false,
83-
toggleColor: Colors.white,
84-
activeToggleColor: Colors.blue,
85-
),
86-
);
87-
88-
final assertion = tester.takeException();
89-
90-
expect(assertion, isAssertionError);
91-
},
92-
);
93-
94-
testWidgets(
95-
"throws an assertion error when both toggleColor and inactiveToggleColor property is not null",
96-
(WidgetTester tester) async {
97-
await tester.pumpWidget(
98-
_FlutterSwitchTest(
99-
status: false,
100-
toggleColor: Colors.blue,
101-
inactiveToggleColor: Colors.blueGrey,
102-
),
103-
);
104-
105-
final assertion = tester.takeException();
106-
107-
expect(assertion, isAssertionError);
108-
},
109-
);
110-
11177
testWidgets(
11278
"throws an assertion error when both switchBorder and activeSwitchBorder property is not null",
11379
(WidgetTester tester) async {
@@ -205,22 +171,16 @@ void main() {
205171
/// A testbed class needed to test the [FlutterSwitch] widget.
206172
class _FlutterSwitchTest extends StatefulWidget {
207173
final bool status;
208-
final Color toggleColor;
209-
final Color activeToggleColor;
210-
final Color inactiveToggleColor;
211-
final Border switchBorder;
212-
final Border activeSwitchBorder;
213-
final Border inactiveSwitchBorder;
214-
final Border toggleBorder;
215-
final Border activeToggleBorder;
216-
final Border inactiveToggleBorder;
174+
final Border? switchBorder;
175+
final Border? activeSwitchBorder;
176+
final Border? inactiveSwitchBorder;
177+
final Border? toggleBorder;
178+
final Border? activeToggleBorder;
179+
final Border? inactiveToggleBorder;
217180

218181
_FlutterSwitchTest({
219-
Key key,
220-
this.status,
221-
this.toggleColor,
222-
this.activeToggleColor,
223-
this.inactiveToggleColor,
182+
Key? key,
183+
required this.status,
224184
this.switchBorder,
225185
this.activeSwitchBorder,
226186
this.inactiveSwitchBorder,
@@ -251,9 +211,6 @@ class __FlutterSwitchTestState extends State<_FlutterSwitchTest> {
251211
home: Scaffold(
252212
body: FlutterSwitch(
253213
value: _status,
254-
toggleColor: widget.toggleColor,
255-
activeToggleColor: widget.activeToggleColor,
256-
inactiveToggleColor: widget.inactiveToggleColor,
257214
switchBorder: widget.switchBorder,
258215
activeSwitchBorder: widget.activeSwitchBorder,
259216
inactiveSwitchBorder: widget.inactiveSwitchBorder,

0 commit comments

Comments
 (0)