| 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 | ShareableFlightCard(info: widget.info), |
| 101 | ), |
| 102 | pixelRatio: 2.0, |
| 103 | delay: const Duration(milliseconds |
| 104 | ); |
| 105 | |
| 106 | if (imageBytes.isEmpty) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Save to temporary file |
| 111 | final tempDir = await getTemporaryDirectory |
| 112 | final file = File( |
| 113 | '${tempDir.path} |
| 114 | await file.writeAsBytes(imageBytes); |
| 115 | |
| 116 | // Share the file |
| 117 | await Share.shareXFiles( |
| 118 | [XFile(file.path)], |
| 119 | text: 'Check out my flight!', |
| 120 | ); |
| 121 | } catch (e) { |
| 122 | // Handle error silently or show a snackbar |
| 123 | if (mounted) { |
| 124 | ScaffoldMessenger.of(context).showSnackBar |
| 125 | SnackBar( |
| 126 | content: Text('Failed to share flight card: $e |
| 127 | ), |
| 128 | ); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | Widget _buildBanner( |
| 134 | BuildContext context, Color color |
| 135 | [String? subtitle]) { |
| 136 | return Container( |
| 137 | decoration: BoxDecoration( |
| 138 | color: color.withOpacity(0.08) |
| 139 | border: Border.all( |
| 140 | color: color.withOpacity(0.3) |
| 141 | ), |
| 142 | borderRadius: BorderRadius.circular( |
| 143 | ), |
| 144 | child: ListTile( |
| 145 | contentPadding: EdgeInsets.zero, |
| 146 | leading: Icon( |
| 147 | icon, |
| 148 | color: color, |
| 149 | ), |
| 150 | title: Text( |
| 151 | title, |
| 152 | style: const TextStyle( |
| 153 | fontWeight: FontWeight.w700, |
| 154 | ), |
| 155 | ), |
| 156 | subtitle: subtitle != null ? Text |
| 157 | ), |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | Widget _buildFlightStatusBanner(BuildContext |
| 162 | final fa = widget.info. |
| 163 | if (fa?.cancelled == true) { |
| 164 | return _buildBanner( |
| 165 | context, |
| 166 | Colors.red, |
| 167 | Icons.cancel, |
| 168 | 'This flight has been cancelled', |
| 169 | ); |
| 170 | } |
| 171 | if (fa?.diverted == true) { |
| 172 | return _buildBanner( |
| 173 | context, |
| 174 | Colors.amber, |
| 175 | Icons.warning_amber_rounded, |
| 176 | 'This flight has been diverted', |
| 177 | ); |
| 178 | } |
| 179 | return const SizedBox |
| 180 | } |
| 181 | |
| 182 | @override |
| 183 | Widget build(BuildContext |
| 184 | final arrival = widget.info. |
| 185 | final departure = widget.info. |
| 186 | return Padding( |
| 187 | padding: const EdgeInsets. |
| 188 | 4.0, |
| 189 | ), |
| 190 | child: Column( |
| 191 | crossAxisAlignment: CrossAxisAlignment.start, |
| 192 | spacing: 4, |
| 193 | children: [ |
| 194 | Row( |
| 195 | spacing: 8, |
| 196 | crossAxisAlignment: CrossAxisAlignment.center, |
| 197 | children: [ |
| 198 | CachedNetworkImage( |
| 199 | imageUrl: |
| 200 | "https://airlabs.co/img/airline/m/${widget.info. |
| 201 | width: 30, |
| 202 | height: 30, |
| 203 | errorWidget: (context, url, error) { |
| 204 | return SizedBox( |
| 205 | height: 0, |
| 206 | width: 0, |
| 207 | ); |
| 208 | }, |
| 209 | ), |
| 210 | Column( |
| 211 | spacing: 2, |
| 212 | crossAxisAlignment: CrossAxisAlignment.start, |
| 213 | children: [ |
| 214 | Row( |
| 215 | children: [ |
| 216 | Text( |
| 217 | "${departure.airport. |
| 218 | style: TextStyle( |
| 219 | fontWeight: FontWeight.bold, |
| 220 | fontSize: 16, |
| 221 | ), |
| 222 | ) |
| 223 | ], |
| 224 | ), |
| 225 | Row( |
| 226 | children: [ |
| 227 | Text( |
| 228 | formatDayAndMonth( |
| 229 | DateTime.parse( |
| 230 | (departure.revisedTime ?? departure.scheduledTime) |
| 231 | .utc), |
| 232 | ), |
| 233 | style: TextStyle( |
| 234 | fontWeight: FontWeight.bold, |
| 235 | color: Theme.of(context) |
| 236 | fontSize: 12, |
| 237 | ), |
| 238 | ), |
| 239 | Text( |
| 240 | " • ", |
| 241 | style: Theme.of(context) |
| 242 | fontSize: 12, |
| 243 | ), |
| 244 | ), |
| 245 | Text( |
| 246 | widget.info.flightNo, |
| 247 | style: TextStyle( |
| 248 | fontWeight: FontWeight.bold, |
| 249 | color: Theme.of(context) |
| 250 | fontSize: 12, |
| 251 | ), |
| 252 | ), |
| 253 | if (widget.info.flightAwareData?.identIata |
| 254 | Text( |
| 255 | " • ", |
| 256 | style: |
| 257 | Theme.of(context).textTheme |
| 258 | fontSize: 12, |
| 259 | ), |
| 260 | ), |
| 261 | Text( |
| 262 | widget.info.flightAwareData?.identIata ?? |
| 263 | style: TextStyle( |
| 264 | fontWeight: FontWeight.bold, |
| 265 | color: |
| 266 | Theme.of(context).textTheme |
| 267 | fontSize: 12, |
| 268 | ), |
| 269 | ), |
| 270 | ] |
| 271 | ], |
| 272 | ), |
| 273 | ], |
| 274 | ), |
| 275 | Spacer(), |
| 276 | Row( |
| 277 | spacing: 4, |
| 278 | children: [ |
| 279 | PopupMenuButton( |
| 280 | icon: Icon(Icons. |
| 281 | color: Colors.white, |
| 282 | itemBuilder: (context) => [ |
| 283 | if (widget.onRefreshTracking != null) |
| 284 | PopupMenuItem( |
| 285 | value: 'refresh_tracking', |
| 286 | child: Row( |
| 287 | children: [ |
| 288 | Icon( |
| 289 | Icons.refresh, |
| 290 | ), |
| 291 | SizedBox(width: 8), |
| 292 | Text('Refresh tracking'), |
| 293 | ], |
| 294 | ), |
| 295 | ), |
| 296 | PopupMenuItem( |
| 297 | value: 'refresh_flight_data', |
| 298 | child: Row( |
| 299 | children: [ |
| 300 | Icon( |
| 301 | Icons.refresh, |
| 302 | ), |
| 303 | SizedBox(width: 8), |
| 304 | Text('Refresh flight data'), |
| 305 | ], |
| 306 | ), |
| 307 | ), |
| 308 | PopupMenuItem( |
| 309 | value: 'send_to_wearable', |
| 310 | child: Row( |
| 311 | children: [ |
| 312 | Icon( |
| 313 | Icons.watch, |
| 314 | ), |
| 315 | SizedBox(width: 8), |
| 316 | Text('Send to wearable'), |
| 317 | ], |
| 318 | ), |
| 319 | ), |
| 320 | PopupMenuItem( |
| 321 | value: 'booking_details', |
| 322 | child: Row( |
| 323 | children: [ |
| 324 | Icon(Icons.confirmation_number), |
| 325 | SizedBox(width: 8), |
| 326 | Text('Booking details'), |
| 327 | ], |
| 328 | ), |
| 329 | ), |
| 330 | PopupMenuItem( |
| 331 | value: 'share', |
| 332 | child: Row( |
| 333 | children: [ |
| 334 | Icon(Icons.share), |
| 335 | SizedBox(width: 8), |
| 336 | Text('Share'), |
| 337 | ], |
| 338 | ), |
| 339 | ), |
| 340 | ], |
| 341 | onSelected: (value) async { |
| 342 | if (value == "refresh_tracking") { |
| 343 | widget.onRefreshTracking?.call(); |
| 344 | } else if (value == "send_to_wearable") { |
| 345 | final channel = MethodChannel( |
| 346 | final flightNumber = widget.info. |
| 347 | final flightNumberWithoutIata = flightNumber |
| 348 | .replaceFirst(widget.info.airline |
| 349 | final flightNumberWithoutIcao = flightNumber |
| 350 | .replaceFirst(widget.info.airline |
| 351 | |
| 352 | await channel.invokeMethod("registerFlight", |
| 353 | "id": widget.info.id, |
| 354 | "date": DateTime.parse |
| 355 | departure.scheduledTime) |
| 356 | .utc) |
| 357 | .toIso8601String(), |
| 358 | "flightIata": (flightNumberWithoutIcao == |
| 359 | flightNumberWithoutIata) |
| 360 | ? "${widget.info |
| 361 | : flightNumber, |
| 362 | "flightIcao": (flightNumberWithoutIcao == |
| 363 | flightNumberWithoutIata) |
| 364 | ? "${widget.info |
| 365 | : flightNumber, |
| 366 | "origin": departure.airport.municipalityName, |
| 367 | "destination": |
| 368 | widget.info.arrival.airport.municipalityName, |
| 369 | }); |
| 370 | } |
| 371 | if (value == "refresh_flight_data") { |
| 372 | widget.onRefreshFlightData?.call(); |
| 373 | } |
| 374 | if (value == "share") { |
| 375 | _shareFlightCard(); |
| 376 | } |
| 377 | if (value == "booking_details") { |
| 378 | widget.onBookingDetails?.call(); |
| 379 | } |
| 380 | }, |
| 381 | ), |
| 382 | SizedBox( |
| 383 | width: 24, |
| 384 | child: IconButton( |
| 385 | onPressed: widget.onClose, |
| 386 | padding: EdgeInsets.zero, |
| 387 | icon: Icon(Icons. |
| 388 | ), |
| 389 | ), |
| 390 | ], |
| 391 | ) |
| 392 | ], |
| 393 | ), |
| 394 | Divider(), |
| 395 | if ((widget.info.flightAwareData?.cancelled |
| 396 | (widget.info.flightAwareData?.diverted == |
| 397 | _buildFlightStatusBanner(context), |
| 398 | Gap(8), |
| 399 | ], |
| 400 | Column( |
| 401 | crossAxisAlignment: CrossAxisAlignment.start, |
| 402 | children: [ |
| 403 | Text( |
| 404 | getTimeMessage( |
| 405 | DateTime.parse( |
| 406 | (departure.revisedTime ?? |
| 407 | departure.predictedTime ?? |
| 408 | departure.scheduledTime) |
| 409 | .utc, |
| 410 | ), |
| 411 | DateTime.parse( |
| 412 | (arrival.revisedTime ?? |
| 413 | arrival.predictedTime ?? |
| 414 | arrival.scheduledTime) |
| 415 | .utc, |
| 416 | ), |
| 417 | ), |
| 418 | style: TextStyle( |
| 419 | fontSize: 18, |
| 420 | fontWeight: FontWeight.bold, |
| 421 | ), |
| 422 | ), |
| 423 | ], |
| 424 | ), |
| 425 | FlightRouteInfo( |
| 426 | arrivalCode: arrival.airport.iata, |
| 427 | arrivalDelay: _calculateDelay(arrival.revisedTime, |
| 428 | arrival.predictedTime ?? arrival.scheduledTime), |
| 429 | arrivalName: arrival.airport.name, |
| 430 | arrivalSubtitle: "", |
| 431 | arrivalTerminal: arrival.terminal, |
| 432 | arrivalTime: DateTime.parse((arrival |
| 433 | arrival.predictedTime ?? |
| 434 | arrival.scheduledTime) |
| 435 | .utc), |
| 436 | arrivalTimezone: arrival.airport.timeZone, |
| 437 | departureCode: departure.airport.iata, |
| 438 | departureDelay: _calculateDelay(departure.revisedTime, |
| 439 | departure.predictedTime ?? departure.scheduledTime), |
| 440 | departureName: departure.airport.name, |
| 441 | departureSubtitle: "", |
| 442 | departureTerminal: departure.terminal, |
| 443 | departureTime: DateTime.parse((departure |
| 444 | departure.predictedTime ?? |
| 445 | departure.scheduledTime) |
| 446 | .utc), |
| 447 | departureTimezone: departure.airport.timeZone, |
| 448 | statusColor: Colors.green, |
| 449 | use24Hrs: MediaQuery.of(context) |
| 450 | arrivalGate: |
| 451 | widget.info.flightAwareData?.gateDestination ?? arrival |
| 452 | departureGate: |
| 453 | widget.info.flightAwareData?.gateOrigin ?? departure |
| 454 | arrivalBaggage: widget.info.flightAwareData?.baggageClaim |
| 455 | arrival.baggageBelt, |
| 456 | distanceKm: widget.info.greatCircleDistance.km != |
| 457 | ? double.tryParse(widget |
| 458 | : null, |
| 459 | flightAwareDataEntity: widget.info.flightAwareData, |
| 460 | ), |
| 461 | Gap(8), |
| 462 | Text( |
| 463 | "Worth Noting", |
| 464 | style: TextStyle( |
| 465 | fontSize: 16, |
| 466 | fontWeight: FontWeight.bold, |
| 467 | ), |
| 468 | ), |
| 469 | Container( |
| 470 | decoration: BoxDecoration( |
| 471 | border: Border.all( |
| 472 | color: Colors.grey.shade300 |
| 473 | ), |
| 474 | borderRadius: BorderRadius.circular( |
| 475 | 5, |
| 476 | ), |
| 477 | ), |
| 478 | child: Padding( |
| 479 | padding: const EdgeInsets. |
| 480 | 8.0, |
| 481 | ).copyWith( |
| 482 | top: 0, |
| 483 | bottom: 0, |
| 484 | ), |
| 485 | child: Column( |
| 486 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 487 | spacing: 8, |
| 488 | children: [ |
| 489 | ListTile( |
| 490 | contentPadding: EdgeInsets.zero, |
| 491 | leading: Icon( |
| 492 | MdiIcons.clock, |
| 493 | ), |
| 494 | title: Builder(builder: (context) { |
| 495 | final diff = getTimezoneDifference( |
| 496 | widget.info.arrival.airport.timeZone, |
| 497 | departure.airport.timeZone, |
| 498 | ); |
| 499 | return Text( |
| 500 | diff == "same timezone" |
| 501 | ? "No timezone difference" |
| 502 | : "Timezone difference of $diff", |
| 503 | style: TextStyle( |
| 504 | fontWeight: FontWeight.w600, |
| 505 | ), |
| 506 | ); |
| 507 | }), |
| 508 | ), |
| 509 | if (widget.info.flightAwareData?.codesharesIcao |
| 510 | widget.info.flightAwareData!.codesharesIcao!. |
| 511 | ListTile( |
| 512 | contentPadding: EdgeInsets.zero, |
| 513 | title: Text( |
| 514 | "Operated as ${widget.info. |
| 515 | style: TextStyle( |
| 516 | fontWeight: FontWeight.w600, |
| 517 | ), |
| 518 | ), |
| 519 | leading: Icon(Icons. |
| 520 | ) |
| 521 | ], |
| 522 | ), |
| 523 | ), |
| 524 | ), |
| 525 | // Booking Details Section |
| 526 | if (widget.info.bookings != null |
| 527 | widget.info.bookings!.isNotEmpty) ...[ |
| 528 | Gap(8), |
| 529 | Text( |
| 530 | "Booking Details", |
| 531 | style: TextStyle( |
| 532 | fontSize: 16, |
| 533 | fontWeight: FontWeight.bold, |
| 534 | ), |
| 535 | ), |
| 536 | Container( |
| 537 | decoration: BoxDecoration( |
| 538 | border: Border.all( |
| 539 | color: Colors.grey.shade300 |
| 540 | ), |
| 541 | borderRadius: BorderRadius.circular( |
| 542 | ), |
| 543 | child: Padding( |
| 544 | padding: const EdgeInsets. |
| 545 | top: 0, |
| 546 | bottom: 0, |
| 547 | ), |
| 548 | child: Column( |
| 549 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 550 | spacing: 8, |
| 551 | children: [ |
| 552 | Builder(builder: (context) { |
| 553 | final booking = widget.info. |
| 554 | |
| 555 | // Extract string values from JsonObject |
| 556 | String? _extractString(dynamic |
| 557 | if (jsonObject == null) return |
| 558 | if (jsonObject is String) return |
| 559 | try { |
| 560 | final value = (jsonObject as dynamic |
| 561 | if (value is String) return |
| 562 | return value?.toString() |
| 563 | } catch (e) { |
| 564 | return jsonObject.toString(); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | final bookingCode = _extractString(booking |
| 569 | final seatNumber = _extractString(booking |
| 570 | final seatingClass = booking.seatingClass? |
| 571 | final seatType = booking.seatType? |
| 572 | final reason = booking.reason? |
| 573 | |
| 574 | return Column( |
| 575 | crossAxisAlignment: CrossAxisAlignment.start, |
| 576 | children: [ |
| 577 | if (bookingCode != null && bookingCode. |
| 578 | ListTile( |
| 579 | contentPadding: EdgeInsets.zero, |
| 580 | leading: Icon(Icons. |
| 581 | title: Text( |
| 582 | 'Booking Code: $bookingCode', |
| 583 | style: TextStyle( |
| 584 | fontWeight: FontWeight.w600, |
| 585 | ), |
| 586 | ), |
| 587 | ), |
| 588 | if (seatNumber != null && seatNumber. |
| 589 | ListTile( |
| 590 | contentPadding: EdgeInsets.zero, |
| 591 | leading: Icon(Icons. |
| 592 | title: Text( |
| 593 | 'Seat: $seatNumber${seatType != |
| 594 | style: TextStyle( |
| 595 | fontWeight: FontWeight.w600, |
| 596 | ), |
| 597 | ), |
| 598 | ), |
| 599 | if (seatingClass != null) |
| 600 | ListTile( |
| 601 | contentPadding: EdgeInsets.zero, |
| 602 | leading: Icon(Icons. |
| 603 | title: Text( |
| 604 | 'Class: ${_formatEnumName(seatingClass)} |
| 605 | style: TextStyle( |
| 606 | fontWeight: FontWeight.w600, |
| 607 | ), |
| 608 | ), |
| 609 | ), |
| 610 | if (reason != null) |
| 611 | ListTile( |
| 612 | contentPadding: EdgeInsets.zero, |
| 613 | leading: Icon(Icons. |
| 614 | title: Text( |
| 615 | 'Reason: ${_formatEnumName(reason)} |
| 616 | style: TextStyle( |
| 617 | fontWeight: FontWeight.w600, |
| 618 | ), |
| 619 | ), |
| 620 | ), |
| 621 | ], |
| 622 | ); |
| 623 | }), |
| 624 | ], |
| 625 | ), |
| 626 | ), |
| 627 | ), |
| 628 | ], |
| 629 | Gap(8), |
| 630 | if (widget.info.aircraft?.model |
| 631 | Text( |
| 632 | widget.info.aircraft!.model!, |
| 633 | style: TextStyle( |
| 634 | fontSize: 16, |
| 635 | fontWeight: FontWeight.bold, |
| 636 | ), |
| 637 | ), |
| 638 | if (widget.info.aircraft?.registration |
| 639 | Container( |
| 640 | decoration: BoxDecoration( |
| 641 | border: Border.all( |
| 642 | color: Colors.grey.shade300 |
| 643 | ), |
| 644 | borderRadius: BorderRadius.circular( |
| 645 | 5, |
| 646 | ), |
| 647 | ), |
| 648 | child: Padding( |
| 649 | padding: const EdgeInsets. |
| 650 | 8.0, |
| 651 | ), |
| 652 | child: Column( |
| 653 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 654 | spacing: 8, |
| 655 | children: [ |
| 656 | if (widget.info.aircraft!.image |
| 657 | ClipRRect( |
| 658 | borderRadius: BorderRadius.circular( |
| 659 | 5, |
| 660 | ), |
| 661 | child: CachedNetworkImage( |
| 662 | imageUrl: widget.info.aircraft!.image |
| 663 | ), |
| 664 | ), |
| 665 | Wrap( |
| 666 | spacing: 10, |
| 667 | alignment: WrapAlignment.spaceBetween, |
| 668 | runSpacing: 10, |
| 669 | children: [ |
| 670 | if (widget.info.aircraft?.age |
| 671 | Column( |
| 672 | crossAxisAlignment: CrossAxisAlignment.start, |
| 673 | children: [ |
| 674 | Text( |
| 675 | "Age", |
| 676 | style: TextStyle( |
| 677 | fontWeight: FontWeight.bold, |
| 678 | color: Colors |
| 679 | .grey[700], // Adjust color as needed |
| 680 | ), |
| 681 | ), |
| 682 | Text( |
| 683 | formatAge( |
| 684 | double.parse( |
| 685 | widget.info.aircraft!.age!, |
| 686 | ), |
| 687 | ), |
| 688 | style: TextStyle( |
| 689 | fontWeight: FontWeight.normal, |
| 690 | color: Colors.black, |
| 691 | ), |
| 692 | ), |
| 693 | ], |
| 694 | ), |
| 695 | if (widget.info.aircraft?.deliveryDate |
| 696 | Column( |
| 697 | crossAxisAlignment: CrossAxisAlignment.end, |
| 698 | children: [ |
| 699 | Text( |
| 700 | "Delivery Date", |
| 701 | style: TextStyle( |
| 702 | fontWeight: FontWeight.bold, |
| 703 | color: Colors |
| 704 | .grey[700], // Adjust color as needed |
| 705 | ), |
| 706 | ), |
| 707 | Text( |
| 708 | formatDate(DateTime.parse |
| 709 | widget.info.aircraft!.deliveryDate!)) |
| 710 | style: TextStyle( |
| 711 | fontWeight: FontWeight.normal, |
| 712 | color: Colors.black, |
| 713 | ), |
| 714 | ), |
| 715 | ], |
| 716 | ), |
| 717 | if (widget.info.aircraft?.registration |
| 718 | Column( |
| 719 | crossAxisAlignment: CrossAxisAlignment.start, |
| 720 | children: [ |
| 721 | Text( |
| 722 | "Registration", |
| 723 | style: TextStyle( |
| 724 | fontWeight: FontWeight.bold, |
| 725 | color: Colors |
| 726 | .grey[700], // Adjust color as needed |
| 727 | ), |
| 728 | ), |
| 729 | Text( |
| 730 | widget.info.aircraft!.registration!, |
| 731 | style: TextStyle( |
| 732 | fontWeight: FontWeight.normal, |
| 733 | color: Colors.black, |
| 734 | ), |
| 735 | ), |
| 736 | const SizedBox(height: |
| 737 | Builder(builder: (context) { |
| 738 | final payload = |
| 739 | (widget.info.aircraft?.payload)?. |
| 740 | final entries = payload == null |
| 741 | ? const <MapEntry |
| 742 | : payload.entries |
| 743 | .where((e) => |
| 744 | e.value is String || |
| 745 | e.value is num || |
| 746 | e.value is bool) |
| 747 | .where((e) => |
| 748 | e.key != 'id' && |
| 749 | e.key != 'numRegistrations') |
| 750 | .toList(); |
| 751 | if (entries.isEmpty) { |
| 752 | return const SizedBox |
| 753 | } |
| 754 | return Column( |
| 755 | crossAxisAlignment: CrossAxisAlignment.start, |
| 756 | children: [ |
| 757 | TextButton( |
| 758 | onPressed: () { |
| 759 | setState(() { |
| 760 | _showAircraftPayload = |
| 761 | !_showAircraftPayload; |
| 762 | }); |
| 763 | }, |
| 764 | style: TextButton.styleFrom( |
| 765 | padding: EdgeInsets.zero, |
| 766 | minimumSize: const Size( |
| 767 | tapTargetSize: |
| 768 | MaterialTapTargetSize.shrinkWrap, |
| 769 | ), |
| 770 | child: Row( |
| 771 | mainAxisSize: MainAxisSize.min, |
| 772 | children: [ |
| 773 | Text( |
| 774 | _showAircraftPayload |
| 775 | ? "Hide more info" |
| 776 | : "Show more info", |
| 777 | style: TextStyle( |
| 778 | fontWeight: FontWeight.w600, |
| 779 | ), |
| 780 | ), |
| 781 | const SizedBox(width: |
| 782 | Icon( |
| 783 | _showAircraftPayload |
| 784 | ? Icons.expand_less |
| 785 | : Icons.expand_more, |
| 786 | size: 18), |
| 787 | ], |
| 788 | ), |
| 789 | ), |
| 790 | if (_showAircraftPayload) |
| 791 | Padding( |
| 792 | padding: |
| 793 | const EdgeInsets.only |
| 794 | child: Column( |
| 795 | crossAxisAlignment: |
| 796 | CrossAxisAlignment.start, |
| 797 | children: entries.map((e) { |
| 798 | final coerced = |
| 799 | _coerceToBool(e.value); |
| 800 | if (coerced != null) { |
| 801 | final boolColor = coerced |
| 802 | ? Colors.green |
| 803 | : Colors.red; |
| 804 | final label = |
| 805 | coerced ? 'Yes' : 'No'; |
| 806 | return Padding( |
| 807 | padding: |
| 808 | const EdgeInsets.symmetric |
| 809 | vertical: 1.0), |
| 810 | child: Row( |
| 811 | mainAxisAlignment: |
| 812 | MainAxisAlignment |
| 813 | .spaceBetween, |
| 814 | children: [ |
| 815 | Expanded( |
| 816 | child: Text( |
| 817 | _formatKey(e.key), |
| 818 | style: TextStyle( |
| 819 | color: |
| 820 | Colors.grey[700], |
| 821 | fontWeight: |
| 822 | FontWeight.w600, |
| 823 | ), |
| 824 | ), |
| 825 | ), |
| 826 | const SizedBox(width: |
| 827 | Row( |
| 828 | mainAxisSize: |
| 829 | MainAxisSize.min, |
| 830 | children: [ |
| 831 | Container( |
| 832 | width: 10, |
| 833 | height: 10, |
| 834 | decoration: |
| 835 | BoxDecoration( |
| 836 | color: boolColor, |
| 837 | shape: |
| 838 | BoxShape.circle, |
| 839 | ), |
| 840 | ), |
| 841 | const SizedBox( |
| 842 | width: 4), |
| 843 | Text( |
| 844 | label, |
| 845 | style: TextStyle( |
| 846 | color: boolColor, |
| 847 | fontSize: 12, |
| 848 | fontWeight: |
| 849 | FontWeight.w600, |
| 850 | ), |
| 851 | ), |
| 852 | ], |
| 853 | ), |
| 854 | ], |
| 855 | ), |
| 856 | ); |
| 857 | } |
| 858 | return Padding( |
| 859 | padding: |
| 860 | const EdgeInsets.symmetric |
| 861 | vertical: 1.0), |
| 862 | child: Row( |
| 863 | mainAxisAlignment: |
| 864 | MainAxisAlignment |
| 865 | .spaceBetween, |
| 866 | children: [ |
| 867 | Expanded( |
| 868 | child: Text( |
| 869 | _formatKey(e.key), |
| 870 | style: TextStyle( |
| 871 | color: Colors.grey[700] |
| 872 | fontWeight: |
| 873 | FontWeight.w600, |
| 874 | ), |
| 875 | ), |
| 876 | ), |
| 877 | const SizedBox(width: |
| 878 | Flexible( |
| 879 | child: Text( |
| 880 | _formatPayloadValue( |
| 881 | e.value), |
| 882 | textAlign: |
| 883 | TextAlign.right, |
| 884 | overflow: |
| 885 | TextOverflow.ellipsis, |
| 886 | ), |
| 887 | ), |
| 888 | ], |
| 889 | ), |
| 890 | ); |
| 891 | }).toList(), |
| 892 | ), |
| 893 | ), |
| 894 | ], |
| 895 | ); |
| 896 | }), |
| 897 | ], |
| 898 | ), |
| 899 | ], |
| 900 | ) |
| 901 | ], |
| 902 | ), |
| 903 | ), |
| 904 | ), |
| 905 | ], |
| 906 | ), |
| 907 | ); |
| 908 | } |
| 909 | |
| 910 | Duration? _calculateDelay(dynamic |
| 911 | if (revisedTime != null && scheduledTime != |
| 912 | return getDelay( |
| 913 | DateTime.parse(scheduledTime.utc |
| 914 | DateTime.parse(revisedTime.utc |
| 915 | ); |
| 916 | } |
| 917 | return null; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | Duration getDelay(DateTime scheduled |
| 922 | // Positive duration => delayed; negative duration => early; zero => on time |
| 923 | return predicted.difference(scheduled); |
| 924 | } |
| 925 | |
| 926 | DateTime convertToArrivalTimeZone( |
| 927 | DateTime departureTime, |
| 928 | String departureTimeZone, |
| 929 | String arrivalTimeZone, |
| 930 | ) { |
| 931 | try { |
| 932 | tz.initializeTimeZones(); |
| 933 | } catch (e) { |
| 934 | print(e); |
| 935 | } |
| 936 | // Initialize time zone locations |
| 937 | final departureLocation = tz.getLocation |
| 938 | final arrivalLocation = tz.getLocation |
| 939 | |
| 940 | // Convert departure time to TZDateTime in the departure location |
| 941 | final departureTZTime = tz.TZDateTime |
| 942 | |
| 943 | // Convert the departure TZDateTime to the arrival time zone |
| 944 | final arrivalTZTime = tz.TZDateTime |
| 945 | |
| 946 | return arrivalTZTime.toLocal(); |
| 947 | } |
| 948 | |
| 949 | String formatAge(double age) { |
| 950 | if (age == 0) return |
| 951 | |
| 952 | final years = age.floor |
| 953 | final months = ((age - years) * |
| 954 | |
| 955 | final List<String |
| 956 | |
| 957 | if (years > 0) { |
| 958 | parts.add('$years |
| 959 | } |
| 960 | |
| 961 | if (months > 0) { |
| 962 | parts.add('$months |
| 963 | } |
| 964 | |
| 965 | return parts.join(' and ' |
| 966 | } |
| 967 | |
| 968 | String formatDate(DateTime date) { |
| 969 | return '${MONTHS[ |
| 970 | } |
| 971 | |
| 972 | // Helper class for chart data |
| 973 | class _ChartPosition { |
| 974 | final double altitude; |
| 975 | final double speed; |
| 976 | final DateTime timestamp; |
| 977 | |
| 978 | _ChartPosition({ |
| 979 | required this.altitude, |
| 980 | required this.speed, |
| 981 | required this.timestamp, |
| 982 | }); |
| 983 | } |