From f02de9c086796c6dda9449b0cd3af43cda76ea4a Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Sat, 3 May 2025 02:25:21 +0200 Subject: [PATCH] Implemented new Stepper Button Widget --- lib/widgets/stepper.dart | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/widgets/stepper.dart diff --git a/lib/widgets/stepper.dart b/lib/widgets/stepper.dart new file mode 100644 index 0000000..879235e --- /dev/null +++ b/lib/widgets/stepper.dart @@ -0,0 +1,73 @@ +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 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 { + 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); + }); + } + } +}