| 1 | import 'dart:io'; |
| 2 | import 'package:built_collection/built_collection.dart'; |
| 3 | import 'package:cached_network_image/cached_network_image.dart'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'package:flutter/services.dart'; |
| 6 | import 'package:gap/gap.dart'; |
| 7 | import 'package:material_design_icons_flutter/material_design_icons_flutter.dart'; |
| 8 | import 'package:openapi/openapi.dart'; |
| 9 | import 'package:aero/constants/main.dart'; |
| 10 | import 'package:aero/extensions/datetime.dart'; |
| 11 | import 'package:aero/formatters/time.dart'; |
| 12 | import 'package:aero/screens/home/widgets/flight_route_info.dart'; |
| 13 | import 'package:aero/screens/home/widgets/shareable_flight_card.dart'; |
| 14 | import 'package:timezone/timezone.dart' as tz; |
| 15 | import 'package:timezone/data/latest.dart' as tz; |
| 16 | import 'package:screenshot/screenshot.dart'; |
| 17 | import 'package:share_plus/share_plus.dart'; |
| 18 | import 'package:path_provider/path_provider.dart'; |
| 19 | |
| 20 | class FlightInfoWidget extends StatefulWidget |
| 21 | final FlightResponseEntity info; |
| 22 | final Function() onClose; |
| 23 | final Function()? onRefreshTracking |
| 24 | final Function()? onRefreshFlightData |
| 25 | final Function()? onBookingDetails |
| 26 | final BuiltList<FlightPositionEntity |
| 27 | const FlightInfoWidget({ |
| 28 | super.key, |
| 29 | required this.info, |
| 30 | required this.onClose, |
| 31 | this.onRefreshTracking, |
| 32 | this.onRefreshFlightData, |
| 33 | this.onBookingDetails, |
| 34 | required this.flightPositions, |
| 35 | }); |
| 36 | |
| 37 | @override |
| 38 | State<FlightInfoWidget> createState |
| 39 | } |
| 40 | |
| 41 | class _FlightInfoWidgetState extends State |
| 42 | bool _showAircraftPayload = false; |
| 43 | final ScreenshotController _screenshotController = |
| 44 | |
| 45 | String _formatKey(String |
| 46 | final withSpaces = key |
| 47 | .replaceAll('_', ' ' |
| 48 | .replaceAllMapped(RegExp(r'(?<!^)([A-Z0-9])' |
| 49 | final parts = withSpaces.split |
| 50 | return parts |
| 51 | .map((w) => w.isEmpty |
| 52 | ? w |
| 53 | : '${w[0 |
| 54 | .join(' '); |
| 55 | } |
| 56 | |
| 57 | String _formatEnumName(String |
| 58 | return enumName |
| 59 | .split('_') |
| 60 | .map((word) => '${ |
| 61 | .join(' '); |
| 62 | } |
| 63 | |
| 64 | bool? _coerceToBool(dynamic |
| 65 | if (value is bool) return |
| 66 | if (value is num) { |
| 67 | if (value == 1) return |
| 68 | if (value == 0) return |
| 69 | return null; |
| 70 | } |
| 71 | if (value is String) { |
| 72 | final v = value.trim |
| 73 | if (v == 'true' || v == |
| 74 | if (v == 'false' || v == |
| 75 | if (v == '1') return |
| 76 | if (v == '0') return |
| 77 | return null; |
| 78 | } |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | String _formatPayloadValue(dynamic |
| 83 | if (value is String) { |
| 84 | try { |
| 85 | final parsed = DateTime. |
| 86 | return formatDate(parsed); |
| 87 | } catch (_) { |
| 88 | // not a parsable date string; fall through |
| 89 | } |
| 90 | } |
| 91 | return "$value" |
| 92 | } |
| 93 | |
| 94 | Future<void> _shareFlightCard |
| 95 | try { |
| 96 | // Capture screenshot |
| 97 | final imageBytes = await _screenshotController. |
| 98 | InheritedTheme.captureAll( |
| 99 | context, |
| 100 | MediaQuery( |
| 101 | data: MediaQuery.of(context) |
| 102 | child: ShareableFlightCard(info: widget. |
| 103 | ), |
| 104 | ), |
| 105 | pixelRatio: 2.0, |
| 106 | delay: const Duration(milliseconds |
| 107 | ); |
| 108 | |
| 109 | if (imageBytes.isEmpty) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | // Save to temporary file |
| 114 | final tempDir = await getTemporaryDirectory |
| 115 | final file = File( |
| 116 | '${tempDir.path} |
| 117 | await file.writeAsBytes(imageBytes); |
| 118 | |
| 119 | // Share the file |
| 120 | await Share.shareXFiles( |
| 121 | [XFile(file.path)], |
| 122 | text: 'Check out my flight!', |
| 123 | ); |
| 124 | } catch (e) { |
| 125 | // Handle error silently or show a snackbar |
| 126 | if (mounted) { |
| 127 | ScaffoldMessenger.of(context).showSnackBar |
| 128 | SnackBar( |
| 129 | content: Text('Failed to share flight card: $e |
| 130 | ), |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | Widget _buildBanner( |
| 137 | BuildContext context, Color color |
| 138 | [String? subtitle]) { |
| 139 | final theme = Theme. |
| 140 | return Container( |
| 141 | padding: const EdgeInsets. |
| 142 | decoration: BoxDecoration( |
| 143 | borderRadius: BorderRadius.circular( |
| 144 | border: Border.all( |
| 145 | color: theme.dividerColor.withOpacity( |
| 146 | ), |
| 147 | ), |
| 148 | child: Row( |
| 149 | crossAxisAlignment: CrossAxisAlignment.start, |
| 150 | children: [ |
| 151 | Container( |
| 152 | width: 32, |
| 153 | height: 32, |
| 154 | decoration: BoxDecoration( |
| 155 | color: color.withOpacity(0.15) |
| 156 | borderRadius: BorderRadius.circular( |
| 157 | ), |
| 158 | child: Icon( |
| 159 | icon, |
| 160 | size: 18, |
| 161 | color: color, |
| 162 | ), |
| 163 | ), |
| 164 | const SizedBox(width: |
| 165 | Expanded( |
| 166 | child: Column( |
| 167 | crossAxisAlignment: CrossAxisAlignment.start, |
| 168 | spacing: subtitle != null ? 4 |
| 169 | children: [ |
| 170 | Text( |
| 171 | title, |
| 172 | style: const TextStyle( |
| 173 | fontWeight: FontWeight.w700, |
| 174 | ), |
| 175 | ), |
| 176 | if (subtitle != null) |
| 177 | Text( |
| 178 | subtitle, |
| 179 | style: TextStyle( |
| 180 | color: theme.textTheme.bodySmall?.color |
| 181 | theme.brightness == Brightness.dark |
| 182 | ), |
| 183 | ), |
| 184 | ], |
| 185 | ), |
| 186 | ), |
| 187 | ], |
| 188 | ), |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | Widget _buildFlightStatusBanner(BuildContext |
| 193 | final fa = widget.info. |
| 194 | if (fa?.cancelled == true) { |
| 195 | return Container( |
| 196 | width: double.infinity, |
| 197 | padding: const EdgeInsets. |
| 198 | decoration: BoxDecoration( |
| 199 | color: Colors.red, |
| 200 | borderRadius: BorderRadius.circular( |
| 201 | ), |
| 202 | child: Column( |
| 203 | crossAxisAlignment: CrossAxisAlignment.start, |
| 204 | children: [ |
| 205 | Text( |
| 206 | 'Flight Cancelled', |
| 207 | style: const TextStyle( |
| 208 | color: Colors.white, |
| 209 | fontSize: 16, |
| 210 | fontWeight: FontWeight.bold, |
| 211 | ), |
| 212 | ), |
| 213 | const SizedBox(height: |
| 214 | Text( |
| 215 | 'Contact your airline', |
| 216 | style: TextStyle( |
| 217 | color: Colors.white.withOpacity |
| 218 | fontSize: 13, |
| 219 | ), |
| 220 | ), |
| 221 | ], |
| 222 | ), |
| 223 | ); |
| 224 | } |
| 225 | if (fa?.diverted == true) { |
| 226 | return _buildBanner( |
| 227 | context, |
| 228 | Colors.amber, |
| 229 | Icons.warning_amber_rounded, |
| 230 | 'This flight has been diverted', |
| 231 | ); |
| 232 | } |
| 233 | return const SizedBox |
| 234 | } |
| 235 | |
| 236 | @override |
| 237 | Widget build(BuildContext |
| 238 | final arrival = widget.info. |
| 239 | final departure = widget.info. |
| 240 | final codeshares = widget.info. |
| 241 | ?.where((code) => code. |
| 242 | .map((code) => code.trim |
| 243 | .toList(growable: false) |
| 244 | const <String>[] |
| 245 | return Padding( |
| 246 | padding: const EdgeInsets. |
| 247 | 4.0, |
| 248 | ), |
| 249 | child: Column( |
| 250 | crossAxisAlignment: CrossAxisAlignment.start, |
| 251 | spacing: 4, |
| 252 | children: [ |
| 253 | if (DateTime.now() |
| 254 | _buildBanner( |
| 255 | context, |
| 256 | Colors.orange, |
| 257 | Icons.history, |
| 258 | 'Flight data may be inaccurate', |
| 259 | 'This flight is more than 7 days old. Times and status may no longer be updated.', |
| 260 | ), |
| 261 | Row( |
| 262 | spacing: 8, |
| 263 | crossAxisAlignment: CrossAxisAlignment.center, |
| 264 | children: [ |
| 265 | CachedNetworkImage( |
| 266 | imageUrl: |
| 267 | "https://www.flightaware.com/images/airline_logos/180px/${widget.info. |
| 268 | width: 30, |
| 269 | height: 30, |
| 270 | errorWidget: (context, url, error) { |
| 271 | return SizedBox( |
| 272 | height: 0, |
| 273 | width: 0, |
| 274 | ); |
| 275 | }, |
| 276 | ), |
| 277 | Column( |
| 278 | spacing: 2, |
| 279 | crossAxisAlignment: CrossAxisAlignment.start, |
| 280 | children: [ |
| 281 | Row( |
| 282 | children: [ |
| 283 | Text( |
| 284 | "${departure.airport. |
| 285 | style: TextStyle( |
| 286 | fontWeight: FontWeight.bold, |
| 287 | fontSize: 16, |
| 288 | ), |
| 289 | ) |
| 290 | ], |
| 291 | ), |
| 292 | Row( |
| 293 | children: [ |
| 294 | Text( |
| 295 | formatDayAndMonth( |
| 296 | DateTime.parse( |
| 297 | (departure.revisedTime ?? departure.scheduledTime) |
| 298 | .utc), |
| 299 | ), |
| 300 | style: TextStyle( |
| 301 | fontWeight: FontWeight.bold, |
| 302 | color: Theme.of(context) |
| 303 | fontSize: 12, |
| 304 | ), |
| 305 | ), |
| 306 | Text( |
| 307 | " • ", |
| 308 | style: Theme.of(context) |
| 309 | fontSize: 12, |
| 310 | ), |
| 311 | ), |
| 312 | Text( |
| 313 | widget.info.flightNo, |
| 314 | style: TextStyle( |
| 315 | fontWeight: FontWeight.bold, |
| 316 | color: Theme.of(context) |
| 317 | fontSize: 12, |
| 318 | ), |
| 319 | ), |
| 320 | if (widget.info.flightAwareData?.identIata |
| 321 | Text( |
| 322 | " • ", |
| 323 | style: |
| 324 | Theme.of(context).textTheme |
| 325 | fontSize: 12, |
| 326 | ), |
| 327 | ), |
| 328 | Text( |
| 329 | widget.info.flightAwareData?.identIata ?? |
| 330 | style: TextStyle( |
| 331 | fontWeight: FontWeight.bold, |
| 332 | color: |
| 333 | Theme.of(context).textTheme |
| 334 | fontSize: 12, |
| 335 | ), |
| 336 | ), |
| 337 | ] |
| 338 | ], |
| 339 | ), |
| 340 | ], |
| 341 | ), |
| 342 | Spacer(), |
| 343 | Row( |
| 344 | spacing: 4, |
| 345 | children: [ |
| 346 | PopupMenuButton( |
| 347 | icon: Icon(Icons. |
| 348 | color: Colors.white, |
| 349 | itemBuilder: (context) => [ |
| 350 | if (widget.onRefreshTracking != null) |
| 351 | PopupMenuItem( |
| 352 | value: 'refresh_tracking', |
| 353 | child: Row( |
| 354 | children: [ |
| 355 | Icon( |
| 356 | Icons.refresh, |
| 357 | ), |
| 358 | SizedBox(width: 8), |
| 359 | Text('Refresh tracking'), |
| 360 | ], |
| 361 | ), |
| 362 | ), |
| 363 | PopupMenuItem( |
| 364 | value: 'refresh_flight_data', |
| 365 | child: Row( |
| 366 | children: [ |
| 367 | Icon( |
| 368 | Icons.refresh, |
| 369 | ), |
| 370 | SizedBox(width: 8), |
| 371 | Text('Refresh flight data'), |
| 372 | ], |
| 373 | ), |
| 374 | ), |
| 375 | PopupMenuItem( |
| 376 | value: 'send_to_wearable', |
| 377 | child: Row( |
| 378 | children: [ |
| 379 | Icon( |
| 380 | Icons.watch, |
| 381 | ), |
| 382 | SizedBox(width: 8), |
| 383 | Text('Send to wearable'), |
| 384 | ], |
| 385 | ), |
| 386 | ), |
| 387 | PopupMenuItem( |
| 388 | value: 'booking_details', |
| 389 | child: Row( |
| 390 | children: [ |
| 391 | Icon(Icons.confirmation_number), |
| 392 | SizedBox(width: 8), |
| 393 | Text('Booking details'), |
| 394 | ], |
| 395 | ), |
| 396 | ), |
| 397 | PopupMenuItem( |
| 398 | value: 'share', |
| 399 | child: Row( |
| 400 | children: [ |
| 401 | Icon(Icons.share), |
| 402 | SizedBox(width: 8), |
| 403 | Text('Share'), |
| 404 | ], |
| 405 | ), |
| 406 | ), |
| 407 | ], |
| 408 | onSelected: (value) async { |
| 409 | if (value == "refresh_tracking") { |
| 410 | widget.onRefreshTracking?.call(); |
| 411 | } else if (value == "send_to_wearable") { |
| 412 | final channel = MethodChannel( |
| 413 | final flightNumber = widget.info. |
| 414 | final airline = widget.info. |
| 415 | final flightAwareData = widget.info. |
| 416 | final departureAirport = departure.airport; |
| 417 | final arrivalAirport = widget.info. |
| 418 | final departureDate = DateTime. |
| 419 | (departure.revisedTime ?? departure.scheduledTime) |
| 420 | .utc, |
| 421 | ); |
| 422 | |
| 423 | final airlineIata = airline.iata ?? |
| 424 | final airlineIcao = airline.icao ?? |
| 425 | final flightNumberWithoutIata = |
| 426 | flightNumber.replaceFirst(airlineIata, '') |
| 427 | final flightNumberWithoutIcao = |
| 428 | flightNumber.replaceFirst(airlineIcao, '') |
| 429 | |
| 430 | String? _fallbackIdent( |
| 431 | String? primary, |
| 432 | String carrier, |
| 433 | String strippedNumber, |
| 434 | ) { |
| 435 | if (primary != null && primary. |
| 436 | return primary; |
| 437 | } |
| 438 | if (carrier.isEmpty) return null |
| 439 | final number = strippedNumber.isNotEmpty |
| 440 | ? strippedNumber |
| 441 | : flightNumber; |
| 442 | return "$carrier$ |
| 443 | } |
| 444 | |
| 445 | final identIata = _fallbackIdent( |
| 446 | flightAwareData?.identIata, |
| 447 | airlineIata, |
| 448 | flightNumberWithoutIata, |
| 449 | ); |
| 450 | |
| 451 | final identIcao = _fallbackIdent( |
| 452 | flightAwareData?.identIcao, |
| 453 | airlineIcao, |
| 454 | flightNumberWithoutIcao, |
| 455 | ); |
| 456 | |
| 457 | final departureAirportCode = |
| 458 | departureAirport.iata.isNotEmpty |
| 459 | ? departureAirport.iata |
| 460 | : departureAirport.icao; |
| 461 | final arrivalAirportCode = |
| 462 | arrivalAirport.iata.isNotEmpty |
| 463 | ? arrivalAirport.iata |
| 464 | : arrivalAirport.icao; |
| 465 | final departureCity = |
| 466 | departureAirport.municipalityName.isNotEmpty |
| 467 | ? departureAirport.municipalityName |
| 468 | : departureAirport.name; |
| 469 | final arrivalCity = |
| 470 | arrivalAirport.municipalityName.isNotEmpty |
| 471 | ? arrivalAirport.municipalityName |
| 472 | : arrivalAirport.name; |
| 473 | |
| 474 | final payload = { |
| 475 | "flight": { |
| 476 | "id": widget.info.id, |
| 477 | "flightNo": flightNumber, |
| 478 | "identIata": identIata, |
| 479 | "identIcao": identIcao, |
| 480 | "date": departureDate.toIso8601String(), |
| 481 | "departureAirportIata": departureAirportCode, |
| 482 | "departureCity": departureCity, |
| 483 | "arrivalAirportIata": arrivalAirportCode, |
| 484 | "arrivalCity": arrivalCity, |
| 485 | }, |
| 486 | "airline": { |
| 487 | "name": airline.name, |
| 488 | "iata": airline.iata, |
| 489 | "icao": airline.icao, |
| 490 | "image": airline.image, |
| 491 | }, |
| 492 | }; |
| 493 | |
| 494 | await channel.invokeMethod("registerFlight", |
| 495 | print(payload); |
| 496 | ScaffoldMessenger.of(context).showSnackBar |
| 497 | SnackBar( |
| 498 | content: Text('Flight sent to wearable'), |
| 499 | ), |
| 500 | ); |
| 501 | } |
| 502 | if (value == "refresh_flight_data") { |
| 503 | widget.onRefreshFlightData?.call(); |
| 504 | } |
| 505 | if (value == "share") { |
| 506 | _shareFlightCard(); |
| 507 | } |
| 508 | if (value == "booking_details") { |
| 509 | widget.onBookingDetails?.call(); |
| 510 | } |
| 511 | }, |
| 512 | ), |
| 513 | SizedBox( |
| 514 | width: 24, |
| 515 | child: IconButton( |
| 516 | onPressed: widget.onClose, |
| 517 | padding: EdgeInsets.zero, |
| 518 | icon: Icon(Icons. |
| 519 | ), |
| 520 | ), |
| 521 | ], |
| 522 | ) |
| 523 | ], |
| 524 | ), |
| 525 | Divider(), |
| 526 | if ((widget.info.flightAwareData?.cancelled |
| 527 | (widget.info.flightAwareData?.diverted == |
| 528 | _buildFlightStatusBanner(context), |
| 529 | Gap(8), |
| 530 | ], |
| 531 | if (widget.info.flightAwareData?.cancelled |
| 532 | Column( |
| 533 | crossAxisAlignment: CrossAxisAlignment.start, |
| 534 | children: [ |
| 535 | Text( |
| 536 | getTimeMessage( |
| 537 | DateTime.parse( |
| 538 | (departure.revisedTime ?? |
| 539 | departure.predictedTime ?? |
| 540 | departure.scheduledTime) |
| 541 | .utc, |
| 542 | ), |
| 543 | DateTime.parse( |
| 544 | (arrival.revisedTime ?? |
| 545 | arrival.predictedTime ?? |
| 546 | arrival.scheduledTime) |
| 547 | .utc, |
| 548 | ), |
| 549 | ), |
| 550 | style: TextStyle( |
| 551 | fontSize: 18, |
| 552 | fontWeight: FontWeight.bold, |
| 553 | ), |
| 554 | ), |
| 555 | ], |
| 556 | ), |
| 557 | FlightRouteInfo( |
| 558 | arrivalCode: arrival.airport.iata, |
| 559 | arrivalDelay: _calculateDelay(arrival.revisedTime, |
| 560 | arrival.predictedTime ?? arrival.scheduledTime), |
| 561 | arrivalName: arrival.airport.name, |
| 562 | arrivalSubtitle: "", |
| 563 | arrivalTerminal: arrival.terminal, |
| 564 | arrivalTime: DateTime.parse((arrival |
| 565 | arrival.predictedTime ?? |
| 566 | arrival.scheduledTime) |
| 567 | .utc), |
| 568 | arrivalTimezone: arrival.airport.timeZone, |
| 569 | departureCode: departure.airport.iata, |
| 570 | departureDelay: _calculateDelay(departure.revisedTime, |
| 571 | departure.predictedTime ?? departure.scheduledTime), |
| 572 | departureName: departure.airport.name, |
| 573 | departureSubtitle: "", |
| 574 | departureTerminal: departure.terminal, |
| 575 | departureTime: DateTime.parse((departure |
| 576 | departure.predictedTime ?? |
| 577 | departure.scheduledTime) |
| 578 | .utc), |
| 579 | departureTimezone: departure.airport.timeZone, |
| 580 | statusColor: Colors.green, |
| 581 | use24Hrs: MediaQuery.of(context) |
| 582 | arrivalGate: |
| 583 | widget.info.flightAwareData?.gateDestination ?? arrival |
| 584 | departureGate: |
| 585 | widget.info.flightAwareData?.gateOrigin ?? departure |
| 586 | arrivalBaggage: widget.info.flightAwareData?.baggageClaim |
| 587 | arrival.baggageBelt, |
| 588 | distanceKm: widget.info.greatCircleDistance.km != |
| 589 | ? double.tryParse(widget |
| 590 | : null, |
| 591 | flightAwareDataEntity: widget.info.flightAwareData, |
| 592 | ), |
| 593 | Gap(8), |
| 594 | Text( |
| 595 | "Worth Noting", |
| 596 | style: TextStyle( |
| 597 | fontSize: 16, |
| 598 | fontWeight: FontWeight.bold, |
| 599 | ), |
| 600 | ), |
| 601 | Column( |
| 602 | children: [ |
| 603 | _buildTimezonePill( |
| 604 | context: context, |
| 605 | arrival: arrival, |
| 606 | departure: departure, |
| 607 | ), |
| 608 | if (codeshares.isNotEmpty) ...[ |
| 609 | const Gap(8) |
| 610 | _buildCodesharePill(context, codeshares), |
| 611 | ], |
| 612 | ], |
| 613 | ), |
| 614 | // Flight Timetable Section |
| 615 | if (widget.info.flightAwareData != null |
| 616 | _buildFlightTimetable(context), |
| 617 | // Booking Details Section |
| 618 | if (widget.info.bookings != null |
| 619 | widget.info.bookings!.isNotEmpty) ...[ |
| 620 | Gap(8), |
| 621 | Text( |
| 622 | "Booking Details", |
| 623 | style: TextStyle( |
| 624 | fontSize: 16, |
| 625 | fontWeight: FontWeight.bold, |
| 626 | ), |
| 627 | ), |
| 628 | Container( |
| 629 | decoration: BoxDecoration( |
| 630 | border: Border.all( |
| 631 | color: Colors.grey.shade300 |
| 632 | ), |
| 633 | borderRadius: BorderRadius.circular( |
| 634 | ), |
| 635 | child: Padding( |
| 636 | padding: const EdgeInsets. |
| 637 | top: 0, |
| 638 | bottom: 0, |
| 639 | ), |
| 640 | child: Column( |
| 641 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 642 | spacing: 8, |
| 643 | children: [ |
| 644 | Builder(builder: (context) { |
| 645 | final booking = widget.info. |
| 646 | |
| 647 | // Extract string values from JsonObject |
| 648 | String? _extractString(dynamic |
| 649 | if (jsonObject == null) return |
| 650 | if (jsonObject is String) return |
| 651 | try { |
| 652 | final value = (jsonObject as dynamic |
| 653 | if (value is String) return |
| 654 | return value?.toString() |
| 655 | } catch (e) { |
| 656 | return jsonObject.toString(); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | final bookingCode = _extractString(booking |
| 661 | final seatNumber = _extractString(booking |
| 662 | final seatingClass = booking.seatingClass? |
| 663 | final seatType = booking.seatType? |
| 664 | final reason = booking.reason? |
| 665 | |
| 666 | return Column( |
| 667 | crossAxisAlignment: CrossAxisAlignment.start, |
| 668 | children: [ |
| 669 | if (bookingCode != null && bookingCode. |
| 670 | ListTile( |
| 671 | contentPadding: EdgeInsets.zero, |
| 672 | leading: Icon(Icons. |
| 673 | title: Text( |
| 674 | 'Booking Code: $bookingCode', |
| 675 | style: TextStyle( |
| 676 | fontWeight: FontWeight.w600, |
| 677 | ), |
| 678 | ), |
| 679 | ), |
| 680 | if (seatNumber != null && seatNumber. |
| 681 | ListTile( |
| 682 | contentPadding: EdgeInsets.zero, |
| 683 | leading: Icon(Icons. |
| 684 | title: Text( |
| 685 | 'Seat: $seatNumber${seatType != |
| 686 | style: TextStyle( |
| 687 | fontWeight: FontWeight.w600, |
| 688 | ), |
| 689 | ), |
| 690 | ), |
| 691 | if (seatingClass != null) |
| 692 | ListTile( |
| 693 | contentPadding: EdgeInsets.zero, |
| 694 | leading: Icon(Icons. |
| 695 | title: Text( |
| 696 | 'Class: ${_formatEnumName(seatingClass)} |
| 697 | style: TextStyle( |
| 698 | fontWeight: FontWeight.w600, |
| 699 | ), |
| 700 | ), |
| 701 | ), |
| 702 | if (reason != null) |
| 703 | ListTile( |
| 704 | contentPadding: EdgeInsets.zero, |
| 705 | leading: Icon(Icons. |
| 706 | title: Text( |
| 707 | 'Reason: ${_formatEnumName(reason)} |
| 708 | style: TextStyle( |
| 709 | fontWeight: FontWeight.w600, |
| 710 | ), |
| 711 | ), |
| 712 | ), |
| 713 | ], |
| 714 | ); |
| 715 | }), |
| 716 | ], |
| 717 | ), |
| 718 | ), |
| 719 | ), |
| 720 | ], |
| 721 | Gap(8), |
| 722 | if (widget.info.aircraft?.model |
| 723 | Text( |
| 724 | widget.info.aircraft!.model!, |
| 725 | style: TextStyle( |
| 726 | fontSize: 16, |
| 727 | fontWeight: FontWeight.bold, |
| 728 | ), |
| 729 | ), |
| 730 | if (widget.info.aircraft?.registration |
| 731 | Container( |
| 732 | decoration: BoxDecoration( |
| 733 | border: Border.all( |
| 734 | color: Colors.grey.shade300 |
| 735 | ), |
| 736 | borderRadius: BorderRadius.circular( |
| 737 | 5, |
| 738 | ), |
| 739 | ), |
| 740 | child: Padding( |
| 741 | padding: const EdgeInsets. |
| 742 | 8.0, |
| 743 | ), |
| 744 | child: Column( |
| 745 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 746 | spacing: 8, |
| 747 | children: [ |
| 748 | if (widget.info.aircraft!.image |
| 749 | ClipRRect( |
| 750 | borderRadius: BorderRadius.circular( |
| 751 | 5, |
| 752 | ), |
| 753 | child: CachedNetworkImage( |
| 754 | imageUrl: widget.info.aircraft!.image |
| 755 | ), |
| 756 | ), |
| 757 | Wrap( |
| 758 | spacing: 10, |
| 759 | alignment: WrapAlignment.spaceBetween, |
| 760 | runSpacing: 10, |
| 761 | children: [ |
| 762 | if (widget.info.aircraft?.age |
| 763 | Column( |
| 764 | crossAxisAlignment: CrossAxisAlignment.start, |
| 765 | children: [ |
| 766 | Text( |
| 767 | "Age", |
| 768 | style: TextStyle( |
| 769 | fontWeight: FontWeight.bold, |
| 770 | color: Colors |
| 771 | .grey[700], // Adjust color as needed |
| 772 | ), |
| 773 | ), |
| 774 | Text( |
| 775 | formatAge( |
| 776 | double.parse( |
| 777 | widget.info.aircraft!.age!, |
| 778 | ), |
| 779 | ), |
| 780 | style: TextStyle( |
| 781 | fontWeight: FontWeight.normal, |
| 782 | color: Colors.black, |
| 783 | ), |
| 784 | ), |
| 785 | ], |
| 786 | ), |
| 787 | if (widget.info.aircraft?.deliveryDate |
| 788 | Column( |
| 789 | crossAxisAlignment: CrossAxisAlignment.end, |
| 790 | children: [ |
| 791 | Text( |
| 792 | "Delivery Date", |
| 793 | style: TextStyle( |
| 794 | fontWeight: FontWeight.bold, |
| 795 | color: Colors |
| 796 | .grey[700], // Adjust color as needed |
| 797 | ), |
| 798 | ), |
| 799 | Text( |
| 800 | formatDate(DateTime.parse |
| 801 | widget.info.aircraft!.deliveryDate!)) |
| 802 | style: TextStyle( |
| 803 | fontWeight: FontWeight.normal, |
| 804 | color: Colors.black, |
| 805 | ), |
| 806 | ), |
| 807 | ], |
| 808 | ), |
| 809 | if (widget.info.aircraft?.registration |
| 810 | Column( |
| 811 | crossAxisAlignment: CrossAxisAlignment.start, |
| 812 | children: [ |
| 813 | Text( |
| 814 | "Registration", |
| 815 | style: TextStyle( |
| 816 | fontWeight: FontWeight.bold, |
| 817 | color: Colors |
| 818 | .grey[700], // Adjust color as needed |
| 819 | ), |
| 820 | ), |
| 821 | Text( |
| 822 | widget.info.aircraft!.registration!, |
| 823 | style: TextStyle( |
| 824 | fontWeight: FontWeight.normal, |
| 825 | color: Colors.black, |
| 826 | ), |
| 827 | ), |
| 828 | const SizedBox(height: |
| 829 | Builder(builder: (context) { |
| 830 | final payload = |
| 831 | (widget.info.aircraft?.payload)?. |
| 832 | final entries = payload == null |
| 833 | ? const <MapEntry |
| 834 | : payload.entries |
| 835 | .where((e) => |
| 836 | e.value is String || |
| 837 | e.value is num || |
| 838 | e.value is bool) |
| 839 | .where((e) => |
| 840 | e.key != 'id' && |
| 841 | e.key != 'numRegistrations') |
| 842 | .toList(); |
| 843 | if (entries.isEmpty) { |
| 844 | return const SizedBox |
| 845 | } |
| 846 | return Column( |
| 847 | crossAxisAlignment: CrossAxisAlignment.start, |
| 848 | children: [ |
| 849 | TextButton( |
| 850 | onPressed: () { |
| 851 | setState(() { |
| 852 | _showAircraftPayload = |
| 853 | !_showAircraftPayload; |
| 854 | }); |
| 855 | }, |
| 856 | style: TextButton.styleFrom( |
| 857 | padding: EdgeInsets.zero, |
| 858 | minimumSize: const Size( |
| 859 | tapTargetSize: |
| 860 | MaterialTapTargetSize.shrinkWrap, |
| 861 | ), |
| 862 | child: Row( |
| 863 | mainAxisSize: MainAxisSize.min, |
| 864 | children: [ |
| 865 | Text( |
| 866 | _showAircraftPayload |
| 867 | ? "Hide more info" |
| 868 | : "Show more info", |
| 869 | style: TextStyle( |
| 870 | fontWeight: FontWeight.w600, |
| 871 | ), |
| 872 | ), |
| 873 | const SizedBox(width: |
| 874 | Icon( |
| 875 | _showAircraftPayload |
| 876 | ? Icons.expand_less |
| 877 | : Icons.expand_more, |
| 878 | size: 18), |
| 879 | ], |
| 880 | ), |
| 881 | ), |
| 882 | if (_showAircraftPayload) |
| 883 | Padding( |
| 884 | padding: |
| 885 | const EdgeInsets.only |
| 886 | child: Column( |
| 887 | crossAxisAlignment: |
| 888 | CrossAxisAlignment.start, |
| 889 | children: entries.map((e) { |
| 890 | final coerced = |
| 891 | _coerceToBool(e.value); |
| 892 | if (coerced != null) { |
| 893 | final boolColor = coerced |
| 894 | ? Colors.green |
| 895 | : Colors.red; |
| 896 | final label = |
| 897 | coerced ? 'Yes' : 'No'; |
| 898 | return Padding( |
| 899 | padding: |
| 900 | const EdgeInsets.symmetric |
| 901 | vertical: 1.0), |
| 902 | child: Row( |
| 903 | mainAxisAlignment: |
| 904 | MainAxisAlignment |
| 905 | .spaceBetween, |
| 906 | children: [ |
| 907 | Expanded( |
| 908 | child: Text( |
| 909 | _formatKey(e.key), |
| 910 | style: TextStyle( |
| 911 | color: |
| 912 | Colors.grey[700], |
| 913 | fontWeight: |
| 914 | FontWeight.w600, |
| 915 | ), |
| 916 | ), |
| 917 | ), |
| 918 | const SizedBox(width: |
| 919 | Row( |
| 920 | mainAxisSize: |
| 921 | MainAxisSize.min, |
| 922 | children: [ |
| 923 | Container( |
| 924 | width: 10, |
| 925 | height: 10, |
| 926 | decoration: |
| 927 | BoxDecoration( |
| 928 | color: boolColor, |
| 929 | shape: |
| 930 | BoxShape.circle, |
| 931 | ), |
| 932 | ), |
| 933 | const SizedBox( |
| 934 | width: 4), |
| 935 | Text( |
| 936 | label, |
| 937 | style: TextStyle( |
| 938 | color: boolColor, |
| 939 | fontSize: 12, |
| 940 | fontWeight: |
| 941 | FontWeight.w600, |
| 942 | ), |
| 943 | ), |
| 944 | ], |
| 945 | ), |
| 946 | ], |
| 947 | ), |
| 948 | ); |
| 949 | } |
| 950 | return Padding( |
| 951 | padding: |
| 952 | const EdgeInsets.symmetric |
| 953 | vertical: 1.0), |
| 954 | child: Row( |
| 955 | mainAxisAlignment: |
| 956 | MainAxisAlignment |
| 957 | .spaceBetween, |
| 958 | children: [ |
| 959 | Expanded( |
| 960 | child: Text( |
| 961 | _formatKey(e.key), |
| 962 | style: TextStyle( |
| 963 | color: Colors.grey[700] |
| 964 | fontWeight: |
| 965 | FontWeight.w600, |
| 966 | ), |
| 967 | ), |
| 968 | ), |
| 969 | const SizedBox(width: |
| 970 | Flexible( |
| 971 | child: Text( |
| 972 | _formatPayloadValue( |
| 973 | e.value), |
| 974 | textAlign: |
| 975 | TextAlign.right, |
| 976 | overflow: |
| 977 | TextOverflow.ellipsis, |
| 978 | ), |
| 979 | ), |
| 980 | ], |
| 981 | ), |
| 982 | ); |
| 983 | }).toList(), |
| 984 | ), |
| 985 | ), |
| 986 | ], |
| 987 | ); |
| 988 | }), |
| 989 | ], |
| 990 | ), |
| 991 | ], |
| 992 | ) |
| 993 | ], |
| 994 | ), |
| 995 | ), |
| 996 | ), |
| 997 | if (widget.info.aircraft?.registrations |
| 998 | widget.info.aircraft!.registrations!. |
| 999 | Gap(8), |
| 1000 | _buildRegistrationHistoryCard(context), |
| 1001 | ], |
| 1002 | // Jet2 Holiday Meme |
| 1003 | if (widget.info.airline.icao == |
| 1004 | widget.info.airline.iata == 'LS' |
| 1005 | const Gap(16) |
| 1006 | _buildJet2Meme(context), |
| 1007 | ], |
| 1008 | ], |
| 1009 | ), |
| 1010 | ); |
| 1011 | } |
| 1012 | |
| 1013 | Widget _buildRegistrationHistoryCard(BuildContext |
| 1014 | final theme = Theme. |
| 1015 | final registrations = widget.info. |
| 1016 | registrations.sort((a, b) { |
| 1017 | final ad = a.registrationDate; |
| 1018 | final bd = b.registrationDate; |
| 1019 | if (ad == null && bd == |
| 1020 | if (ad == null) return |
| 1021 | if (bd == null) return |
| 1022 | return DateTime.parse |
| 1023 | }); |
| 1024 | |
| 1025 | return Column( |
| 1026 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1027 | children: [ |
| 1028 | Text( |
| 1029 | "Registration History", |
| 1030 | style: TextStyle( |
| 1031 | fontSize: 16, |
| 1032 | fontWeight: FontWeight.bold, |
| 1033 | ), |
| 1034 | ), |
| 1035 | const Gap(6) |
| 1036 | Container( |
| 1037 | padding: const EdgeInsets. |
| 1038 | decoration: BoxDecoration( |
| 1039 | borderRadius: BorderRadius.circular( |
| 1040 | border: Border.all( |
| 1041 | color: theme.dividerColor.withOpacity( |
| 1042 | ), |
| 1043 | ), |
| 1044 | child: Column( |
| 1045 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 1046 | children: [ |
| 1047 | for (int i = 0 |
| 1048 | if (i > 0) |
| 1049 | Divider( |
| 1050 | height: 16, |
| 1051 | color: theme.dividerColor.withOpacity( |
| 1052 | ), |
| 1053 | _buildRegistrationRow(context, registrations[i]), |
| 1054 | ], |
| 1055 | ], |
| 1056 | ), |
| 1057 | ), |
| 1058 | ], |
| 1059 | ); |
| 1060 | } |
| 1061 | |
| 1062 | Widget _buildRegistrationRow( |
| 1063 | BuildContext context, AircraftRegistrationEntity reg) { |
| 1064 | final theme = Theme. |
| 1065 | final statusColor = reg.active ? |
| 1066 | final statusLabel = reg.active ? |
| 1067 | String? dateLabel; |
| 1068 | if (reg.registrationDate != null) { |
| 1069 | try { |
| 1070 | dateLabel = formatDate(DateTime. |
| 1071 | } catch (_) {} |
| 1072 | } |
| 1073 | return Row( |
| 1074 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1075 | children: [ |
| 1076 | Container( |
| 1077 | margin: const EdgeInsets. |
| 1078 | width: 8, |
| 1079 | height: 8, |
| 1080 | decoration: BoxDecoration( |
| 1081 | color: statusColor, |
| 1082 | shape: BoxShape.circle, |
| 1083 | ), |
| 1084 | ), |
| 1085 | const SizedBox(width: |
| 1086 | Expanded( |
| 1087 | child: Column( |
| 1088 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1089 | children: [ |
| 1090 | Row( |
| 1091 | children: [ |
| 1092 | Text( |
| 1093 | reg.reg, |
| 1094 | style: const TextStyle( |
| 1095 | fontWeight: FontWeight.w700, |
| 1096 | fontSize: 14, |
| 1097 | ), |
| 1098 | ), |
| 1099 | const SizedBox(width: |
| 1100 | Container( |
| 1101 | padding: const EdgeInsets. |
| 1102 | horizontal: 6, vertical: 2) |
| 1103 | decoration: BoxDecoration( |
| 1104 | color: statusColor.withOpacity(0.12) |
| 1105 | borderRadius: BorderRadius.circular( |
| 1106 | ), |
| 1107 | child: Text( |
| 1108 | statusLabel, |
| 1109 | style: TextStyle( |
| 1110 | color: statusColor, |
| 1111 | fontSize: 10, |
| 1112 | fontWeight: FontWeight.w700, |
| 1113 | ), |
| 1114 | ), |
| 1115 | ), |
| 1116 | ], |
| 1117 | ), |
| 1118 | if (reg.airlineName != null && |
| 1119 | Padding( |
| 1120 | padding: const EdgeInsets. |
| 1121 | child: Text( |
| 1122 | reg.airlineName!, |
| 1123 | style: TextStyle( |
| 1124 | fontSize: 12, |
| 1125 | color: theme.textTheme.bodySmall?.color |
| 1126 | ), |
| 1127 | ), |
| 1128 | ), |
| 1129 | if (dateLabel != null) |
| 1130 | Padding( |
| 1131 | padding: const EdgeInsets. |
| 1132 | child: Text( |
| 1133 | 'Registered on: $dateLabel', |
| 1134 | style: TextStyle( |
| 1135 | fontSize: 11, |
| 1136 | color: theme.textTheme.bodySmall?.color |
| 1137 | ?.withOpacity(0.7), |
| 1138 | ), |
| 1139 | ), |
| 1140 | ), |
| 1141 | if (reg.hexIcao != null && |
| 1142 | Padding( |
| 1143 | padding: const EdgeInsets. |
| 1144 | child: Text( |
| 1145 | 'ICAO Hex: ${reg.hexIcao!} |
| 1146 | style: TextStyle( |
| 1147 | fontSize: 11, |
| 1148 | fontFamily: 'monospace', |
| 1149 | color: theme.textTheme.bodySmall?.color |
| 1150 | ?.withOpacity(0.7), |
| 1151 | ), |
| 1152 | ), |
| 1153 | ), |
| 1154 | ], |
| 1155 | ), |
| 1156 | ), |
| 1157 | ], |
| 1158 | ); |
| 1159 | } |
| 1160 | |
| 1161 | Widget _buildJet2Meme(BuildContext |
| 1162 | final isDark = Theme. |
| 1163 | |
| 1164 | return Padding( |
| 1165 | padding: const EdgeInsets. |
| 1166 | child: Center( |
| 1167 | child: Text( |
| 1168 | 'Nothing beats a Jet2 holiday', |
| 1169 | textAlign: TextAlign.center, |
| 1170 | style: TextStyle( |
| 1171 | fontSize: 10, |
| 1172 | fontWeight: FontWeight.w400, |
| 1173 | letterSpacing: 0.5, |
| 1174 | color: isDark ? Colors.grey[ |
| 1175 | ), |
| 1176 | ), |
| 1177 | ), |
| 1178 | ); |
| 1179 | } |
| 1180 | |
| 1181 | Widget _buildFlightTimetable(BuildContext |
| 1182 | final fa = widget.info. |
| 1183 | final departure = widget.info. |
| 1184 | final arrival = widget.info. |
| 1185 | final theme = Theme. |
| 1186 | final use24Hrs = MediaQuery. |
| 1187 | |
| 1188 | final depTz = _sanitizeTimezone(departure |
| 1189 | final arrTz = _sanitizeTimezone(arrival |
| 1190 | |
| 1191 | // Check if any timetable data exists |
| 1192 | final hasAnyData = fa.scheduledOut != |
| 1193 | fa.estimatedOut != null || |
| 1194 | fa.actualOut != null || |
| 1195 | fa.scheduledOff != null || |
| 1196 | fa.estimatedOff != null || |
| 1197 | fa.actualOff != null || |
| 1198 | fa.scheduledOn != null || |
| 1199 | fa.estimatedOn != null || |
| 1200 | fa.actualOn != null || |
| 1201 | fa.scheduledIn != null || |
| 1202 | fa.estimatedIn != null || |
| 1203 | fa.actualIn != null; |
| 1204 | |
| 1205 | if (!hasAnyData) return const |
| 1206 | |
| 1207 | try { |
| 1208 | tz.initializeTimeZones(); |
| 1209 | } catch (_) {} |
| 1210 | |
| 1211 | String formatTimeInTz(DateTime |
| 1212 | final location = tz.getLocation |
| 1213 | final tzDt = tz.TZDateTime |
| 1214 | if (use24Hrs) { |
| 1215 | return '${tzDt. |
| 1216 | } |
| 1217 | final hour = tzDt.hour % |
| 1218 | final amPm = tzDt.hour >= |
| 1219 | return '$hour: |
| 1220 | } |
| 1221 | |
| 1222 | String formatDateTimeInTz(DateTime |
| 1223 | final location = tz.getLocation |
| 1224 | final tzDt = tz.TZDateTime |
| 1225 | return '${MONTHS[ |
| 1226 | } |
| 1227 | |
| 1228 | String formatDuration(int |
| 1229 | final absMins = mins.abs |
| 1230 | if (absMins < 60) return |
| 1231 | final hours = absMins ~/ |
| 1232 | final remainder = absMins % 60 |
| 1233 | if (remainder == 0) return |
| 1234 | return '${hours} |
| 1235 | } |
| 1236 | |
| 1237 | /// Returns (label, color) for the status indicator comparing scheduled vs actual/estimated |
| 1238 | (String, Color) getTimeDelta |
| 1239 | if (actual == null) return |
| 1240 | final diff = actual.difference |
| 1241 | final mins = diff.inMinutes; |
| 1242 | if (mins.abs() < |
| 1243 | if (mins < 0) return |
| 1244 | if (mins <= 15) return |
| 1245 | return ('${formatDuration( |
| 1246 | } |
| 1247 | |
| 1248 | Widget buildPhaseRow({ |
| 1249 | required String phase, |
| 1250 | required IconData icon, |
| 1251 | required String timezone, |
| 1252 | required DateTime? scheduled, |
| 1253 | required DateTime? estimated, |
| 1254 | required DateTime? actual, |
| 1255 | }) { |
| 1256 | if (scheduled == null && estimated == |
| 1257 | return const SizedBox |
| 1258 | } |
| 1259 | |
| 1260 | // Determine the best "resolved" time and its status |
| 1261 | final resolvedTime = actual ?? estimated; |
| 1262 | final (deltaLabel, deltaColor) = scheduled != |
| 1263 | ? getTimeDelta(scheduled, resolvedTime) |
| 1264 | : ('', Colors. |
| 1265 | |
| 1266 | return Padding( |
| 1267 | padding: const EdgeInsets. |
| 1268 | child: Column( |
| 1269 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1270 | children: [ |
| 1271 | Row( |
| 1272 | children: [ |
| 1273 | Icon(icon, size: 16 |
| 1274 | const SizedBox(width: |
| 1275 | Text( |
| 1276 | phase, |
| 1277 | style: const TextStyle( |
| 1278 | fontWeight: FontWeight.w700, |
| 1279 | fontSize: 13, |
| 1280 | ), |
| 1281 | ), |
| 1282 | if (deltaLabel.isNotEmpty) ...[ |
| 1283 | const Spacer(), |
| 1284 | Container( |
| 1285 | padding: |
| 1286 | const EdgeInsets.symmetric |
| 1287 | decoration: BoxDecoration( |
| 1288 | color: deltaColor.withOpacity(0.12) |
| 1289 | borderRadius: BorderRadius.circular( |
| 1290 | ), |
| 1291 | child: Text( |
| 1292 | deltaLabel, |
| 1293 | style: TextStyle( |
| 1294 | color: deltaColor, |
| 1295 | fontSize: 11, |
| 1296 | fontWeight: FontWeight.w700, |
| 1297 | ), |
| 1298 | ), |
| 1299 | ), |
| 1300 | ], |
| 1301 | ], |
| 1302 | ), |
| 1303 | const SizedBox(height: |
| 1304 | Padding( |
| 1305 | padding: const EdgeInsets. |
| 1306 | child: Table( |
| 1307 | columnWidths: const { |
| 1308 | 0: FlexColumnWidth(1) |
| 1309 | 1: FlexColumnWidth(2) |
| 1310 | }, |
| 1311 | children: [ |
| 1312 | if (scheduled != null) |
| 1313 | _buildTimeRow( |
| 1314 | 'Scheduled', |
| 1315 | formatDateTimeInTz(scheduled, timezone), |
| 1316 | Colors.grey.shade600, |
| 1317 | theme), |
| 1318 | if (estimated != null) |
| 1319 | _buildTimeRow( |
| 1320 | 'Estimated', |
| 1321 | formatDateTimeInTz(estimated, timezone), |
| 1322 | actual == null ? deltaColor : Colors |
| 1323 | theme, |
| 1324 | ), |
| 1325 | if (actual != null) |
| 1326 | _buildTimeRow( |
| 1327 | 'Actual', |
| 1328 | formatDateTimeInTz(actual, timezone), |
| 1329 | deltaColor, |
| 1330 | theme, |
| 1331 | bold: true, |
| 1332 | ), |
| 1333 | ], |
| 1334 | ), |
| 1335 | ), |
| 1336 | ], |
| 1337 | ), |
| 1338 | ); |
| 1339 | } |
| 1340 | |
| 1341 | return Column( |
| 1342 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1343 | children: [ |
| 1344 | const Gap(8) |
| 1345 | Text( |
| 1346 | 'Flight Timetable', |
| 1347 | style: TextStyle( |
| 1348 | fontSize: 16, |
| 1349 | fontWeight: FontWeight.bold, |
| 1350 | ), |
| 1351 | ), |
| 1352 | Gap(4), |
| 1353 | Container( |
| 1354 | padding: const EdgeInsets. |
| 1355 | decoration: BoxDecoration( |
| 1356 | borderRadius: BorderRadius.circular( |
| 1357 | border: Border.all( |
| 1358 | color: theme.dividerColor.withOpacity( |
| 1359 | ), |
| 1360 | ), |
| 1361 | child: Column( |
| 1362 | children: [ |
| 1363 | buildPhaseRow( |
| 1364 | phase: 'Gate Departure', |
| 1365 | icon: Icons.door_sliding_outlined, |
| 1366 | timezone: depTz, |
| 1367 | scheduled: fa.scheduledOut, |
| 1368 | estimated: fa.estimatedOut, |
| 1369 | actual: fa.actualOut, |
| 1370 | ), |
| 1371 | buildPhaseRow( |
| 1372 | phase: 'Takeoff', |
| 1373 | icon: Icons.flight_takeoff, |
| 1374 | timezone: depTz, |
| 1375 | scheduled: fa.scheduledOff, |
| 1376 | estimated: fa.estimatedOff, |
| 1377 | actual: fa.actualOff, |
| 1378 | ), |
| 1379 | buildPhaseRow( |
| 1380 | phase: 'Landing', |
| 1381 | icon: Icons.flight_land, |
| 1382 | timezone: arrTz, |
| 1383 | scheduled: fa.scheduledOn, |
| 1384 | estimated: fa.estimatedOn, |
| 1385 | actual: fa.actualOn, |
| 1386 | ), |
| 1387 | buildPhaseRow( |
| 1388 | phase: 'Gate Arrival', |
| 1389 | icon: Icons.door_front_door_outlined, |
| 1390 | timezone: arrTz, |
| 1391 | scheduled: fa.scheduledIn, |
| 1392 | estimated: fa.estimatedIn, |
| 1393 | actual: fa.actualIn, |
| 1394 | ), |
| 1395 | ], |
| 1396 | ), |
| 1397 | ), |
| 1398 | ], |
| 1399 | ); |
| 1400 | } |
| 1401 | |
| 1402 | TableRow _buildTimeRow( |
| 1403 | String label, String time |
| 1404 | {bool bold = false}) { |
| 1405 | return TableRow( |
| 1406 | children: [ |
| 1407 | Padding( |
| 1408 | padding: const EdgeInsets. |
| 1409 | child: Text( |
| 1410 | label, |
| 1411 | style: TextStyle( |
| 1412 | fontSize: 12, |
| 1413 | color: theme.textTheme.bodySmall?.color |
| 1414 | ), |
| 1415 | ), |
| 1416 | ), |
| 1417 | Padding( |
| 1418 | padding: const EdgeInsets. |
| 1419 | child: Text( |
| 1420 | time, |
| 1421 | style: TextStyle( |
| 1422 | fontSize: 12, |
| 1423 | fontWeight: bold ? FontWeight.w700 |
| 1424 | color: color, |
| 1425 | ), |
| 1426 | ), |
| 1427 | ), |
| 1428 | ], |
| 1429 | ); |
| 1430 | } |
| 1431 | |
| 1432 | Widget _buildTimezonePill({ |
| 1433 | required BuildContext context, |
| 1434 | required RouteInfoEntity arrival, |
| 1435 | required RouteInfoEntity departure, |
| 1436 | }) { |
| 1437 | final arrivalTime = _resolveEventTime(arrival) |
| 1438 | if (arrivalTime == null) { |
| 1439 | return const SizedBox |
| 1440 | } |
| 1441 | |
| 1442 | final use24Hrs = MediaQuery. |
| 1443 | final arrivalTz = _sanitizeTimezone(arrival |
| 1444 | final departureTz = _sanitizeTimezone(departure |
| 1445 | final arrivalLocal = |
| 1446 | arrivalTime.toTimezoneString(arrivalTz, use24Hrs: use24Hrs) |
| 1447 | final arrivalInDeparture = |
| 1448 | arrivalTime.toTimezoneString(departureTz, use24Hrs: use24Hrs) |
| 1449 | final diff = getTimezoneDifference(arrivalTz |
| 1450 | |
| 1451 | final arrivalLabel = _formatAirportLabel(arrival |
| 1452 | final departureLabel = _formatAirportLabel(departure |
| 1453 | |
| 1454 | final diffTitle = _formatDiffTitle(diff) |
| 1455 | final subtitle = diff == 'same timezone' |
| 1456 | ? "$arrivalLocal arrival in $ |
| 1457 | : "$arrivalLocal arrival in $ |
| 1458 | |
| 1459 | final theme = Theme. |
| 1460 | |
| 1461 | return Container( |
| 1462 | padding: const EdgeInsets. |
| 1463 | decoration: BoxDecoration( |
| 1464 | borderRadius: BorderRadius.circular( |
| 1465 | border: Border.all( |
| 1466 | color: theme.dividerColor.withOpacity( |
| 1467 | ), |
| 1468 | ), |
| 1469 | child: Row( |
| 1470 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1471 | children: [ |
| 1472 | Container( |
| 1473 | width: 32, |
| 1474 | height: 32, |
| 1475 | decoration: BoxDecoration( |
| 1476 | color: theme.colorScheme.primary.withOpacity |
| 1477 | borderRadius: BorderRadius.circular( |
| 1478 | ), |
| 1479 | child: Icon( |
| 1480 | MdiIcons.clockOutline, |
| 1481 | size: 18, |
| 1482 | ), |
| 1483 | ), |
| 1484 | const SizedBox(width: |
| 1485 | Expanded( |
| 1486 | child: Column( |
| 1487 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1488 | spacing: 4, |
| 1489 | children: [ |
| 1490 | Text( |
| 1491 | diffTitle, |
| 1492 | style: const TextStyle( |
| 1493 | fontWeight: FontWeight.w700, |
| 1494 | ), |
| 1495 | ), |
| 1496 | Text( |
| 1497 | subtitle, |
| 1498 | style: TextStyle( |
| 1499 | color: theme.textTheme.bodySmall?.color |
| 1500 | theme.brightness == Brightness.dark |
| 1501 | ), |
| 1502 | ), |
| 1503 | ], |
| 1504 | ), |
| 1505 | ), |
| 1506 | ], |
| 1507 | ), |
| 1508 | ); |
| 1509 | } |
| 1510 | |
| 1511 | Widget _buildCodesharePill( |
| 1512 | BuildContext context, |
| 1513 | List<String> codeshares, |
| 1514 | ) { |
| 1515 | final theme = Theme. |
| 1516 | |
| 1517 | return Container( |
| 1518 | padding: const EdgeInsets. |
| 1519 | decoration: BoxDecoration( |
| 1520 | borderRadius: BorderRadius.circular( |
| 1521 | border: Border.all( |
| 1522 | color: theme.dividerColor.withOpacity( |
| 1523 | ), |
| 1524 | ), |
| 1525 | child: Row( |
| 1526 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1527 | children: [ |
| 1528 | Container( |
| 1529 | width: 32, |
| 1530 | height: 32, |
| 1531 | decoration: BoxDecoration( |
| 1532 | color: theme.colorScheme.secondary.withOpacity |
| 1533 | borderRadius: BorderRadius.circular( |
| 1534 | ), |
| 1535 | child: Icon( |
| 1536 | Icons.flight_takeoff, |
| 1537 | size: 18, |
| 1538 | color: theme.colorScheme.secondary, |
| 1539 | ), |
| 1540 | ), |
| 1541 | const SizedBox(width: |
| 1542 | Expanded( |
| 1543 | child: Column( |
| 1544 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1545 | spacing: 6, |
| 1546 | children: [ |
| 1547 | const Text( |
| 1548 | "Codeshare flight", |
| 1549 | style: TextStyle(fontWeight: FontWeight |
| 1550 | ), |
| 1551 | Text( |
| 1552 | "Also sold as ${codeshares.join( |
| 1553 | style: TextStyle( |
| 1554 | color: theme.textTheme.bodySmall?.color |
| 1555 | theme.brightness == Brightness.dark |
| 1556 | ), |
| 1557 | ), |
| 1558 | Text( |
| 1559 | "Same aircraft, different partner airline numbers.", |
| 1560 | style: TextStyle( |
| 1561 | fontSize: 12, |
| 1562 | color: theme.textTheme.bodySmall?.color |
| 1563 | theme.brightness == Brightness.dark |
| 1564 | ), |
| 1565 | ), |
| 1566 | ], |
| 1567 | ), |
| 1568 | ), |
| 1569 | ], |
| 1570 | ), |
| 1571 | ); |
| 1572 | } |
| 1573 | |
| 1574 | DateTime? _resolveEventTime(RouteInfoEntity |
| 1575 | final candidate = |
| 1576 | segment.revisedTime ?? segment.predictedTime ?? segment. |
| 1577 | try { |
| 1578 | return DateTime.parse |
| 1579 | } catch (_) { |
| 1580 | return null; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | String _formatAirportLabel(RouteAirportEntity |
| 1585 | final code = _airportCode(airport) |
| 1586 | if (code.isEmpty) return airport. |
| 1587 | return "${airport. |
| 1588 | } |
| 1589 | |
| 1590 | String _airportCode(RouteAirportEntity |
| 1591 | return airport.iata.isNotEmpty ? |
| 1592 | } |
| 1593 | |
| 1594 | String _sanitizeTimezone(String |
| 1595 | return timezone.trim(). |
| 1596 | } |
| 1597 | |
| 1598 | String _formatDiffTitle(String |
| 1599 | if (diff == 'same timezone') { |
| 1600 | return "No Timezone Change"; |
| 1601 | } |
| 1602 | final sign = diff.startsWith |
| 1603 | final remainder = diff.substring |
| 1604 | final normalized = remainder |
| 1605 | .replaceAll('hrs', 'Hours' |
| 1606 | .replaceAll('hr', 'Hour' |
| 1607 | .replaceAll('mins', 'Minutes' |
| 1608 | .replaceAll('min', 'Minute' |
| 1609 | return "$sign$ |
| 1610 | } |
| 1611 | |
| 1612 | String _airportCity(RouteAirportEntity |
| 1613 | return airport.municipalityName.isNotEmpty |
| 1614 | ? airport.municipalityName |
| 1615 | : airport.shortName ?? airport.name; |
| 1616 | } |
| 1617 | |
| 1618 | Duration? _calculateDelay(dynamic |
| 1619 | if (revisedTime != null && scheduledTime != |
| 1620 | return getDelay( |
| 1621 | DateTime.parse(scheduledTime.utc |
| 1622 | DateTime.parse(revisedTime.utc |
| 1623 | ); |
| 1624 | } |
| 1625 | return null; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | Duration getDelay(DateTime scheduled |
| 1630 | // Positive duration => delayed; negative duration => early; zero => on time |
| 1631 | return predicted.difference(scheduled); |
| 1632 | } |
| 1633 | |
| 1634 | DateTime convertToArrivalTimeZone( |
| 1635 | DateTime departureTime, |
| 1636 | String departureTimeZone, |
| 1637 | String arrivalTimeZone, |
| 1638 | ) { |
| 1639 | try { |
| 1640 | tz.initializeTimeZones(); |
| 1641 | } catch (e) { |
| 1642 | print(e); |
| 1643 | } |
| 1644 | // Initialize time zone locations |
| 1645 | final departureLocation = tz.getLocation |
| 1646 | final arrivalLocation = tz.getLocation |
| 1647 | |
| 1648 | // Convert departure time to TZDateTime in the departure location |
| 1649 | final departureTZTime = tz.TZDateTime |
| 1650 | |
| 1651 | // Convert the departure TZDateTime to the arrival time zone |
| 1652 | final arrivalTZTime = tz.TZDateTime |
| 1653 | |
| 1654 | return arrivalTZTime.toLocal(); |
| 1655 | } |
| 1656 | |
| 1657 | String formatAge(double age) { |
| 1658 | if (age == 0) return |
| 1659 | |
| 1660 | final years = age.floor |
| 1661 | final months = ((age - years) * |
| 1662 | |
| 1663 | final List<String |
| 1664 | |
| 1665 | if (years > 0) { |
| 1666 | parts.add('$years |
| 1667 | } |
| 1668 | |
| 1669 | if (months > 0) { |
| 1670 | parts.add('$months |
| 1671 | } |
| 1672 | |
| 1673 | return parts.join(' and ' |
| 1674 | } |
| 1675 | |
| 1676 | String formatDate(DateTime date) { |
| 1677 | return '${MONTHS[ |
| 1678 | } |
| 1679 | |
| 1680 | // ignore: unused_element |
| 1681 | class _ChartPosition { |
| 1682 | final double altitude; |
| 1683 | final double speed; |
| 1684 | final DateTime timestamp; |
| 1685 | |
| 1686 | _ChartPosition({ |
| 1687 | required this.altitude, |
| 1688 | required this.speed, |
| 1689 | required this.timestamp, |
| 1690 | }); |
| 1691 | } |