pressure_settings_screen.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../services/bluetooth_service.dart';
  4. class PressureSettingsScreen extends ConsumerWidget {
  5. const PressureSettingsScreen({super.key});
  6. @override
  7. Widget build(BuildContext context, WidgetRef ref) {
  8. final machineState = ref.watch(bluetoothServiceProvider);
  9. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  10. return Scaffold(
  11. appBar: AppBar(title: const Text('Pressure Profiling')),
  12. body: ListView(
  13. padding: const EdgeInsets.all(16),
  14. children: [
  15. _PressureSlider(
  16. label: 'Start Pressure (%)',
  17. value: machineState.pressureStart.toDouble(),
  18. min: 0,
  19. max: 100,
  20. onChanged: (v) =>
  21. bluetoothService.updatePressureSettings(start: v.toInt()),
  22. ),
  23. _PressureSlider(
  24. label: 'End Pressure (%)',
  25. value: machineState.pressureEnd.toDouble(),
  26. min: 0,
  27. max: 100,
  28. onChanged: (v) =>
  29. bluetoothService.updatePressureSettings(end: v.toInt()),
  30. ),
  31. _PressureSlider(
  32. label: 'Period (s)',
  33. value: machineState.pressurePeriod.toDouble(),
  34. min: 0,
  35. max: 60,
  36. onChanged: (v) =>
  37. bluetoothService.updatePressureSettings(period: v.toInt()),
  38. ),
  39. const Divider(),
  40. const Padding(
  41. padding: EdgeInsets.all(16.0),
  42. child: Text(
  43. 'The pump will linearly transition from the start pressure to the end pressure over the specified period.',
  44. style: TextStyle(color: Colors.grey, fontSize: 14),
  45. textAlign: TextAlign.center,
  46. ),
  47. ),
  48. ],
  49. ),
  50. );
  51. }
  52. }
  53. /// Sends only on release; every `cmd set` rewrites controller EEPROM.
  54. class _PressureSlider extends StatefulWidget {
  55. final String label;
  56. final double value;
  57. final double min;
  58. final double max;
  59. final ValueChanged<double> onChanged;
  60. const _PressureSlider({
  61. required this.label,
  62. required this.value,
  63. required this.min,
  64. required this.max,
  65. required this.onChanged,
  66. });
  67. @override
  68. State<_PressureSlider> createState() => _PressureSliderState();
  69. }
  70. class _PressureSliderState extends State<_PressureSlider> {
  71. double? _draft;
  72. @override
  73. void didUpdateWidget(covariant _PressureSlider oldWidget) {
  74. super.didUpdateWidget(oldWidget);
  75. if (oldWidget.value != widget.value) _draft = null;
  76. }
  77. @override
  78. Widget build(BuildContext context) {
  79. final value = _draft ?? widget.value;
  80. return Column(
  81. crossAxisAlignment: CrossAxisAlignment.start,
  82. children: [
  83. Padding(
  84. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  85. child: Text(
  86. '${widget.label}: ${value.round()}${widget.label.contains('%') ? '%' : 's'}',
  87. ),
  88. ),
  89. Slider(
  90. value: value.clamp(widget.min, widget.max),
  91. min: widget.min,
  92. max: widget.max,
  93. divisions: (widget.max - widget.min).toInt(),
  94. onChanged: (v) => setState(() => _draft = v.roundToDouble()),
  95. onChangeEnd: (v) {
  96. setState(() => _draft = null);
  97. widget.onChanged(v.roundToDouble());
  98. },
  99. ),
  100. ],
  101. );
  102. }
  103. }