From 42c97d8cb2193a00b333ec2911c1dd16be853eea Mon Sep 17 00:00:00 2001 From: KIMSEONGMIN <128333586+Brian0KIM@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:00:51 +0900 Subject: [PATCH] bus history by bus number implemented --- front/lib/bus_history_page.dart | 128 ++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 6 deletions(-) diff --git a/front/lib/bus_history_page.dart b/front/lib/bus_history_page.dart index 6f274aa..4de3077 100644 --- a/front/lib/bus_history_page.dart +++ b/front/lib/bus_history_page.dart @@ -23,13 +23,64 @@ class BusHistoryPage extends StatefulWidget { class _BusHistoryPageState extends State { bool isLoading = false; Map> groupedData = {}; + final Map _scrollControllers = {}; @override void initState() { super.initState(); fetchBusHistory(); } + @override + void dispose() { + // ScrollController 해제 + for (var controller in _scrollControllers.values) { + controller.dispose(); + } + super.dispose(); + } + void _scrollToCurrentTime(int daysAgo, List times) { + if (times.isEmpty) return; + + final now = DateTime.now(); + final currentTimeString = "${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}"; + + // 현재 시간과 가장 가까운 인덱스 찾기 + int closestIndex = 0; + int minDifference = double.maxFinite.toInt(); + + for (int i = 0; i < times.length; i++) { + final difference = _getTimeDifference(currentTimeString, times[i]); + if (difference.abs() < minDifference) { + minDifference = difference.abs(); + closestIndex = i; + } + } + // ScrollController가 없으면 생성 + if (!_scrollControllers.containsKey(daysAgo)) { + _scrollControllers[daysAgo] = ScrollController(); + } + + // 스크롤 위치 조정 + WidgetsBinding.instance.addPostFrameCallback((_) { + if (_scrollControllers[daysAgo]!.hasClients) { + _scrollControllers[daysAgo]!.animateTo( + closestIndex * 56.0, // ListTile의 대략적인 높이 + duration: const Duration(milliseconds: 300), + curve: Curves.easeOut, + ); + } + }); + } + int _getTimeDifference(String time1, String time2) { + final t1Parts = time1.split(':'); + final t2Parts = time2.split(':'); + + final t1Minutes = int.parse(t1Parts[0]) * 60 + int.parse(t1Parts[1]); + final t2Minutes = int.parse(t2Parts[0]) * 60 + int.parse(t2Parts[1]); + + return t2Minutes - t1Minutes; + } Future fetchBusHistory() async { setState(() { isLoading = true; @@ -64,6 +115,9 @@ class _BusHistoryPageState extends State { setState(() { groupedData = tempGroupedData; + groupedData.forEach((daysAgo, times) { + _scrollToCurrentTime(daysAgo, times); + }); }); } } @@ -95,11 +149,23 @@ class _BusHistoryPageState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text( - "${widget.stationName}(${widget.busNumber})", - style: const TextStyle( - fontSize: 20, - fontWeight: FontWeight.bold, + RichText( + text: TextSpan( + style: const TextStyle( + fontSize: 20, + fontWeight: FontWeight.bold, + color: Colors.black, // 기본 텍스트 색상 + ), + children: [ + TextSpan(text: widget.stationName), + TextSpan( + text: " ${widget.busNumber}", + style: TextStyle( + color: getBusColor(widget.busNumber), + ), + ), + const TextSpan(text: "번 버스"), + ], ), ), const Text( @@ -109,6 +175,13 @@ class _BusHistoryPageState extends State { fontSize: 14, ), ), + Text( + _getDateMessage(), // 요일별 안내 메시지 추가 + style: const TextStyle( + color: Colors.grey, + fontSize: 12, + ), + ), ], ), ), @@ -139,12 +212,13 @@ class _BusHistoryPageState extends State { const Divider(height: 1), Expanded( child: ListView.separated( + controller: _scrollControllers[entry.key], padding: EdgeInsets.zero, itemCount: entry.value.length, separatorBuilder: (context, index) => const Divider(height: 1), itemBuilder: (context, index) { return ListTile( - leading: const Icon(Icons.check), + leading: Icon(Icons.directions_bus, color: getBusColor(widget.busNumber)), title: Text( entry.value[index], style: const TextStyle( @@ -167,4 +241,46 @@ class _BusHistoryPageState extends State { ), ); } +} + + +Color getBusColor(String routeNumber) { + switch (routeNumber) { + case "9": + return const Color(0xff33CC99); // 지선버스 + case "1112": + return const Color(0xffE60012); // 지선버스 + case "5100": + return const Color(0xffE60012); // 광역버스 + case "7000": + return const Color(0xffE60012); // 광역버스 + case "M5107": + return const Color(0xff006896); // M버스 + case "1560A": + return const Color(0xffE60012); // 지선버스 + case "1560B": + return const Color(0xffE60012); // 지선버스 + default: + return Colors.black; + } +} +String _getDateMessage() { + final today = DateTime.now(); + final weekday = today.weekday; // 1 = 월요일, 7 = 일요일 + + switch (weekday) { + case 1: // 월요일 + return 'ⓘ 월요일: 3일전, 7일전 도착 기록을 제공합니다.'; + case 2: // 화요일 + return 'ⓘ 화요일: 1일전, 7일전 도착 기록을 제공합니다.'; + case 3: // 수요일 + case 4: // 목요일 + case 5: // 금요일 + return 'ⓘ 수요일, 목요일, 금요일: 1일전, 2일전, 7일전 도착 기록을 제공합니다.'; + case 6: // 토요일 + case 7: // 일요일 + return 'ⓘ 토요일, 일요일: 7일전 도착 기록을 제공합니다.'; + default: + return 'ⓘ 7일전 도착 기록을 제공합니다.'; + } } \ No newline at end of file