Skip to content

Instantly share code, notes, and snippets.

@nglauber
Last active April 30, 2020 19:42
Show Gist options
  • Save nglauber/55b7b1a64e9f839b8b3c1ee2b02f91fc to your computer and use it in GitHub Desktop.
Save nglauber/55b7b1a64e9f839b8b3c1ee2b02f91fc to your computer and use it in GitHub Desktop.
class StepCounter extends StatefulWidget {
int value;
final int minValue;
final int maxValue;
final Function(int) onValueChanged;
StepCounter(
{@required this.value,
@required this.onValueChanged,
this.minValue = 0,
this.maxValue = 10}) {
if (minValue > maxValue) {
throw Exception(
'Invalid range. Min value ($minValue) must be less than max value ($maxValue)');
}
if (value > maxValue || value < minValue) {
throw Exception(
'Invalid value. The value must be between $minValue and $maxValue');
}
}
@override
_StepCounterState createState() => _StepCounterState();
}
class _StepCounterState extends State<StepCounter> {
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
StepButton(Icons.remove, () {
if (widget.value > widget.minValue) {
setState(() {
widget.value--;
widget.onValueChanged(widget.value);
});
}
}),
Container(
width: 60,
child: Container(
padding: EdgeInsets.all(8),
child: Text(
widget.value.toString(),
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
)),
),
StepButton(Icons.add, () {
if (widget.value < widget.maxValue) {
setState(() {
widget.value++;
widget.onValueChanged(widget.value);
});
}
}),
],
);
}
Widget StepButton(IconData icon, Function f) {
return GestureDetector(
child: Container(
decoration: BoxDecoration(color: Colors.blue),
padding: EdgeInsets.all(8),
child: Icon(
icon,
color: Colors.white,
)),
onTap: () {
f();
},
);
}
}
import 'package:first_app/stepper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Test step counter', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: StepCounter(
value: 1,
onValueChanged: (_) {},
),
));
// Verify that our counter starts at 0.
expect(find.text('1'), findsOneWidget);
expect(find.text('0'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('1'), findsNothing);
expect(find.text('2'), findsOneWidget);
});
testWidgets('Test min value', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: StepCounter(
value: 1,
minValue: 1,
onValueChanged: (_) {},
),
));
// Verify that our counter starts at 1.
expect(find.text('1'), findsOneWidget);
expect(find.text('0'), findsNothing);
// Tap the '-' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.remove));
await tester.pump();
// Verify that our counter has decremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
testWidgets('Test max value', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: StepCounter(
value: 10,
maxValue: 10,
onValueChanged: (_) {},
),
));
// Verify that our counter starts at 10.
expect(find.text('10'), findsOneWidget);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has not incremented.
expect(find.text('11'), findsNothing);
expect(find.text('10'), findsOneWidget);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment