|
| 1 | +import 'package:dplanner/widgets/color_unit_widget.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter/services.dart'; |
| 4 | + |
| 5 | +import '../const/style.dart'; |
| 6 | + |
| 7 | +class ColorScrollWidget extends StatefulWidget { |
| 8 | + final Color defaultColor; |
| 9 | + final List<Color> availableColors; |
| 10 | + final ValueChanged<Color> onColorChanged; // 색상이 변경될 때 호출될 콜백 |
| 11 | + |
| 12 | + const ColorScrollWidget({ |
| 13 | + super.key, |
| 14 | + required this.defaultColor, |
| 15 | + required this.availableColors, |
| 16 | + required this.onColorChanged, // 초기 색상 설정은 필요 없으므로 제거 |
| 17 | + }); |
| 18 | + |
| 19 | + @override |
| 20 | + State<ColorScrollWidget> createState() => _ColorScrollWidgetState(); |
| 21 | +} |
| 22 | + |
| 23 | +class _ColorScrollWidgetState extends State<ColorScrollWidget> { |
| 24 | + static const double circleSize = ColorUnitWidget.circleSize; |
| 25 | + static const double circlePadding = 16.0; |
| 26 | + static const double indicatorWidth = 180.0; // 5개의 원이 보여질 정도의 가로 길이 |
| 27 | + static const double scrollBarHeight = 4.0; // 스크롤바 높이 |
| 28 | + static const Duration animationDuration = Duration(milliseconds: 300); // 스크롤 애니메이션 지속 시간 |
| 29 | + |
| 30 | + List<Color> colors = []; // 실제로 사용할 색상 리스트 |
| 31 | + |
| 32 | + final ScrollController _scrollController = ScrollController(); |
| 33 | + int selectedIndex = 2; // 초기 선택된 색상 인덱스 |
| 34 | + |
| 35 | + @override |
| 36 | + void initState() { |
| 37 | + super.initState(); |
| 38 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 39 | + setState(() { |
| 40 | + colors.addAll([ |
| 41 | + Colors.transparent, |
| 42 | + Colors.transparent, |
| 43 | + ...widget.availableColors, |
| 44 | + Colors.transparent, |
| 45 | + Colors.transparent]); |
| 46 | + }); |
| 47 | + selectedIndex = _getColorIndex(widget.defaultColor); // 기본 색상 인덱스 설정 |
| 48 | + _centerScrollOnSelected(); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @override |
| 53 | + void dispose() { |
| 54 | + _scrollController.dispose(); |
| 55 | + super.dispose(); |
| 56 | + } |
| 57 | + |
| 58 | + int _getColorIndex(Color color) { |
| 59 | + // 기본 색상에 대한 인덱스를 반환합니다. |
| 60 | + int index = colors.indexOf(color); |
| 61 | + if (index == -1) { |
| 62 | + // 색상이 colors 목록에 없으면 기본 색상 인덱스를 설정하지 않습니다. |
| 63 | + return 2; // 기본 색상 인덱스 |
| 64 | + } |
| 65 | + return index; |
| 66 | + } |
| 67 | + |
| 68 | + void _centerScrollOnSelected() { |
| 69 | + // 선택된 색상이 화면 중앙에 오도록 스크롤 위치를 설정합니다. |
| 70 | + double targetOffset = selectedIndex * (circleSize + circlePadding) - (indicatorWidth / 2) + circleSize / 2; |
| 71 | + _scrollController.jumpTo(targetOffset); |
| 72 | + } |
| 73 | + |
| 74 | + void _onScroll() { |
| 75 | + // 스크롤 위치를 기반으로 인디케이터의 중심에 가장 가까운 색상 인덱스를 계산합니다. |
| 76 | + double offset = _scrollController.offset + (indicatorWidth / 2); |
| 77 | + int newIndex = (offset / (circleSize + circlePadding)).round(); |
| 78 | + |
| 79 | + // 색상이 인디케이터의 중심에 가장 가깝도록 선택합니다. |
| 80 | + double centerOffset = (circleSize + circlePadding) * newIndex; |
| 81 | + double indicatorCenter = _scrollController.offset + (indicatorWidth / 2); |
| 82 | + double colorCenter = centerOffset + circleSize / 2; |
| 83 | + |
| 84 | + if ((indicatorCenter - colorCenter).abs() < (circleSize + circlePadding) / 2) { |
| 85 | + // 색상 인덱스를 실제 색상 범위로 제한합니다. |
| 86 | + if (newIndex < 2) newIndex = 2; // 첫 번째 색상 (두 번째 인덱스)으로 제한 |
| 87 | + if (newIndex >= colors.length - 2) newIndex = colors.length - 3; // 마지막 색상 (마지막에서 두 번째 인덱스)으로 제한 |
| 88 | + |
| 89 | + if (newIndex != selectedIndex && newIndex >= 0 && newIndex < colors.length) { |
| 90 | + setState(() { |
| 91 | + selectedIndex = newIndex; |
| 92 | + HapticFeedback.selectionClick(); |
| 93 | + widget.onColorChanged(selectedColor); |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + void _onColorTap(int index) async { |
| 100 | + // 사용자가 특정 색상을 탭했을 때 해당 색상이 중앙에 오도록 부드럽게 스크롤합니다. |
| 101 | + int adjustedIndex = index; |
| 102 | + if (index < 2) adjustedIndex = 2; // 첫 번째 색상 (두 번째 인덱스)으로 제한 |
| 103 | + if (index >= colors.length - 2) adjustedIndex = colors.length - 3; // 마지막 색상 (마지막에서 두 번째 인덱스)으로 제한 |
| 104 | + |
| 105 | + setState(() { |
| 106 | + selectedIndex = adjustedIndex; |
| 107 | + }); |
| 108 | + |
| 109 | + double targetOffset = adjustedIndex * (circleSize + circlePadding) - (indicatorWidth / 2) + circleSize / 2; |
| 110 | + |
| 111 | + await _scrollController.animateTo( |
| 112 | + targetOffset, |
| 113 | + duration: animationDuration, |
| 114 | + curve: Curves.easeInOut, |
| 115 | + ); |
| 116 | + |
| 117 | + HapticFeedback.selectionClick(); |
| 118 | + widget.onColorChanged(selectedColor); |
| 119 | + } |
| 120 | + |
| 121 | + Color get selectedColor { |
| 122 | + if (selectedIndex == 0) { |
| 123 | + return AppColor.reservationColors.first; // 첫 번째 더미 색상 선택 시 첫 번째 실제 색상 반환 |
| 124 | + } else if (selectedIndex == colors.length - 1) { |
| 125 | + return AppColor.reservationColors.last; // 마지막 더미 색상 선택 시 마지막 실제 색상 반환 |
| 126 | + } else { |
| 127 | + return colors[selectedIndex]; // 그 외에는 실제 선택된 색상 반환 |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @override |
| 132 | + Widget build(BuildContext context) { |
| 133 | + return Column( |
| 134 | + mainAxisSize: MainAxisSize.min, |
| 135 | + children: [ |
| 136 | + Stack( |
| 137 | + alignment: Alignment.center, |
| 138 | + children: [ |
| 139 | + SizedBox( |
| 140 | + width: indicatorWidth, |
| 141 | + height: circleSize + scrollBarHeight, |
| 142 | + child: NotificationListener<ScrollNotification>( |
| 143 | + onNotification: (scrollNotification) { |
| 144 | + if (scrollNotification is ScrollUpdateNotification) { |
| 145 | + _onScroll(); |
| 146 | + } |
| 147 | + return true; |
| 148 | + }, |
| 149 | + child: ListView.builder( |
| 150 | + controller: _scrollController, |
| 151 | + scrollDirection: Axis.horizontal, |
| 152 | + itemCount: colors.length, |
| 153 | + itemBuilder: (context, index) { |
| 154 | + return GestureDetector( |
| 155 | + onTap: () => _onColorTap(index), |
| 156 | + child: Padding( |
| 157 | + padding: const EdgeInsets.symmetric(horizontal: circlePadding / 2), |
| 158 | + child: ColorUnitWidget( |
| 159 | + color: colors[index], |
| 160 | + showBorder: index >= 2 && index < 2 + widget.availableColors.length, |
| 161 | + borderWidth: selectedIndex == index ? 0 : 5, |
| 162 | + ) |
| 163 | + ), |
| 164 | + ); |
| 165 | + }, |
| 166 | + ), |
| 167 | + ), |
| 168 | + ), |
| 169 | + ], |
| 170 | + ), |
| 171 | + ], |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments