control_item_slider.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import 'package:flutter/material.dart';
  2. import 'package:flyinsono/architecture/utils/common_util.dart';
  3. import 'package:flyinsono/architecture/utils/prompt_box.dart';
  4. import 'package:flyinsono/lab/color/lab_colors.dart';
  5. // 控制面板-滑块控制器
  6. class ControlItemSlider extends StatefulWidget {
  7. const ControlItemSlider({
  8. super.key,
  9. required this.itemText,
  10. required this.minValue,
  11. required this.maxValue,
  12. required this.currentValue,
  13. this.onChanged,
  14. });
  15. final String itemText;
  16. final int minValue;
  17. final int maxValue;
  18. final int currentValue;
  19. final ValueChanged<int>? onChanged;
  20. @override
  21. State<ControlItemSlider> createState() => _ControlItemSliderState();
  22. }
  23. class _ControlItemSliderState extends State<ControlItemSlider> {
  24. late int _currentValue;
  25. @override
  26. void initState() {
  27. super.initState();
  28. _currentValue = widget.currentValue;
  29. }
  30. @override
  31. void didUpdateWidget(ControlItemSlider oldWidget) {
  32. super.didUpdateWidget(oldWidget);
  33. if (widget.currentValue != _currentValue) {
  34. _currentValue = widget.currentValue;
  35. }
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return Container(
  40. padding: EdgeInsets.only(top: 10),
  41. decoration: BoxDecoration(
  42. color: LabColors.base200,
  43. borderRadius: BorderRadius.circular(3),
  44. border: Border.all(
  45. color: LabColors.base400,
  46. width: 1,
  47. ),
  48. ),
  49. child: Column(
  50. crossAxisAlignment: CrossAxisAlignment.center,
  51. children: [
  52. Text(
  53. widget.itemText,
  54. style: TextStyle(
  55. fontSize: 14,
  56. height: 1,
  57. color: LabColors.text700,
  58. ),
  59. ),
  60. SizedBox(
  61. height: 2,
  62. ),
  63. Row(
  64. children: [
  65. Material(
  66. color: Colors.transparent,
  67. child: Container(
  68. margin: const EdgeInsets.only(left: 5),
  69. child: InkWell(
  70. borderRadius: BorderRadius.circular(3),
  71. hoverColor: LabColors.base400,
  72. onTap: () {
  73. if (_currentValue == widget.minValue) {
  74. PromptBox.toast('已达最小值!');
  75. return;
  76. }
  77. setState(() {
  78. _currentValue = (_currentValue - 1)
  79. .clamp(widget.minValue, widget.maxValue);
  80. });
  81. widget.onChanged?.call(_currentValue);
  82. },
  83. child: Padding(
  84. padding: const EdgeInsets.symmetric(vertical: 4),
  85. child: Icon(
  86. Icons.keyboard_arrow_left_rounded,
  87. color: LabColors.base800,
  88. size: 28,
  89. ),
  90. ),
  91. ),
  92. ),
  93. ),
  94. Expanded(
  95. child: SliderTheme(
  96. data: SliderThemeData(
  97. trackHeight: 8,
  98. thumbShape: const RoundSliderThumbShape(
  99. enabledThumbRadius: 12,
  100. elevation: 3,
  101. pressedElevation: 3,
  102. ),
  103. overlayShape: const RoundSliderOverlayShape(
  104. overlayRadius: 0,
  105. ),
  106. thumbColor: LabColors.base600,
  107. overlayColor: Colors.transparent,
  108. activeTrackColor: LabColors.base500,
  109. inactiveTrackColor: LabColors.base100,
  110. activeTickMarkColor: LabColors.base400,
  111. inactiveTickMarkColor: LabColors.base400,
  112. ),
  113. child: Slider(
  114. value: _currentValue.toDouble(),
  115. min: widget.minValue.toDouble(),
  116. max: widget.maxValue.toDouble(),
  117. divisions: widget.maxValue - widget.minValue,
  118. // activeColor: LabColors.base600,
  119. // inactiveColor: LabColors.base100,
  120. onChanged: (double value) {
  121. setState(() {
  122. _currentValue = value.round();
  123. });
  124. CommonUtil.debounce(
  125. () {
  126. widget.onChanged?.call(_currentValue);
  127. },
  128. durationTime: 800,
  129. );
  130. },
  131. ),
  132. ),
  133. ),
  134. Material(
  135. color: Colors.transparent,
  136. child: Container(
  137. margin: const EdgeInsets.only(right: 5),
  138. child: InkWell(
  139. borderRadius: BorderRadius.circular(3),
  140. hoverColor: LabColors.base400,
  141. onTap: () {
  142. if (_currentValue == widget.maxValue) {
  143. PromptBox.toast('已达最大值!');
  144. return;
  145. }
  146. setState(() {
  147. _currentValue = (_currentValue + 1)
  148. .clamp(widget.minValue, widget.maxValue);
  149. });
  150. widget.onChanged?.call(_currentValue);
  151. },
  152. child: Padding(
  153. padding: const EdgeInsets.symmetric(vertical: 4),
  154. child: Icon(
  155. Icons.keyboard_arrow_right_rounded,
  156. color: LabColors.base800,
  157. size: 28,
  158. ),
  159. ),
  160. ),
  161. ),
  162. ),
  163. ],
  164. ),
  165. SizedBox(
  166. height: 5,
  167. )
  168. ],
  169. ),
  170. );
  171. }
  172. }