Files
cabo-counter/lib/widgets/stepper.dart
2025-05-03 02:25:21 +02:00

74 lines
1.7 KiB
Dart

import 'package:flutter/cupertino.dart'; // Für iOS-Style
class Stepper extends StatefulWidget {
final int minValue;
final int maxValue;
final int? initialValue;
final int step;
final ValueChanged<int> onChanged;
const Stepper({
super.key,
required this.minValue,
required this.maxValue,
required this.step,
required this.onChanged,
this.initialValue,
});
@override
// ignore: library_private_types_in_public_api
_StepperState createState() => _StepperState();
}
class _StepperState extends State<Stepper> {
late int _value;
@override
void initState() {
super.initState();
final start = widget.initialValue ?? widget.minValue;
_value = start.clamp(widget.minValue, widget.maxValue);
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
CupertinoButton(
padding: const EdgeInsets.all(8),
onPressed: _decrement,
child: const Icon(CupertinoIcons.minus),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: Text('$_value', style: const TextStyle(fontSize: 18)),
),
CupertinoButton(
padding: const EdgeInsets.all(8),
onPressed: _increment,
child: const Icon(CupertinoIcons.add),
),
],
);
}
void _increment() {
if (_value + widget.step <= widget.maxValue) {
setState(() {
_value += widget.step;
widget.onChanged.call(_value);
});
}
}
void _decrement() {
if (_value - widget.step >= widget.minValue) {
setState(() {
_value -= widget.step;
widget.onChanged.call(_value);
});
}
}
}