| 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:openapi/openapi.dart'; |
| 3 | import 'package:aero/extensions/datetime.dart'; |
| 4 | import 'package:aero/extensions/duration.dart'; |
| 5 | import 'package:intl/intl.dart'; |
| 6 | |
| 7 | class FlightRouteInfo extends StatelessWidget |
| 8 | final String departureCode; |
| 9 | final String departureName; |
| 10 | final String? departureTerminal; |
| 11 | final String? departureGate; |
| 12 | final DateTime departureTime; |
| 13 | final Duration? departureDelay; |
| 14 | final String departureSubtitle; |
| 15 | final String departureTimezone; |
| 16 | |
| 17 | final String arrivalCode; |
| 18 | final String arrivalName; |
| 19 | final String? arrivalTerminal; |
| 20 | final String? arrivalGate; |
| 21 | final DateTime arrivalTime; |
| 22 | final Duration? arrivalDelay; |
| 23 | final String arrivalSubtitle; |
| 24 | final String arrivalTimezone; |
| 25 | final String? arrivalBaggage; |
| 26 | |
| 27 | final Color? statusColor; |
| 28 | final bool use24Hrs; |
| 29 | final double? distanceKm; |
| 30 | final FlightAwareDataEntity? flightAwareDataEntity; |
| 31 | |
| 32 | const FlightRouteInfo( |
| 33 | {super.key, |
| 34 | required this.departureCode, |
| 35 | required this.departureName, |
| 36 | this.departureTerminal, |
| 37 | this.departureGate, |
| 38 | required this.departureTime, |
| 39 | this.departureDelay, |
| 40 | required this.departureSubtitle, |
| 41 | required this.arrivalCode, |
| 42 | required this.arrivalName, |
| 43 | this.arrivalTerminal, |
| 44 | this.arrivalGate, |
| 45 | required this.arrivalTime, |
| 46 | this.arrivalDelay, |
| 47 | required this.arrivalSubtitle, |
| 48 | this.statusColor, |
| 49 | required this.arrivalTimezone, |
| 50 | required this.departureTimezone, |
| 51 | required this.use24Hrs, |
| 52 | this.arrivalBaggage, |
| 53 | this.distanceKm, |
| 54 | this.flightAwareDataEntity = null}) |
| 55 | |
| 56 | @override |
| 57 | Widget build(BuildContext |
| 58 | final Duration totalDuration = arrivalTime |
| 59 | |
| 60 | return Container( |
| 61 | padding: const EdgeInsets. |
| 62 | 8.0, |
| 63 | ), |
| 64 | decoration: BoxDecoration( |
| 65 | border: Border.all( |
| 66 | color: Colors.grey.shade300 |
| 67 | ), |
| 68 | borderRadius: BorderRadius.circular( |
| 69 | 5, |
| 70 | ), |
| 71 | ), |
| 72 | child: Padding( |
| 73 | padding: const EdgeInsets. |
| 74 | top: 8.0, |
| 75 | ), |
| 76 | child: Column( |
| 77 | crossAxisAlignment: CrossAxisAlignment.start, |
| 78 | children: [ |
| 79 | _buildStopRow( |
| 80 | code: departureCode, |
| 81 | name: departureName, |
| 82 | terminal: departureTerminal, |
| 83 | gate: departureGate, |
| 84 | time: departureTime, |
| 85 | delay: departureDelay, |
| 86 | subtitle: departureSubtitle, |
| 87 | timezone: departureTimezone, |
| 88 | isDeparture: true, |
| 89 | ), |
| 90 | _buildMidDivider( |
| 91 | totalDuration: totalDuration, |
| 92 | distanceKm: distanceKm, |
| 93 | ), |
| 94 | _buildStopRow( |
| 95 | code: arrivalCode, |
| 96 | name: arrivalName, |
| 97 | terminal: arrivalTerminal, |
| 98 | gate: arrivalGate, |
| 99 | time: arrivalTime, |
| 100 | delay: arrivalDelay, |
| 101 | subtitle: arrivalSubtitle, |
| 102 | timezone: arrivalTimezone, |
| 103 | baggage: arrivalBaggage, |
| 104 | isDeparture: false, |
| 105 | ), |
| 106 | ], |
| 107 | ), |
| 108 | ), |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | Widget _buildStopRow({ |
| 113 | required String code, |
| 114 | required String name, |
| 115 | required String? terminal, |
| 116 | String? gate, |
| 117 | required DateTime time, |
| 118 | required Duration? delay, |
| 119 | required String subtitle, |
| 120 | required String timezone, |
| 121 | String? baggage, |
| 122 | required bool isDeparture, |
| 123 | }) { |
| 124 | final Duration? d = |
| 125 | final bool hasDelay = d |
| 126 | final bool isEarly = hasDelay |
| 127 | final Color timeColor = |
| 128 | hasDelay ? (isEarly ? Colors.green |
| 129 | final DateTime? originalTime = |
| 130 | |
| 131 | // Prefer props, fallback to FlightAware |
| 132 | final String? faTerminal = |
| 133 | ? flightAwareDataEntity?.terminalOrigin |
| 134 | : flightAwareDataEntity?.terminalDestination; |
| 135 | final String? faGate = |
| 136 | ? flightAwareDataEntity?.gateOrigin |
| 137 | : flightAwareDataEntity?.gateDestination; |
| 138 | final String? faBaggage = |
| 139 | final String? faRunway = |
| 140 | ? flightAwareDataEntity?.actualRunwayOff |
| 141 | : flightAwareDataEntity?.actualRunwayOn; |
| 142 | |
| 143 | final String? effTerminal = |
| 144 | (terminal != null && terminal.isNotEmpty) ? |
| 145 | final String? effGate = |
| 146 | final String? effBaggage = |
| 147 | (baggage != null && baggage.isNotEmpty) ? |
| 148 | final String? effRunway = |
| 149 | (faRunway != null && faRunway.isNotEmpty) ? |
| 150 | |
| 151 | return Row( |
| 152 | crossAxisAlignment: CrossAxisAlignment.start, |
| 153 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 154 | children: [ |
| 155 | Expanded( |
| 156 | child: Column( |
| 157 | crossAxisAlignment: CrossAxisAlignment.start, |
| 158 | children: [ |
| 159 | Text( |
| 160 | code, |
| 161 | style: const TextStyle( |
| 162 | fontSize: 20, |
| 163 | fontWeight: FontWeight.bold, |
| 164 | ), |
| 165 | ), |
| 166 | Text( |
| 167 | name, |
| 168 | style: TextStyle( |
| 169 | fontSize: 14, |
| 170 | color: Colors.grey.shade600 |
| 171 | ), |
| 172 | ), |
| 173 | const SizedBox(height: |
| 174 | _buildInfoPills( |
| 175 | terminal: effTerminal, |
| 176 | gate: effGate, |
| 177 | baggage: isDeparture ? null : effBaggage, |
| 178 | runway: effRunway, |
| 179 | isDeparture: isDeparture, |
| 180 | ), |
| 181 | if (subtitle.isNotEmpty) ...[ |
| 182 | const SizedBox(height: |
| 183 | Text( |
| 184 | subtitle, |
| 185 | style: TextStyle( |
| 186 | fontSize: 12, |
| 187 | color: Colors.grey.shade500 |
| 188 | ), |
| 189 | ), |
| 190 | ], |
| 191 | ], |
| 192 | ), |
| 193 | ), |
| 194 | const SizedBox(width: |
| 195 | // Right: time block |
| 196 | Column( |
| 197 | crossAxisAlignment: CrossAxisAlignment.end, |
| 198 | children: [ |
| 199 | Row( |
| 200 | mainAxisSize: MainAxisSize.min, |
| 201 | children: [ |
| 202 | if (originalTime != null) |
| 203 | Padding( |
| 204 | padding: const EdgeInsets. |
| 205 | child: Text( |
| 206 | originalTime.toTimezoneString( |
| 207 | timezone, |
| 208 | use24Hrs: use24Hrs, |
| 209 | ), |
| 210 | style: TextStyle( |
| 211 | fontSize: 12, |
| 212 | color: Colors.grey.shade500 |
| 213 | decoration: TextDecoration.lineThrough, |
| 214 | ), |
| 215 | ), |
| 216 | ), |
| 217 | Text( |
| 218 | time.toTimezoneString( |
| 219 | timezone, |
| 220 | use24Hrs: use24Hrs, |
| 221 | ), |
| 222 | style: TextStyle( |
| 223 | fontSize: 20, |
| 224 | color: timeColor, |
| 225 | fontWeight: FontWeight.bold, |
| 226 | ), |
| 227 | ), |
| 228 | ], |
| 229 | ), |
| 230 | if (hasDelay) |
| 231 | Padding( |
| 232 | padding: const EdgeInsets. |
| 233 | child: Text( |
| 234 | isEarly |
| 235 | ? "${_formatMinutesOnly(d |
| 236 | : "${_formatMinutesOnly(d |
| 237 | style: TextStyle( |
| 238 | fontSize: 12, |
| 239 | color: timeColor, |
| 240 | fontWeight: FontWeight.w600, |
| 241 | ), |
| 242 | ), |
| 243 | ), |
| 244 | if (hasDelay) |
| 245 | Padding( |
| 246 | padding: const EdgeInsets. |
| 247 | child: Text( |
| 248 | _timeAgo(time), |
| 249 | style: TextStyle( |
| 250 | fontSize: 12, |
| 251 | color: Colors.grey.shade600 |
| 252 | ), |
| 253 | ), |
| 254 | ), |
| 255 | ], |
| 256 | ), |
| 257 | ], |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | Widget _buildMidDivider( |
| 262 | {required Duration totalDuration, |
| 263 | final String durationText = totalDuration |
| 264 | String centerText = durationText; |
| 265 | if (distanceKm != null) { |
| 266 | final numberFormatter = NumberFormat. |
| 267 | final String kmFormatted = numberFormatter |
| 268 | centerText = "$durationText • $ |
| 269 | } |
| 270 | |
| 271 | return Row( |
| 272 | children: [ |
| 273 | Expanded(child: Divider(color |
| 274 | Container( |
| 275 | padding: const EdgeInsets. |
| 276 | margin: const EdgeInsets. |
| 277 | horizontal: 4, |
| 278 | vertical: 4, |
| 279 | ), |
| 280 | child: Text( |
| 281 | centerText, |
| 282 | style: TextStyle( |
| 283 | fontSize: 12, |
| 284 | color: Colors.grey.shade800 |
| 285 | fontWeight: FontWeight.w600, |
| 286 | ), |
| 287 | ), |
| 288 | ), |
| 289 | Expanded(child: Divider(color |
| 290 | ], |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | Widget _buildInfoPills( |
| 295 | {String? terminal, |
| 296 | String? gate, |
| 297 | String? baggage, |
| 298 | String? runway, |
| 299 | required bool isDeparture}) { |
| 300 | final List<Widget |
| 301 | if (terminal != null && terminal. |
| 302 | children.add(_pill( |
| 303 | icon: Icons.title_rounded, |
| 304 | label: terminal, |
| 305 | )); |
| 306 | } |
| 307 | if (gate != null && gate. |
| 308 | children.add(_pill( |
| 309 | icon: Icons.door_front_door, |
| 310 | label: gate, |
| 311 | )); |
| 312 | } |
| 313 | if (baggage != null && baggage. |
| 314 | children.add(_pill( |
| 315 | icon: Icons.luggage, |
| 316 | label: baggage, |
| 317 | )); |
| 318 | } |
| 319 | if (runway != null && runway. |
| 320 | children.add(_pill( |
| 321 | icon: isDeparture ? Icons.flight_takeoff |
| 322 | label: runway, |
| 323 | )); |
| 324 | } |
| 325 | |
| 326 | if (children.isEmpty) return const |
| 327 | |
| 328 | return Wrap( |
| 329 | spacing: 8, |
| 330 | runSpacing: 6, |
| 331 | children: children, |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | Widget _pill({required |
| 336 | return Container( |
| 337 | padding: const EdgeInsets. |
| 338 | decoration: BoxDecoration( |
| 339 | color: Colors.grey.shade100 |
| 340 | borderRadius: BorderRadius.circular( |
| 341 | border: Border.all(color |
| 342 | ), |
| 343 | child: Row( |
| 344 | mainAxisSize: MainAxisSize.min, |
| 345 | children: [ |
| 346 | Icon(icon, size: 14 |
| 347 | const SizedBox(width: |
| 348 | Text( |
| 349 | label, |
| 350 | style: TextStyle( |
| 351 | fontSize: 12, |
| 352 | color: Colors.grey.shade800 |
| 353 | fontWeight: FontWeight.w600, |
| 354 | ), |
| 355 | ), |
| 356 | ], |
| 357 | ), |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | String _formatMinutesOnly(Duration |
| 362 | final Duration abs = d |
| 363 | final int hours = abs |
| 364 | final int minutes = abs |
| 365 | if (hours > 0 && minutes > |
| 366 | return "$hours |
| 367 | } else if (hours > 0) { |
| 368 | return "$hours |
| 369 | } else if (minutes > 0) { |
| 370 | return "$minutes mins" |
| 371 | } else { |
| 372 | return "just now"; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | String _timeAgo(DateTime |
| 377 | final Duration diff = |
| 378 | if (diff.inMinutes.abs() |
| 379 | return "just now"; |
| 380 | } |
| 381 | |
| 382 | final Duration absDiff = diff |
| 383 | final int days = absDiff |
| 384 | final int hours = absDiff |
| 385 | final int minutes = absDiff |
| 386 | |
| 387 | String base; |
| 388 | if (days > 0) { |
| 389 | // Show days when more than 24 hours |
| 390 | final List<String |
| 391 | parts.add("$days |
| 392 | |
| 393 | if (hours > 0) { |
| 394 | parts.add("$hours |
| 395 | } |
| 396 | |
| 397 | if (minutes > 0) { |
| 398 | parts.add("$minutes |
| 399 | } |
| 400 | |
| 401 | base = parts.join( |
| 402 | } else { |
| 403 | // Less than 24 hours, use the existing format |
| 404 | base = _formatMinutesOnly(diff); |
| 405 | } |
| 406 | |
| 407 | if (diff.isNegative) { |
| 408 | return "in $base" |
| 409 | } |
| 410 | return "$base ago" |
| 411 | } |
| 412 | } |