| 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 | final codeshares = widget.info. |
| 187 | ?.where((code) => code. |
| 188 | .map((code) => code.trim |
| 189 | .toList(growable: false) |
| 190 | const <String>[] |
| 191 | return Padding( |
| 192 | padding: const EdgeInsets. |
| 193 | 4.0, |
| 194 | ), |
| 195 | child: Column( |
| 196 | crossAxisAlignment: CrossAxisAlignment.start, |
| 197 | spacing: 4, |
| 198 | children: [ |
| 199 | Row( |
| 200 | spacing: 8, |
| 201 | crossAxisAlignment: CrossAxisAlignment.center, |
| 202 | children: [ |
| 203 | CachedNetworkImage( |
| 204 | imageUrl: |
| 205 | "https://airlabs.co/img/airline/m/${widget.info. |
| 206 | width: 30, |
| 207 | height: 30, |
| 208 | errorWidget: (context, url, error) { |
| 209 | return SizedBox( |
| 210 | height: 0, |
| 211 | width: 0, |
| 212 | ); |
| 213 | }, |
| 214 | ), |
| 215 | Column( |
| 216 | spacing: 2, |
| 217 | crossAxisAlignment: CrossAxisAlignment.start, |
| 218 | children: [ |
| 219 | Row( |
| 220 | children: [ |
| 221 | Text( |
| 222 | "${departure.airport. |
| 223 | style: TextStyle( |
| 224 | fontWeight: FontWeight.bold, |
| 225 | fontSize: 16, |
| 226 | ), |
| 227 | ) |
| 228 | ], |
| 229 | ), |
| 230 | Row( |
| 231 | children: [ |
| 232 | Text( |
| 233 | formatDayAndMonth( |
| 234 | DateTime.parse( |
| 235 | (departure.revisedTime ?? departure.scheduledTime) |
| 236 | .utc), |
| 237 | ), |
| 238 | style: TextStyle( |
| 239 | fontWeight: FontWeight.bold, |
| 240 | color: Theme.of(context) |
| 241 | fontSize: 12, |
| 242 | ), |
| 243 | ), |
| 244 | Text( |
| 245 | " • ", |
| 246 | style: Theme.of(context) |
| 247 | fontSize: 12, |
| 248 | ), |
| 249 | ), |
| 250 | Text( |
| 251 | widget.info.flightNo, |
| 252 | style: TextStyle( |
| 253 | fontWeight: FontWeight.bold, |
| 254 | color: Theme.of(context) |
| 255 | fontSize: 12, |
| 256 | ), |
| 257 | ), |
| 258 | if (widget.info.flightAwareData?.identIata |
| 259 | Text( |
| 260 | " • ", |
| 261 | style: |
| 262 | Theme.of(context).textTheme |
| 263 | fontSize: 12, |
| 264 | ), |
| 265 | ), |
| 266 | Text( |
| 267 | widget.info.flightAwareData?.identIata ?? |
| 268 | style: TextStyle( |
| 269 | fontWeight: FontWeight.bold, |
| 270 | color: |
| 271 | Theme.of(context).textTheme |
| 272 | fontSize: 12, |
| 273 | ), |
| 274 | ), |
| 275 | ] |
| 276 | ], |
| 277 | ), |
| 278 | ], |
| 279 | ), |
| 280 | Spacer(), |
| 281 | Row( |
| 282 | spacing: 4, |
| 283 | children: [ |
| 284 | PopupMenuButton( |
| 285 | icon: Icon(Icons. |
| 286 | color: Colors.white, |
| 287 | itemBuilder: (context) => [ |
| 288 | if (widget.onRefreshTracking != null) |
| 289 | PopupMenuItem( |
| 290 | value: 'refresh_tracking', |
| 291 | child: Row( |
| 292 | children: [ |
| 293 | Icon( |
| 294 | Icons.refresh, |
| 295 | ), |
| 296 | SizedBox(width: 8), |
| 297 | Text('Refresh tracking'), |
| 298 | ], |
| 299 | ), |
| 300 | ), |
| 301 | PopupMenuItem( |
| 302 | value: 'refresh_flight_data', |
| 303 | child: Row( |
| 304 | children: [ |
| 305 | Icon( |
| 306 | Icons.refresh, |
| 307 | ), |
| 308 | SizedBox(width: 8), |
| 309 | Text('Refresh flight data'), |
| 310 | ], |
| 311 | ), |
| 312 | ), |
| 313 | PopupMenuItem( |
| 314 | value: 'send_to_wearable', |
| 315 | child: Row( |
| 316 | children: [ |
| 317 | Icon( |
| 318 | Icons.watch, |
| 319 | ), |
| 320 | SizedBox(width: 8), |
| 321 | Text('Send to wearable'), |
| 322 | ], |
| 323 | ), |
| 324 | ), |
| 325 | PopupMenuItem( |
| 326 | value: 'booking_details', |
| 327 | child: Row( |
| 328 | children: [ |
| 329 | Icon(Icons.confirmation_number), |
| 330 | SizedBox(width: 8), |
| 331 | Text('Booking details'), |
| 332 | ], |
| 333 | ), |
| 334 | ), |
| 335 | PopupMenuItem( |
| 336 | value: 'share', |
| 337 | child: Row( |
| 338 | children: [ |
| 339 | Icon(Icons.share), |
| 340 | SizedBox(width: 8), |
| 341 | Text('Share'), |
| 342 | ], |
| 343 | ), |
| 344 | ), |
| 345 | ], |
| 346 | onSelected: (value) async { |
| 347 | if (value == "refresh_tracking") { |
| 348 | widget.onRefreshTracking?.call(); |
| 349 | } else if (value == "send_to_wearable") { |
| 350 | final channel = MethodChannel( |
| 351 | final flightNumber = widget.info. |
| 352 | final airline = widget.info. |
| 353 | final flightAwareData = widget.info. |
| 354 | final departureAirport = departure.airport; |
| 355 | final arrivalAirport = widget.info. |
| 356 | final departureDate = DateTime. |
| 357 | (departure.revisedTime ?? departure.scheduledTime).utc, |
| 358 | ); |
| 359 | |
| 360 | final airlineIata = airline.iata ?? |
| 361 | final airlineIcao = airline.icao ?? |
| 362 | final flightNumberWithoutIata = |
| 363 | flightNumber.replaceFirst(airlineIata, '') |
| 364 | final flightNumberWithoutIcao = |
| 365 | flightNumber.replaceFirst(airlineIcao, '') |
| 366 | |
| 367 | String? _fallbackIdent( |
| 368 | String? primary, |
| 369 | String carrier, |
| 370 | String strippedNumber, |
| 371 | ) { |
| 372 | if (primary != null && primary. |
| 373 | return primary; |
| 374 | } |
| 375 | if (carrier.isEmpty) return null |
| 376 | final number = |
| 377 | strippedNumber.isNotEmpty ? strippedNumber : flightNumber; |
| 378 | return "$carrier$ |
| 379 | } |
| 380 | |
| 381 | final identIata = _fallbackIdent( |
| 382 | flightAwareData?.identIata, |
| 383 | airlineIata, |
| 384 | flightNumberWithoutIata, |
| 385 | ); |
| 386 | |
| 387 | final identIcao = _fallbackIdent( |
| 388 | flightAwareData?.identIcao, |
| 389 | airlineIcao, |
| 390 | flightNumberWithoutIcao, |
| 391 | ); |
| 392 | |
| 393 | final departureAirportCode = departureAirport.iata. |
| 394 | ? departureAirport.iata |
| 395 | : departureAirport.icao; |
| 396 | final arrivalAirportCode = arrivalAirport.iata. |
| 397 | ? arrivalAirport.iata |
| 398 | : arrivalAirport.icao; |
| 399 | final departureCity = departureAirport.municipalityName. |
| 400 | ? departureAirport.municipalityName |
| 401 | : departureAirport.name; |
| 402 | final arrivalCity = arrivalAirport.municipalityName. |
| 403 | ? arrivalAirport.municipalityName |
| 404 | : arrivalAirport.name; |
| 405 | |
| 406 | final payload = { |
| 407 | "flight": { |
| 408 | "id": widget.info.id, |
| 409 | "flightNo": flightNumber, |
| 410 | "identIata": identIata, |
| 411 | "identIcao": identIcao, |
| 412 | "date": departureDate.toIso8601String(), |
| 413 | "departureAirportIata": departureAirportCode, |
| 414 | "departureCity": departureCity, |
| 415 | "arrivalAirportIata": arrivalAirportCode, |
| 416 | "arrivalCity": arrivalCity, |
| 417 | }, |
| 418 | "airline": { |
| 419 | "name": airline.name, |
| 420 | "iata": airline.iata, |
| 421 | "icao": airline.icao, |
| 422 | "image": airline.image, |
| 423 | }, |
| 424 | }; |
| 425 | |
| 426 | await channel.invokeMethod("registerFlight", |
| 427 | print(payload); |
| 428 | ScaffoldMessenger.of(context).showSnackBar |
| 429 | SnackBar( |
| 430 | content: Text('Flight sent to wearable'), |
| 431 | ), |
| 432 | ); |
| 433 | } |
| 434 | if (value == "refresh_flight_data") { |
| 435 | widget.onRefreshFlightData?.call(); |
| 436 | } |
| 437 | if (value == "share") { |
| 438 | _shareFlightCard(); |
| 439 | } |
| 440 | if (value == "booking_details") { |
| 441 | widget.onBookingDetails?.call(); |
| 442 | } |
| 443 | }, |
| 444 | ), |
| 445 | SizedBox( |
| 446 | width: 24, |
| 447 | child: IconButton( |
| 448 | onPressed: widget.onClose, |
| 449 | padding: EdgeInsets.zero, |
| 450 | icon: Icon(Icons. |
| 451 | ), |
| 452 | ), |
| 453 | ], |
| 454 | ) |
| 455 | ], |
| 456 | ), |
| 457 | Divider(), |
| 458 | if ((widget.info.flightAwareData?.cancelled |
| 459 | (widget.info.flightAwareData?.diverted == |
| 460 | _buildFlightStatusBanner(context), |
| 461 | Gap(8), |
| 462 | ], |
| 463 | Column( |
| 464 | crossAxisAlignment: CrossAxisAlignment.start, |
| 465 | children: [ |
| 466 | Text( |
| 467 | getTimeMessage( |
| 468 | DateTime.parse( |
| 469 | (departure.revisedTime ?? |
| 470 | departure.predictedTime ?? |
| 471 | departure.scheduledTime) |
| 472 | .utc, |
| 473 | ), |
| 474 | DateTime.parse( |
| 475 | (arrival.revisedTime ?? |
| 476 | arrival.predictedTime ?? |
| 477 | arrival.scheduledTime) |
| 478 | .utc, |
| 479 | ), |
| 480 | ), |
| 481 | style: TextStyle( |
| 482 | fontSize: 18, |
| 483 | fontWeight: FontWeight.bold, |
| 484 | ), |
| 485 | ), |
| 486 | ], |
| 487 | ), |
| 488 | FlightRouteInfo( |
| 489 | arrivalCode: arrival.airport.iata, |
| 490 | arrivalDelay: _calculateDelay(arrival.revisedTime, |
| 491 | arrival.predictedTime ?? arrival.scheduledTime), |
| 492 | arrivalName: arrival.airport.name, |
| 493 | arrivalSubtitle: "", |
| 494 | arrivalTerminal: arrival.terminal, |
| 495 | arrivalTime: DateTime.parse((arrival |
| 496 | arrival.predictedTime ?? |
| 497 | arrival.scheduledTime) |
| 498 | .utc), |
| 499 | arrivalTimezone: arrival.airport.timeZone, |
| 500 | departureCode: departure.airport.iata, |
| 501 | departureDelay: _calculateDelay(departure.revisedTime, |
| 502 | departure.predictedTime ?? departure.scheduledTime), |
| 503 | departureName: departure.airport.name, |
| 504 | departureSubtitle: "", |
| 505 | departureTerminal: departure.terminal, |
| 506 | departureTime: DateTime.parse((departure |
| 507 | departure.predictedTime ?? |
| 508 | departure.scheduledTime) |
| 509 | .utc), |
| 510 | departureTimezone: departure.airport.timeZone, |
| 511 | statusColor: Colors.green, |
| 512 | use24Hrs: MediaQuery.of(context) |
| 513 | arrivalGate: |
| 514 | widget.info.flightAwareData?.gateDestination ?? arrival |
| 515 | departureGate: |
| 516 | widget.info.flightAwareData?.gateOrigin ?? departure |
| 517 | arrivalBaggage: widget.info.flightAwareData?.baggageClaim |
| 518 | arrival.baggageBelt, |
| 519 | distanceKm: widget.info.greatCircleDistance.km != |
| 520 | ? double.tryParse(widget |
| 521 | : null, |
| 522 | flightAwareDataEntity: widget.info.flightAwareData, |
| 523 | ), |
| 524 | Gap(8), |
| 525 | Text( |
| 526 | "Worth Noting", |
| 527 | style: TextStyle( |
| 528 | fontSize: 16, |
| 529 | fontWeight: FontWeight.bold, |
| 530 | ), |
| 531 | ), |
| 532 | Column( |
| 533 | children: [ |
| 534 | _buildTimezonePill( |
| 535 | context: context, |
| 536 | arrival: arrival, |
| 537 | departure: departure, |
| 538 | ), |
| 539 | if (codeshares.isNotEmpty) ...[ |
| 540 | const Gap(8) |
| 541 | _buildCodesharePill(context, codeshares), |
| 542 | ], |
| 543 | ], |
| 544 | ), |
| 545 | // Booking Details Section |
| 546 | if (widget.info.bookings != null |
| 547 | widget.info.bookings!.isNotEmpty) ...[ |
| 548 | Gap(8), |
| 549 | Text( |
| 550 | "Booking Details", |
| 551 | style: TextStyle( |
| 552 | fontSize: 16, |
| 553 | fontWeight: FontWeight.bold, |
| 554 | ), |
| 555 | ), |
| 556 | Container( |
| 557 | decoration: BoxDecoration( |
| 558 | border: Border.all( |
| 559 | color: Colors.grey.shade300 |
| 560 | ), |
| 561 | borderRadius: BorderRadius.circular( |
| 562 | ), |
| 563 | child: Padding( |
| 564 | padding: const EdgeInsets. |
| 565 | top: 0, |
| 566 | bottom: 0, |
| 567 | ), |
| 568 | child: Column( |
| 569 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 570 | spacing: 8, |
| 571 | children: [ |
| 572 | Builder(builder: (context) { |
| 573 | final booking = widget.info. |
| 574 | |
| 575 | // Extract string values from JsonObject |
| 576 | String? _extractString(dynamic |
| 577 | if (jsonObject == null) return |
| 578 | if (jsonObject is String) return |
| 579 | try { |
| 580 | final value = (jsonObject as dynamic |
| 581 | if (value is String) return |
| 582 | return value?.toString() |
| 583 | } catch (e) { |
| 584 | return jsonObject.toString(); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | final bookingCode = _extractString(booking |
| 589 | final seatNumber = _extractString(booking |
| 590 | final seatingClass = booking.seatingClass? |
| 591 | final seatType = booking.seatType? |
| 592 | final reason = booking.reason? |
| 593 | |
| 594 | return Column( |
| 595 | crossAxisAlignment: CrossAxisAlignment.start, |
| 596 | children: [ |
| 597 | if (bookingCode != null && bookingCode. |
| 598 | ListTile( |
| 599 | contentPadding: EdgeInsets.zero, |
| 600 | leading: Icon(Icons. |
| 601 | title: Text( |
| 602 | 'Booking Code: $bookingCode', |
| 603 | style: TextStyle( |
| 604 | fontWeight: FontWeight.w600, |
| 605 | ), |
| 606 | ), |
| 607 | ), |
| 608 | if (seatNumber != null && seatNumber. |
| 609 | ListTile( |
| 610 | contentPadding: EdgeInsets.zero, |
| 611 | leading: Icon(Icons. |
| 612 | title: Text( |
| 613 | 'Seat: $seatNumber${seatType != |
| 614 | style: TextStyle( |
| 615 | fontWeight: FontWeight.w600, |
| 616 | ), |
| 617 | ), |
| 618 | ), |
| 619 | if (seatingClass != null) |
| 620 | ListTile( |
| 621 | contentPadding: EdgeInsets.zero, |
| 622 | leading: Icon(Icons. |
| 623 | title: Text( |
| 624 | 'Class: ${_formatEnumName(seatingClass)} |
| 625 | style: TextStyle( |
| 626 | fontWeight: FontWeight.w600, |
| 627 | ), |
| 628 | ), |
| 629 | ), |
| 630 | if (reason != null) |
| 631 | ListTile( |
| 632 | contentPadding: EdgeInsets.zero, |
| 633 | leading: Icon(Icons. |
| 634 | title: Text( |
| 635 | 'Reason: ${_formatEnumName(reason)} |
| 636 | style: TextStyle( |
| 637 | fontWeight: FontWeight.w600, |
| 638 | ), |
| 639 | ), |
| 640 | ), |
| 641 | ], |
| 642 | ); |
| 643 | }), |
| 644 | ], |
| 645 | ), |
| 646 | ), |
| 647 | ), |
| 648 | ], |
| 649 | Gap(8), |
| 650 | if (widget.info.aircraft?.model |
| 651 | Text( |
| 652 | widget.info.aircraft!.model!, |
| 653 | style: TextStyle( |
| 654 | fontSize: 16, |
| 655 | fontWeight: FontWeight.bold, |
| 656 | ), |
| 657 | ), |
| 658 | if (widget.info.aircraft?.registration |
| 659 | Container( |
| 660 | decoration: BoxDecoration( |
| 661 | border: Border.all( |
| 662 | color: Colors.grey.shade300 |
| 663 | ), |
| 664 | borderRadius: BorderRadius.circular( |
| 665 | 5, |
| 666 | ), |
| 667 | ), |
| 668 | child: Padding( |
| 669 | padding: const EdgeInsets. |
| 670 | 8.0, |
| 671 | ), |
| 672 | child: Column( |
| 673 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 674 | spacing: 8, |
| 675 | children: [ |
| 676 | if (widget.info.aircraft!.image |
| 677 | ClipRRect( |
| 678 | borderRadius: BorderRadius.circular( |
| 679 | 5, |
| 680 | ), |
| 681 | child: CachedNetworkImage( |
| 682 | imageUrl: widget.info.aircraft!.image |
| 683 | ), |
| 684 | ), |
| 685 | Wrap( |
| 686 | spacing: 10, |
| 687 | alignment: WrapAlignment.spaceBetween, |
| 688 | runSpacing: 10, |
| 689 | children: [ |
| 690 | if (widget.info.aircraft?.age |
| 691 | Column( |
| 692 | crossAxisAlignment: CrossAxisAlignment.start, |
| 693 | children: [ |
| 694 | Text( |
| 695 | "Age", |
| 696 | style: TextStyle( |
| 697 | fontWeight: FontWeight.bold, |
| 698 | color: Colors |
| 699 | .grey[700], // Adjust color as needed |
| 700 | ), |
| 701 | ), |
| 702 | Text( |
| 703 | formatAge( |
| 704 | double.parse( |
| 705 | widget.info.aircraft!.age!, |
| 706 | ), |
| 707 | ), |
| 708 | style: TextStyle( |
| 709 | fontWeight: FontWeight.normal, |
| 710 | color: Colors.black, |
| 711 | ), |
| 712 | ), |
| 713 | ], |
| 714 | ), |
| 715 | if (widget.info.aircraft?.deliveryDate |
| 716 | Column( |
| 717 | crossAxisAlignment: CrossAxisAlignment.end, |
| 718 | children: [ |
| 719 | Text( |
| 720 | "Delivery Date", |
| 721 | style: TextStyle( |
| 722 | fontWeight: FontWeight.bold, |
| 723 | color: Colors |
| 724 | .grey[700], // Adjust color as needed |
| 725 | ), |
| 726 | ), |
| 727 | Text( |
| 728 | formatDate(DateTime.parse |
| 729 | widget.info.aircraft!.deliveryDate!)) |
| 730 | style: TextStyle( |
| 731 | fontWeight: FontWeight.normal, |
| 732 | color: Colors.black, |
| 733 | ), |
| 734 | ), |
| 735 | ], |
| 736 | ), |
| 737 | if (widget.info.aircraft?.registration |
| 738 | Column( |
| 739 | crossAxisAlignment: CrossAxisAlignment.start, |
| 740 | children: [ |
| 741 | Text( |
| 742 | "Registration", |
| 743 | style: TextStyle( |
| 744 | fontWeight: FontWeight.bold, |
| 745 | color: Colors |
| 746 | .grey[700], // Adjust color as needed |
| 747 | ), |
| 748 | ), |
| 749 | Text( |
| 750 | widget.info.aircraft!.registration!, |
| 751 | style: TextStyle( |
| 752 | fontWeight: FontWeight.normal, |
| 753 | color: Colors.black, |
| 754 | ), |
| 755 | ), |
| 756 | const SizedBox(height: |
| 757 | Builder(builder: (context) { |
| 758 | final payload = |
| 759 | (widget.info.aircraft?.payload)?. |
| 760 | final entries = payload == null |
| 761 | ? const <MapEntry |
| 762 | : payload.entries |
| 763 | .where((e) => |
| 764 | e.value is String || |
| 765 | e.value is num || |
| 766 | e.value is bool) |
| 767 | .where((e) => |
| 768 | e.key != 'id' && |
| 769 | e.key != 'numRegistrations') |
| 770 | .toList(); |
| 771 | if (entries.isEmpty) { |
| 772 | return const SizedBox |
| 773 | } |
| 774 | return Column( |
| 775 | crossAxisAlignment: CrossAxisAlignment.start, |
| 776 | children: [ |
| 777 | TextButton( |
| 778 | onPressed: () { |
| 779 | setState(() { |
| 780 | _showAircraftPayload = |
| 781 | !_showAircraftPayload; |
| 782 | }); |
| 783 | }, |
| 784 | style: TextButton.styleFrom( |
| 785 | padding: EdgeInsets.zero, |
| 786 | minimumSize: const Size( |
| 787 | tapTargetSize: |
| 788 | MaterialTapTargetSize.shrinkWrap, |
| 789 | ), |
| 790 | child: Row( |
| 791 | mainAxisSize: MainAxisSize.min, |
| 792 | children: [ |
| 793 | Text( |
| 794 | _showAircraftPayload |
| 795 | ? "Hide more info" |
| 796 | : "Show more info", |
| 797 | style: TextStyle( |
| 798 | fontWeight: FontWeight.w600, |
| 799 | ), |
| 800 | ), |
| 801 | const SizedBox(width: |
| 802 | Icon( |
| 803 | _showAircraftPayload |
| 804 | ? Icons.expand_less |
| 805 | : Icons.expand_more, |
| 806 | size: 18), |
| 807 | ], |
| 808 | ), |
| 809 | ), |
| 810 | if (_showAircraftPayload) |
| 811 | Padding( |
| 812 | padding: |
| 813 | const EdgeInsets.only |
| 814 | child: Column( |
| 815 | crossAxisAlignment: |
| 816 | CrossAxisAlignment.start, |
| 817 | children: entries.map((e) { |
| 818 | final coerced = |
| 819 | _coerceToBool(e.value); |
| 820 | if (coerced != null) { |
| 821 | final boolColor = coerced |
| 822 | ? Colors.green |
| 823 | : Colors.red; |
| 824 | final label = |
| 825 | coerced ? 'Yes' : 'No'; |
| 826 | return Padding( |
| 827 | padding: |
| 828 | const EdgeInsets.symmetric |
| 829 | vertical: 1.0), |
| 830 | child: Row( |
| 831 | mainAxisAlignment: |
| 832 | MainAxisAlignment |
| 833 | .spaceBetween, |
| 834 | children: [ |
| 835 | Expanded( |
| 836 | child: Text( |
| 837 | _formatKey(e.key), |
| 838 | style: TextStyle( |
| 839 | color: |
| 840 | Colors.grey[700], |
| 841 | fontWeight: |
| 842 | FontWeight.w600, |
| 843 | ), |
| 844 | ), |
| 845 | ), |
| 846 | const SizedBox(width: |
| 847 | Row( |
| 848 | mainAxisSize: |
| 849 | MainAxisSize.min, |
| 850 | children: [ |
| 851 | Container( |
| 852 | width: 10, |
| 853 | height: 10, |
| 854 | decoration: |
| 855 | BoxDecoration( |
| 856 | color: boolColor, |
| 857 | shape: |
| 858 | BoxShape.circle, |
| 859 | ), |
| 860 | ), |
| 861 | const SizedBox( |
| 862 | width: 4), |
| 863 | Text( |
| 864 | label, |
| 865 | style: TextStyle( |
| 866 | color: boolColor, |
| 867 | fontSize: 12, |
| 868 | fontWeight: |
| 869 | FontWeight.w600, |
| 870 | ), |
| 871 | ), |
| 872 | ], |
| 873 | ), |
| 874 | ], |
| 875 | ), |
| 876 | ); |
| 877 | } |
| 878 | return Padding( |
| 879 | padding: |
| 880 | const EdgeInsets.symmetric |
| 881 | vertical: 1.0), |
| 882 | child: Row( |
| 883 | mainAxisAlignment: |
| 884 | MainAxisAlignment |
| 885 | .spaceBetween, |
| 886 | children: [ |
| 887 | Expanded( |
| 888 | child: Text( |
| 889 | _formatKey(e.key), |
| 890 | style: TextStyle( |
| 891 | color: Colors.grey[700] |
| 892 | fontWeight: |
| 893 | FontWeight.w600, |
| 894 | ), |
| 895 | ), |
| 896 | ), |
| 897 | const SizedBox(width: |
| 898 | Flexible( |
| 899 | child: Text( |
| 900 | _formatPayloadValue( |
| 901 | e.value), |
| 902 | textAlign: |
| 903 | TextAlign.right, |
| 904 | overflow: |
| 905 | TextOverflow.ellipsis, |
| 906 | ), |
| 907 | ), |
| 908 | ], |
| 909 | ), |
| 910 | ); |
| 911 | }).toList(), |
| 912 | ), |
| 913 | ), |
| 914 | ], |
| 915 | ); |
| 916 | }), |
| 917 | ], |
| 918 | ), |
| 919 | ], |
| 920 | ) |
| 921 | ], |
| 922 | ), |
| 923 | ), |
| 924 | ), |
| 925 | ], |
| 926 | ), |
| 927 | ); |
| 928 | } |
| 929 | |
| 930 | Widget _buildTimezonePill({ |
| 931 | required BuildContext context, |
| 932 | required RouteInfoEntity arrival, |
| 933 | required RouteInfoEntity departure, |
| 934 | }) { |
| 935 | final arrivalTime = _resolveEventTime(arrival) |
| 936 | if (arrivalTime == null) { |
| 937 | return const SizedBox |
| 938 | } |
| 939 | |
| 940 | final use24Hrs = MediaQuery. |
| 941 | final arrivalTz = _sanitizeTimezone(arrival |
| 942 | final departureTz = _sanitizeTimezone(departure |
| 943 | final arrivalLocal = |
| 944 | arrivalTime.toTimezoneString(arrivalTz, use24Hrs: use24Hrs) |
| 945 | final arrivalInDeparture = |
| 946 | arrivalTime.toTimezoneString(departureTz, use24Hrs: use24Hrs) |
| 947 | final diff = getTimezoneDifference(arrivalTz |
| 948 | |
| 949 | final arrivalLabel = _formatAirportLabel(arrival |
| 950 | final departureLabel = _formatAirportLabel(departure |
| 951 | |
| 952 | final diffTitle = _formatDiffTitle(diff) |
| 953 | final subtitle = diff == 'same timezone' |
| 954 | ? "$arrivalLocal arrival in $ |
| 955 | : "$arrivalLocal arrival in $ |
| 956 | |
| 957 | final theme = Theme. |
| 958 | |
| 959 | return Container( |
| 960 | padding: const EdgeInsets. |
| 961 | decoration: BoxDecoration( |
| 962 | borderRadius: BorderRadius.circular( |
| 963 | border: Border.all( |
| 964 | color: theme.dividerColor.withOpacity( |
| 965 | ), |
| 966 | ), |
| 967 | child: Row( |
| 968 | crossAxisAlignment: CrossAxisAlignment.start, |
| 969 | children: [ |
| 970 | Container( |
| 971 | width: 32, |
| 972 | height: 32, |
| 973 | decoration: BoxDecoration( |
| 974 | color: theme.colorScheme.primary.withOpacity |
| 975 | borderRadius: BorderRadius.circular( |
| 976 | ), |
| 977 | child: Icon( |
| 978 | MdiIcons.clockOutline, |
| 979 | size: 18, |
| 980 | ), |
| 981 | ), |
| 982 | const SizedBox(width: |
| 983 | Expanded( |
| 984 | child: Column( |
| 985 | crossAxisAlignment: CrossAxisAlignment.start, |
| 986 | spacing: 4, |
| 987 | children: [ |
| 988 | Text( |
| 989 | diffTitle, |
| 990 | style: const TextStyle( |
| 991 | fontWeight: FontWeight.w700, |
| 992 | ), |
| 993 | ), |
| 994 | Text( |
| 995 | subtitle, |
| 996 | style: TextStyle( |
| 997 | color: theme.textTheme.bodySmall?.color |
| 998 | theme.brightness == Brightness.dark |
| 999 | ), |
| 1000 | ), |
| 1001 | ], |
| 1002 | ), |
| 1003 | ), |
| 1004 | ], |
| 1005 | ), |
| 1006 | ); |
| 1007 | } |
| 1008 | |
| 1009 | Widget _buildCodesharePill( |
| 1010 | BuildContext context, |
| 1011 | List<String> codeshares, |
| 1012 | ) { |
| 1013 | final theme = Theme. |
| 1014 | |
| 1015 | return Container( |
| 1016 | padding: const EdgeInsets. |
| 1017 | decoration: BoxDecoration( |
| 1018 | borderRadius: BorderRadius.circular( |
| 1019 | border: Border.all( |
| 1020 | color: theme.dividerColor.withOpacity( |
| 1021 | ), |
| 1022 | ), |
| 1023 | child: Row( |
| 1024 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1025 | children: [ |
| 1026 | Container( |
| 1027 | width: 32, |
| 1028 | height: 32, |
| 1029 | decoration: BoxDecoration( |
| 1030 | color: theme.colorScheme.secondary.withOpacity |
| 1031 | borderRadius: BorderRadius.circular( |
| 1032 | ), |
| 1033 | child: Icon( |
| 1034 | Icons.flight_takeoff, |
| 1035 | size: 18, |
| 1036 | color: theme.colorScheme.secondary, |
| 1037 | ), |
| 1038 | ), |
| 1039 | const SizedBox(width: |
| 1040 | Expanded( |
| 1041 | child: Column( |
| 1042 | crossAxisAlignment: CrossAxisAlignment.start, |
| 1043 | spacing: 6, |
| 1044 | children: [ |
| 1045 | const Text( |
| 1046 | "Codeshare flight", |
| 1047 | style: TextStyle(fontWeight: FontWeight |
| 1048 | ), |
| 1049 | Text( |
| 1050 | "Also sold as ${codeshares.join( |
| 1051 | style: TextStyle( |
| 1052 | color: theme.textTheme.bodySmall?.color |
| 1053 | theme.brightness == Brightness.dark |
| 1054 | ), |
| 1055 | ), |
| 1056 | Text( |
| 1057 | "Same aircraft, different partner airline numbers.", |
| 1058 | style: TextStyle( |
| 1059 | fontSize: 12, |
| 1060 | color: theme.textTheme.bodySmall?.color |
| 1061 | theme.brightness == Brightness.dark |
| 1062 | ), |
| 1063 | ), |
| 1064 | ], |
| 1065 | ), |
| 1066 | ), |
| 1067 | ], |
| 1068 | ), |
| 1069 | ); |
| 1070 | } |
| 1071 | |
| 1072 | DateTime? _resolveEventTime(RouteInfoEntity |
| 1073 | final candidate = |
| 1074 | segment.revisedTime ?? segment.predictedTime ?? segment. |
| 1075 | try { |
| 1076 | return DateTime.parse |
| 1077 | } catch (_) { |
| 1078 | return null; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | String _formatAirportLabel(RouteAirportEntity |
| 1083 | final code = _airportCode(airport) |
| 1084 | if (code.isEmpty) return airport. |
| 1085 | return "${airport. |
| 1086 | } |
| 1087 | |
| 1088 | String _airportCode(RouteAirportEntity |
| 1089 | return airport.iata.isNotEmpty ? |
| 1090 | } |
| 1091 | |
| 1092 | String _sanitizeTimezone(String |
| 1093 | return timezone.trim(). |
| 1094 | } |
| 1095 | |
| 1096 | String _formatDiffTitle(String |
| 1097 | if (diff == 'same timezone') { |
| 1098 | return "No Timezone Change"; |
| 1099 | } |
| 1100 | final sign = diff.startsWith |
| 1101 | final remainder = diff.substring |
| 1102 | final normalized = remainder |
| 1103 | .replaceAll('hrs', 'Hours' |
| 1104 | .replaceAll('hr', 'Hour' |
| 1105 | .replaceAll('mins', 'Minutes' |
| 1106 | .replaceAll('min', 'Minute' |
| 1107 | return "$sign$ |
| 1108 | } |
| 1109 | |
| 1110 | String _airportCity(RouteAirportEntity |
| 1111 | return airport.municipalityName.isNotEmpty |
| 1112 | ? airport.municipalityName |
| 1113 | : airport.shortName ?? airport.name; |
| 1114 | } |
| 1115 | |
| 1116 | Duration? _calculateDelay(dynamic |
| 1117 | if (revisedTime != null && scheduledTime != |
| 1118 | return getDelay( |
| 1119 | DateTime.parse(scheduledTime.utc |
| 1120 | DateTime.parse(revisedTime.utc |
| 1121 | ); |
| 1122 | } |
| 1123 | return null; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | Duration getDelay(DateTime scheduled |
| 1128 | // Positive duration => delayed; negative duration => early; zero => on time |
| 1129 | return predicted.difference(scheduled); |
| 1130 | } |
| 1131 | |
| 1132 | DateTime convertToArrivalTimeZone( |
| 1133 | DateTime departureTime, |
| 1134 | String departureTimeZone, |
| 1135 | String arrivalTimeZone, |
| 1136 | ) { |
| 1137 | try { |
| 1138 | tz.initializeTimeZones(); |
| 1139 | } catch (e) { |
| 1140 | print(e); |
| 1141 | } |
| 1142 | // Initialize time zone locations |
| 1143 | final departureLocation = tz.getLocation |
| 1144 | final arrivalLocation = tz.getLocation |
| 1145 | |
| 1146 | // Convert departure time to TZDateTime in the departure location |
| 1147 | final departureTZTime = tz.TZDateTime |
| 1148 | |
| 1149 | // Convert the departure TZDateTime to the arrival time zone |
| 1150 | final arrivalTZTime = tz.TZDateTime |
| 1151 | |
| 1152 | return arrivalTZTime.toLocal(); |
| 1153 | } |
| 1154 | |
| 1155 | String formatAge(double age) { |
| 1156 | if (age == 0) return |
| 1157 | |
| 1158 | final years = age.floor |
| 1159 | final months = ((age - years) * |
| 1160 | |
| 1161 | final List<String |
| 1162 | |
| 1163 | if (years > 0) { |
| 1164 | parts.add('$years |
| 1165 | } |
| 1166 | |
| 1167 | if (months > 0) { |
| 1168 | parts.add('$months |
| 1169 | } |
| 1170 | |
| 1171 | return parts.join(' and ' |
| 1172 | } |
| 1173 | |
| 1174 | String formatDate(DateTime date) { |
| 1175 | return '${MONTHS[ |
| 1176 | } |
| 1177 | |
| 1178 | // ignore: unused_element |
| 1179 | class _ChartPosition { |
| 1180 | final double altitude; |
| 1181 | final double speed; |
| 1182 | final DateTime timestamp; |
| 1183 | |
| 1184 | _ChartPosition({ |
| 1185 | required this.altitude, |
| 1186 | required this.speed, |
| 1187 | required this.timestamp, |
| 1188 | }); |
| 1189 | } |