Skip to content

Commit bef1f8b

Browse files
authored
Merge pull request PramodJoshi#44 from itaishalom/master
If the value is canceled, onToggle should return null, not the index.
2 parents 1164364 + c6514af commit bef1f8b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

example/lib/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MyApp extends StatelessWidget {
2626
),
2727
ToggleSwitch(
2828
initialLabelIndex: 0,
29+
doubleTapDisable: true,
2930
totalSwitches: 3,
3031
labels: ['America', 'Canada', 'Mexico'],
3132
onToggle: (index) {
@@ -113,8 +114,9 @@ class MyApp extends StatelessWidget {
113114
ToggleSwitch(
114115
minWidth: 90.0,
115116
minHeight: 70.0,
116-
initialLabelIndex: 2,
117+
initialLabelIndex: null,
117118
cornerRadius: 20.0,
119+
doubleTapDisable: true,
118120
activeFgColor: Colors.white,
119121
inactiveBgColor: Colors.grey,
120122
inactiveFgColor: Colors.white,

lib/toggle_switch.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import 'package:flutter/material.dart';
44
import 'package:flutter/rendering.dart';
55

6-
typedef OnToggle = void Function(int index);
6+
typedef OnToggle = void Function(int? index);
77

88
// ignore: must_be_immutable
99
class ToggleSwitch extends StatefulWidget {
@@ -337,15 +337,21 @@ class _ToggleSwitchState extends State<ToggleSwitch>
337337

338338
/// Handles selection
339339
void _handleOnTap(int index) async {
340+
bool notifyNull = false;
340341
if (widget.changeOnTap) {
341342
if (widget.doubleTapDisable && widget.initialLabelIndex == index) {
342343
setState(() => widget.initialLabelIndex = null);
344+
notifyNull = true;
343345
} else {
344346
setState(() => widget.initialLabelIndex = index);
345347
}
346348
}
347349
if (widget.onToggle != null) {
348-
widget.onToggle!(index);
350+
if (notifyNull) {
351+
widget.onToggle!(null);
352+
} else {
353+
widget.onToggle!(index);
354+
}
349355
}
350356
}
351357

0 commit comments

Comments
 (0)