Skip to content

Commit 0f901a2

Browse files
Merge pull request #13 from boringdeveloper/0.1.x
Revised onTap() logic
2 parents 05bf72f + 205765d commit 0f901a2

2 files changed

Lines changed: 40 additions & 29 deletions

File tree

lib/flutter_switch.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ class _FlutterSwitchState extends State<FlutterSwitch>
210210
return GestureDetector(
211211
onTap: () {
212212
if (widget.value)
213-
_animationController.reverse();
214-
else
215213
_animationController.forward();
214+
else
215+
_animationController.reverse();
216216

217217
widget.onToggle(!widget.value);
218218
},

test/flutter_switch_test.dart

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ void main() {
1212
"displays the toggle indicator on the right if the given value is true",
1313
(WidgetTester tester) async {
1414
await tester.pumpWidget(
15-
_FlutterSwitchTestbed(
16-
value: true,
15+
_FlutterSwitchTest(
16+
status: true,
1717
),
1818
);
1919

@@ -27,8 +27,8 @@ void main() {
2727
"displays the toggle indicator on the left if the given value is false",
2828
(WidgetTester tester) async {
2929
await tester.pumpWidget(
30-
_FlutterSwitchTestbed(
31-
value: false,
30+
_FlutterSwitchTest(
31+
status: false,
3232
),
3333
);
3434

@@ -39,29 +39,29 @@ void main() {
3939
);
4040

4141
testWidgets(
42-
"changes the toggle indicator alignment to the right on tap when the given value is false",
42+
"changes the toggle indicator alignment to the right on tap when the initial value is false",
4343
(WidgetTester tester) async {
4444
await tester.pumpWidget(
45-
_FlutterSwitchTestbed(
46-
value: false,
45+
new _FlutterSwitchTest(
46+
status: false,
4747
),
4848
);
4949

5050
await tester.tap(find.byType(FlutterSwitch));
51-
await tester.pump();
51+
await tester.pumpAndSettle();
5252

5353
final align = tester.widget<Align>(alignToggleIndicatorFinder);
5454

55-
expect(align.alignment, equals(Alignment.centerLeft));
55+
expect(align.alignment, equals(Alignment.centerRight));
5656
},
5757
);
5858

5959
testWidgets(
60-
"changes the toggle indicator alignment to the left on tap when the given value is true",
60+
"changes the toggle indicator alignment to the left on tap when the initial value is true",
6161
(WidgetTester tester) async {
6262
await tester.pumpWidget(
63-
_FlutterSwitchTestbed(
64-
value: true,
63+
_FlutterSwitchTest(
64+
status: true,
6565
),
6666
);
6767

@@ -77,32 +77,43 @@ void main() {
7777
}
7878

7979
/// A testbed class needed to test the [FlutterSwitch] widget.
80-
class _FlutterSwitchTestbed extends StatelessWidget {
81-
/// A callback that is called when the switch is toggled.
82-
final ValueChanged<bool> onToggle;
83-
84-
/// An initial value of the switch.
85-
final bool value;
80+
class _FlutterSwitchTest extends StatefulWidget {
81+
final status;
8682

87-
/// Creates a new instance of this testbed.
88-
const _FlutterSwitchTestbed({
83+
_FlutterSwitchTest({
8984
Key key,
90-
this.onToggle = _defaultOnToggle,
91-
this.value,
85+
this.status,
9286
}) : super(key: key);
9387

88+
@override
89+
__FlutterSwitchTestState createState() => __FlutterSwitchTestState();
90+
}
91+
92+
class __FlutterSwitchTestState extends State<_FlutterSwitchTest> {
93+
bool _status = false;
94+
95+
@override
96+
void initState() {
97+
super.initState();
98+
99+
setState(() {
100+
_status = widget.status;
101+
});
102+
}
103+
94104
@override
95105
Widget build(BuildContext context) {
96106
return MaterialApp(
97107
home: Scaffold(
98108
body: FlutterSwitch(
99-
value: value,
100-
onToggle: onToggle,
109+
value: _status,
110+
onToggle: (val) {
111+
setState(() {
112+
_status = val;
113+
});
114+
},
101115
),
102116
),
103117
);
104118
}
105-
106-
/// A default on toggle callback of the switch.
107-
static void _defaultOnToggle(bool value) {}
108119
}

0 commit comments

Comments
 (0)