74 lines
2.2 KiB
Dart
74 lines
2.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../constants.dart';
|
|
|
|
class NumberStepperField extends StatelessWidget {
|
|
final String label;
|
|
final num? value;
|
|
final num min;
|
|
final num max;
|
|
final num step;
|
|
final String? unit;
|
|
final ValueChanged<num> onChanged;
|
|
|
|
const NumberStepperField({
|
|
Key? key,
|
|
required this.label,
|
|
required this.value,
|
|
this.min = 0,
|
|
this.max = 999999,
|
|
this.step = 1,
|
|
this.unit,
|
|
required this.onChanged,
|
|
}) : super(key: key);
|
|
|
|
num get _current => (value ?? min).clamp(min, max);
|
|
|
|
String _format(num n) => n.truncateToDouble() == n ? n.toInt().toString() : n.toString();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final current = _current;
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
Text(label, style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500)),
|
|
const SizedBox(width: 12),
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: kSecond),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.remove_circle_outline),
|
|
color: current <= min ? Colors.grey[400] : kPrimaryColor,
|
|
onPressed: current <= min
|
|
? null
|
|
: () => onChanged((current - step).clamp(min, max)),
|
|
),
|
|
SizedBox(
|
|
width: 56,
|
|
child: Text(
|
|
unit != null ? "${_format(current)} $unit" : _format(current),
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
|
|
),
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.add_circle_outline),
|
|
color: current >= max ? Colors.grey[400] : kPrimaryColor,
|
|
onPressed: current >= max
|
|
? null
|
|
: () => onChanged((current + step).clamp(min, max)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|