| 1 | import 'package:cached_network_image/cached_network_image.dart'; |
| 2 | import 'package:flutter/material.dart'; |
| 3 | import 'package:flutter_svg/flutter_svg.dart'; |
| 4 | import 'package:gap/gap.dart'; |
| 5 | import 'package:google_fonts/google_fonts.dart'; |
| 6 | import 'package:openapi/openapi.dart'; |
| 7 | |
| 8 | import 'package:aero/extensions/datetime.dart'; |
| 9 | import 'package:aero/extensions/duration.dart'; |
| 10 | import 'package:aero/formatters/time.dart'; |
| 11 | import 'package:aero/utils/duration.dart'; |
| 12 | |
| 13 | class ShareableFlightCard extends StatelessWidget |
| 14 | const ShareableFlightCard({ |
| 15 | super.key, |
| 16 | required this.info, |
| 17 | }); |
| 18 | |
| 19 | final FlightResponseEntity info; |
| 20 | |
| 21 | @override |
| 22 | Widget build(BuildContext |
| 23 | final departure = info.departure; |
| 24 | final arrival = info.arrival; |
| 25 | final airline = info.airline; |
| 26 | |
| 27 | final departureTime = DateTime. |
| 28 | (departure.revisedTime ?? departure.scheduledTime).utc, |
| 29 | ); |
| 30 | final arrivalTime = DateTime. |
| 31 | (arrival.revisedTime ?? arrival.scheduledTime).utc, |
| 32 | ); |
| 33 | final departureScheduled = DateTime. |
| 34 | final arrivalScheduled = DateTime. |
| 35 | |
| 36 | final departureDelay = departure.revisedTime != |
| 37 | ? departureTime.difference(departureScheduled) |
| 38 | : null; |
| 39 | final arrivalDelay = arrival.revisedTime != |
| 40 | ? arrivalTime.difference(arrivalScheduled) |
| 41 | : null; |
| 42 | |
| 43 | Duration duration; |
| 44 | try { |
| 45 | duration = calculateFlightDuration( |
| 46 | departureTime: departureTime.toIso8601String(), |
| 47 | departureTimezone: departure.airport.timeZone ?? 'UTC' |
| 48 | arrivalTime: arrivalTime.toIso8601String(), |
| 49 | arrivalTimezone: arrival.airport.timeZone ?? 'UTC' |
| 50 | ); |
| 51 | } catch (_) { |
| 52 | duration = arrivalTime.difference(departureTime); |
| 53 | } |
| 54 | |
| 55 | return Container( |
| 56 | width: 450, |
| 57 | height: 600, |
| 58 | decoration: BoxDecoration( |
| 59 | color: const Color( |
| 60 | borderRadius: BorderRadius.circular( |
| 61 | ), |
| 62 | child: Stack( |
| 63 | clipBehavior: Clip.antiAlias, |
| 64 | children: [ |
| 65 | Positioned.fill( |
| 66 | child: Opacity( |
| 67 | opacity: 0.3, |
| 68 | child: SvgPicture.asset( |
| 69 | 'assets/background/world.svg', |
| 70 | fit: BoxFit.cover, |
| 71 | colorFilter: ColorFilter.mode( |
| 72 | Colors.black.withOpacity( |
| 73 | BlendMode.darken, |
| 74 | ), |
| 75 | ), |
| 76 | ), |
| 77 | ), |
| 78 | // Status badge for cancelled or diverted flights |
| 79 | if (info.flightAwareData?.cancelled == |
| 80 | info.flightAwareData?.diverted == true) |
| 81 | Positioned( |
| 82 | top: 16, |
| 83 | right: 16, |
| 84 | child: Container( |
| 85 | padding: const EdgeInsets. |
| 86 | decoration: BoxDecoration( |
| 87 | color: info.flightAwareData?.cancelled == |
| 88 | ? Colors.red |
| 89 | : Colors.amber.shade700 |
| 90 | borderRadius: BorderRadius.circular( |
| 91 | ), |
| 92 | child: Text( |
| 93 | info.flightAwareData?.cancelled == true |
| 94 | ? 'CANCELLED' |
| 95 | : 'DIVERTED', |
| 96 | style: TextStyle( |
| 97 | fontFamily: "DM Sans", |
| 98 | fontSize: 10, |
| 99 | fontWeight: FontWeight.bold, |
| 100 | color: Colors.white, |
| 101 | letterSpacing: 0.5, |
| 102 | ), |
| 103 | ), |
| 104 | ), |
| 105 | ), |
| 106 | Positioned.fill( |
| 107 | child: Container( |
| 108 | decoration: BoxDecoration( |
| 109 | gradient: RadialGradient( |
| 110 | center: Alignment.topLeft, |
| 111 | radius: 1.5, |
| 112 | colors: [ |
| 113 | Colors.white.withOpacity( |
| 114 | Colors.white.withOpacity( |
| 115 | Colors.transparent, |
| 116 | ], |
| 117 | stops: const [0.0, |
| 118 | ), |
| 119 | ), |
| 120 | ), |
| 121 | ), |
| 122 | Padding( |
| 123 | padding: const EdgeInsets. |
| 124 | child: Column( |
| 125 | crossAxisAlignment: CrossAxisAlignment.start, |
| 126 | children: [ |
| 127 | Row( |
| 128 | crossAxisAlignment: CrossAxisAlignment.start, |
| 129 | children: [ |
| 130 | if (airline.image != null) |
| 131 | SvgPicture.network( |
| 132 | airline.image!, |
| 133 | width: 32, |
| 134 | height: 32, |
| 135 | ) |
| 136 | else if (airline.iata != null |
| 137 | CachedNetworkImage( |
| 138 | imageUrl: |
| 139 | "https://www.flightaware.com/images/airline_logos/180px/${airline.icao} |
| 140 | width: 32, |
| 141 | height: 32, |
| 142 | errorWidget: (context, url, error) => |
| 143 | const SizedBox.shrink |
| 144 | ), |
| 145 | const Gap(12) |
| 146 | Expanded( |
| 147 | child: Column( |
| 148 | crossAxisAlignment: CrossAxisAlignment.start, |
| 149 | children: [ |
| 150 | Text( |
| 151 | airline.name ?? 'Airline', |
| 152 | style: TextStyle( |
| 153 | fontFamily: "DM Sans", |
| 154 | fontSize: 20, |
| 155 | fontWeight: FontWeight.bold, |
| 156 | color: Colors.white.withOpacity |
| 157 | ), |
| 158 | ), |
| 159 | Text( |
| 160 | "${info.flightNo} |
| 161 | style: TextStyle( |
| 162 | fontFamily: "DM Sans", |
| 163 | fontSize: 16, |
| 164 | fontWeight: FontWeight.w600, |
| 165 | color: Colors.white70, |
| 166 | ), |
| 167 | ), |
| 168 | ], |
| 169 | ), |
| 170 | ), |
| 171 | if (info.greatCircleDistance.km != null |
| 172 | Padding( |
| 173 | padding: EdgeInsets.only( |
| 174 | top: (info.flightAwareData?.cancelled == |
| 175 | info.flightAwareData?.diverted == true) |
| 176 | ? 28.0 |
| 177 | : 0.0, |
| 178 | ), |
| 179 | child: Column( |
| 180 | crossAxisAlignment: CrossAxisAlignment.end, |
| 181 | children: [ |
| 182 | Text( |
| 183 | '${double.parse( |
| 184 | style: TextStyle( |
| 185 | fontFamily: "DM Sans", |
| 186 | fontSize: 15, |
| 187 | fontWeight: FontWeight.bold, |
| 188 | color: Colors.white.withOpacity |
| 189 | ), |
| 190 | ), |
| 191 | const Gap(2) |
| 192 | Text( |
| 193 | '${(double.parse( |
| 194 | style: TextStyle( |
| 195 | fontFamily: "DM Sans", |
| 196 | fontSize: 12, |
| 197 | color: Colors.grey[400] |
| 198 | ), |
| 199 | ), |
| 200 | ], |
| 201 | ), |
| 202 | ), |
| 203 | ], |
| 204 | ), |
| 205 | const Spacer(), |
| 206 | Row( |
| 207 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 208 | crossAxisAlignment: CrossAxisAlignment.start, |
| 209 | children: [ |
| 210 | Expanded( |
| 211 | child: Column( |
| 212 | crossAxisAlignment: CrossAxisAlignment.start, |
| 213 | children: [ |
| 214 | Text( |
| 215 | 'Departure', |
| 216 | style: TextStyle( |
| 217 | fontFamily: "DM Sans", |
| 218 | fontSize: 12, |
| 219 | color: Colors.grey[300] |
| 220 | ), |
| 221 | ), |
| 222 | Row( |
| 223 | children: [ |
| 224 | Text( |
| 225 | departure.airport.iata ?? 'N/A', |
| 226 | style: TextStyle( |
| 227 | fontFamily: "DM Sans", |
| 228 | fontSize: 32, |
| 229 | fontWeight: FontWeight.bold, |
| 230 | color: Colors.white.withOpacity |
| 231 | ), |
| 232 | ), |
| 233 | const Gap(8) |
| 234 | Icon( |
| 235 | Icons.flight_takeoff, |
| 236 | color: Colors.white.withOpacity |
| 237 | size: 24, |
| 238 | ), |
| 239 | ], |
| 240 | ), |
| 241 | Text( |
| 242 | departure.airport.municipalityName ?? 'Unknown', |
| 243 | style: TextStyle( |
| 244 | fontFamily: "DM Sans", |
| 245 | fontSize: 14, |
| 246 | color: Colors.grey[300] |
| 247 | ), |
| 248 | ), |
| 249 | const Gap(4) |
| 250 | Column( |
| 251 | crossAxisAlignment: CrossAxisAlignment.start, |
| 252 | children: [ |
| 253 | Text( |
| 254 | departureTime.toTimezoneString( |
| 255 | departure.airport.timeZone ?? 'UTC', |
| 256 | use24Hrs: MediaQuery.of(context) |
| 257 | .alwaysUse24HourFormat, |
| 258 | ), |
| 259 | style: TextStyle( |
| 260 | fontFamily: "DM Sans", |
| 261 | fontSize: 16, |
| 262 | fontWeight: FontWeight.w600, |
| 263 | color: Colors.white.withOpacity |
| 264 | ), |
| 265 | ), |
| 266 | if (departureDelay != null && |
| 267 | departureDelay != Duration.zero) |
| 268 | Text( |
| 269 | departureDelay.isNegative |
| 270 | ? '${departureDelay.abs |
| 271 | : '${departureDelay.toHumanReadable |
| 272 | style: TextStyle( |
| 273 | fontFamily: "DM Sans", |
| 274 | fontSize: 11, |
| 275 | color: departureDelay.isNegative |
| 276 | ? Colors.green[300] |
| 277 | : Colors.red[300] |
| 278 | fontWeight: FontWeight.w500, |
| 279 | ), |
| 280 | ), |
| 281 | ], |
| 282 | ), |
| 283 | _buildShareableAirportDetails( |
| 284 | terminal: departure.terminal, |
| 285 | gate: info.flightAwareData?.gateOrigin ?? |
| 286 | departure.gate, |
| 287 | baggage: null, |
| 288 | runway: info.flightAwareData?.actualRunwayOff, |
| 289 | alignment: CrossAxisAlignment.start, |
| 290 | ), |
| 291 | ], |
| 292 | ), |
| 293 | ), |
| 294 | Expanded( |
| 295 | child: Column( |
| 296 | crossAxisAlignment: CrossAxisAlignment.end, |
| 297 | children: [ |
| 298 | Text( |
| 299 | 'Arrival', |
| 300 | style: TextStyle( |
| 301 | fontFamily: "DM Sans", |
| 302 | fontSize: 12, |
| 303 | color: Colors.grey[300] |
| 304 | ), |
| 305 | ), |
| 306 | Row( |
| 307 | mainAxisAlignment: MainAxisAlignment.end, |
| 308 | children: [ |
| 309 | Icon( |
| 310 | Icons.flight_land, |
| 311 | color: Colors.white.withOpacity |
| 312 | size: 24, |
| 313 | ), |
| 314 | const Gap(8) |
| 315 | Text( |
| 316 | arrival.airport.iata ?? 'N/A', |
| 317 | style: TextStyle( |
| 318 | fontFamily: "DM Sans", |
| 319 | fontSize: 32, |
| 320 | fontWeight: FontWeight.bold, |
| 321 | color: Colors.white.withOpacity |
| 322 | ), |
| 323 | ), |
| 324 | ], |
| 325 | ), |
| 326 | Text( |
| 327 | arrival.airport.municipalityName ?? 'Unknown', |
| 328 | style: TextStyle( |
| 329 | fontFamily: "DM Sans", |
| 330 | fontSize: 14, |
| 331 | color: Colors.grey[300] |
| 332 | ), |
| 333 | textAlign: TextAlign.right, |
| 334 | ), |
| 335 | const Gap(4) |
| 336 | Column( |
| 337 | crossAxisAlignment: CrossAxisAlignment.end, |
| 338 | children: [ |
| 339 | Text( |
| 340 | arrivalTime.toTimezoneString( |
| 341 | arrival.airport.timeZone ?? 'UTC', |
| 342 | use24Hrs: MediaQuery.of(context) |
| 343 | .alwaysUse24HourFormat, |
| 344 | ), |
| 345 | style: TextStyle( |
| 346 | fontFamily: "DM Sans", |
| 347 | fontSize: 16, |
| 348 | fontWeight: FontWeight.w600, |
| 349 | color: Colors.white.withOpacity |
| 350 | ), |
| 351 | ), |
| 352 | if (arrivalDelay != null && |
| 353 | arrivalDelay != Duration.zero) |
| 354 | Text( |
| 355 | arrivalDelay.isNegative |
| 356 | ? '${arrivalDelay.abs |
| 357 | : '${arrivalDelay.toHumanReadable |
| 358 | style: TextStyle( |
| 359 | fontFamily: "DM Sans", |
| 360 | fontSize: 11, |
| 361 | color: arrivalDelay.isNegative |
| 362 | ? Colors.green[300] |
| 363 | : Colors.red[300] |
| 364 | fontWeight: FontWeight.w500, |
| 365 | ), |
| 366 | ), |
| 367 | ], |
| 368 | ), |
| 369 | _buildShareableAirportDetails( |
| 370 | terminal: arrival.terminal, |
| 371 | gate: info.flightAwareData?.gateDestination ?? |
| 372 | arrival.gate, |
| 373 | baggage: info.flightAwareData?.baggageClaim ?? |
| 374 | arrival.baggageBelt, |
| 375 | runway: info.flightAwareData?.actualRunwayOn, |
| 376 | alignment: CrossAxisAlignment.end, |
| 377 | ), |
| 378 | ], |
| 379 | ), |
| 380 | ), |
| 381 | ], |
| 382 | ), |
| 383 | const Spacer(), |
| 384 | Row( |
| 385 | mainAxisAlignment: MainAxisAlignment.spaceAround, |
| 386 | children: [ |
| 387 | _buildShareableDetailItem( |
| 388 | Icons.access_time, |
| 389 | 'Duration', |
| 390 | duration.toHumanReadable(), |
| 391 | ), |
| 392 | _buildShareableDetailItem( |
| 393 | Icons.calendar_today, |
| 394 | 'Date', |
| 395 | formatDayAndMonth(departureTime), |
| 396 | ), |
| 397 | _buildShareableDetailItem( |
| 398 | Icons.flight, |
| 399 | 'Aircraft', |
| 400 | info.aircraft?.model?.split |
| 401 | info.aircraft?.registration ?? |
| 402 | 'N/A', |
| 403 | ), |
| 404 | ], |
| 405 | ), |
| 406 | const Gap(16) |
| 407 | Divider( |
| 408 | color: Colors.grey.shade700 |
| 409 | thickness: 0.5, |
| 410 | ), |
| 411 | const Gap(12) |
| 412 | Row( |
| 413 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 414 | children: [ |
| 415 | Text( |
| 416 | 'Powered by Aero', |
| 417 | style: TextStyle( |
| 418 | fontFamily: "DM Sans", |
| 419 | fontSize: 11, |
| 420 | color: Colors.grey[500] |
| 421 | fontStyle: FontStyle.italic, |
| 422 | ), |
| 423 | ), |
| 424 | if (info.aircraft?.registration != |
| 425 | Text( |
| 426 | info.aircraft!.registration!, |
| 427 | style: GoogleFonts.jetBrainsMono( |
| 428 | fontSize: 10, |
| 429 | color: Colors.grey[500] |
| 430 | letterSpacing: 1.2, |
| 431 | ), |
| 432 | ), |
| 433 | ], |
| 434 | ), |
| 435 | ], |
| 436 | ), |
| 437 | ), |
| 438 | ], |
| 439 | ), |
| 440 | ); |
| 441 | } |
| 442 | |
| 443 | Widget _buildShareableDetailItem(IconData |
| 444 | return Expanded( |
| 445 | child: Column( |
| 446 | mainAxisSize: MainAxisSize.min, |
| 447 | children: [ |
| 448 | Icon( |
| 449 | icon, |
| 450 | color: Colors.white70, |
| 451 | size: 20, |
| 452 | ), |
| 453 | const Gap(6) |
| 454 | Text( |
| 455 | label, |
| 456 | style: TextStyle( |
| 457 | fontFamily: "DM Sans", |
| 458 | fontSize: 11, |
| 459 | color: Colors.grey[500] |
| 460 | fontWeight: FontWeight.w500, |
| 461 | ), |
| 462 | ), |
| 463 | const Gap(2) |
| 464 | Text( |
| 465 | value, |
| 466 | style: TextStyle( |
| 467 | fontFamily: "DM Sans", |
| 468 | fontSize: 13, |
| 469 | fontWeight: FontWeight.w600, |
| 470 | color: Colors.white.withOpacity |
| 471 | ), |
| 472 | textAlign: TextAlign.center, |
| 473 | maxLines: 2, |
| 474 | overflow: TextOverflow.ellipsis, |
| 475 | ), |
| 476 | ], |
| 477 | ), |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | Widget _buildShareableAirportDetails({ |
| 482 | required String? terminal, |
| 483 | required String? gate, |
| 484 | required String? baggage, |
| 485 | required String? runway, |
| 486 | required CrossAxisAlignment alignment, |
| 487 | }) { |
| 488 | final items = <Widget |
| 489 | |
| 490 | if (terminal != null && terminal. |
| 491 | items.add( |
| 492 | _buildShareableDetailRow( |
| 493 | 'Terminal', |
| 494 | terminal, |
| 495 | alignment: alignment, |
| 496 | ), |
| 497 | ); |
| 498 | } |
| 499 | |
| 500 | if (gate != null && gate. |
| 501 | items.add( |
| 502 | _buildShareableDetailRow( |
| 503 | 'Gate', |
| 504 | gate, |
| 505 | alignment: alignment, |
| 506 | ), |
| 507 | ); |
| 508 | } |
| 509 | |
| 510 | if (baggage != null && baggage. |
| 511 | items.add( |
| 512 | _buildShareableDetailRow( |
| 513 | 'Baggage', |
| 514 | baggage, |
| 515 | alignment: alignment, |
| 516 | ), |
| 517 | ); |
| 518 | } |
| 519 | |
| 520 | if (runway != null && runway. |
| 521 | items.add( |
| 522 | _buildShareableDetailRow( |
| 523 | 'Runway', |
| 524 | runway, |
| 525 | alignment: alignment, |
| 526 | ), |
| 527 | ); |
| 528 | } |
| 529 | |
| 530 | if (items.isEmpty) { |
| 531 | return const SizedBox |
| 532 | } |
| 533 | |
| 534 | return Column( |
| 535 | crossAxisAlignment: alignment, |
| 536 | children: items, |
| 537 | ); |
| 538 | } |
| 539 | |
| 540 | Widget _buildShareableDetailRow( |
| 541 | String label, |
| 542 | String value, { |
| 543 | required CrossAxisAlignment alignment, |
| 544 | }) { |
| 545 | return Padding( |
| 546 | padding: const EdgeInsets. |
| 547 | child: Row( |
| 548 | mainAxisSize: MainAxisSize.min, |
| 549 | children: [ |
| 550 | Text( |
| 551 | '$label: ', |
| 552 | style: TextStyle( |
| 553 | fontFamily: "DM Sans", |
| 554 | fontSize: 10, |
| 555 | color: Colors.white.withOpacity |
| 556 | fontWeight: FontWeight.w600, |
| 557 | ), |
| 558 | ), |
| 559 | Text( |
| 560 | value, |
| 561 | style: TextStyle( |
| 562 | fontFamily: "DM Sans", |
| 563 | fontSize: 10, |
| 564 | color: Colors.white70, |
| 565 | fontWeight: FontWeight.w600, |
| 566 | ), |
| 567 | ), |
| 568 | ], |
| 569 | ), |
| 570 | ); |
| 571 | } |
| 572 | } |