| 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:openapi/openapi.dart'; |
| 3 | import 'package:aero/extensions/datetime.dart'; |
| 4 | import 'package:aero/extensions/duration.dart'; |
| 5 | import 'package:intl/intl.dart'; |
| 6 | |
| 7 | class FlightRouteInfo extends StatelessWidget |
| 8 | final String departureCode; |
| 9 | final String departureName; |
| 10 | final String? departureTerminal; |
| 11 | final String? departureGate; |
| 12 | final DateTime departureTime; |
| 13 | final Duration? departureDelay; |
| 14 | final String departureSubtitle; |
| 15 | final String departureTimezone; |
| 16 | |
| 17 | final String arrivalCode; |
| 18 | final String arrivalName; |
| 19 | final String? arrivalTerminal; |
| 20 | final String? arrivalGate; |
| 21 | final DateTime arrivalTime; |
| 22 | final Duration? arrivalDelay; |
| 23 | final String arrivalSubtitle; |
| 24 | final String arrivalTimezone; |
| 25 | final String? arrivalBaggage; |
| 26 | |
| 27 | final Color? statusColor; |
| 28 | final bool use24Hrs; |
| 29 | final double? distanceKm; |
| 30 | final FlightAwareDataEntity? flightAwareDataEntity; |
| 31 | |
| 32 | const FlightRouteInfo( |
| 33 | {super.key, |
| 34 | required this.departureCode, |
| 35 | required this.departureName, |
| 36 | this.departureTerminal, |
| 37 | this.departureGate, |
| 38 | required this.departureTime, |
| 39 | this.departureDelay, |
| 40 | required this.departureSubtitle, |
| 41 | required this.arrivalCode, |
| 42 | required this.arrivalName, |
| 43 | this.arrivalTerminal, |
| 44 | this.arrivalGate, |
| 45 | required this.arrivalTime, |
| 46 | this.arrivalDelay, |
| 47 | required this.arrivalSubtitle, |
| 48 | this.statusColor, |
| 49 | required this.arrivalTimezone, |
| 50 | required this.departureTimezone, |
| 51 | required this.use24Hrs, |
| 52 | this.arrivalBaggage, |
| 53 | this.distanceKm, |
| 54 | this.flightAwareDataEntity = null}) |
| 55 | |
| 56 | @override |
| 57 | Widget build(BuildContext |
| 58 | final Duration totalDuration = arrivalTime |
| 59 | |
| 60 | return Container( |
| 61 | padding: const EdgeInsets. |
| 62 | 8.0, |
| 63 | ), |
| 64 | decoration: BoxDecoration( |
| 65 | border: Border.all( |
| 66 | color: Colors.grey.shade300 |
| 67 | ), |
| 68 | borderRadius: BorderRadius.circular( |
| 69 | 5, |
| 70 | ), |
| 71 | ), |
| 72 | child: Padding( |
| 73 | padding: const EdgeInsets. |
| 74 | top: 8.0, |
| 75 | ), |
| 76 | child: Column( |
| 77 | crossAxisAlignment: CrossAxisAlignment.start, |
| 78 | children: [ |
| 79 | _buildStopRow( |
| 80 | code: departureCode, |
| 81 | name: departureName, |
| 82 | terminal: departureTerminal, |
| 83 | gate: departureGate, |
| 84 | time: departureTime, |
| 85 | delay: departureDelay, |
| 86 | subtitle: departureSubtitle, |
| 87 | timezone: departureTimezone, |
| 88 | isDeparture: true, |
| 89 | ), |
| 90 | _buildMidDivider( |
| 91 | totalDuration: totalDuration, |
| 92 | distanceKm: distanceKm, |
| 93 | ), |
| 94 | _buildStopRow( |
| 95 | code: arrivalCode, |
| 96 | name: arrivalName, |
| 97 | terminal: arrivalTerminal, |
| 98 | gate: arrivalGate, |
| 99 | time: arrivalTime, |
| 100 | delay: arrivalDelay, |
| 101 | subtitle: arrivalSubtitle, |
| 102 | timezone: arrivalTimezone, |
| 103 | baggage: arrivalBaggage, |
| 104 | isDeparture: false, |
| 105 | ), |
| 106 | ], |
| 107 | ), |
| 108 | ), |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | Widget _buildStopRow({ |
| 113 | required String code, |
| 114 | required String name, |
| 115 | required String? terminal, |
| 116 | String? gate, |
| 117 | required DateTime time, |
| 118 | required Duration? delay, |
| 119 | required String subtitle, |
| 120 | required String timezone, |
| 121 | String? baggage, |
| 122 | required bool isDeparture, |
| 123 | }) { |
| 124 | final Duration? d = |
| 125 | final bool hasDelay = d |
| 126 | final bool isEarly = hasDelay |
| 127 | final Color timeColor = |
| 128 | hasDelay ? (isEarly ? Colors.green |
| 129 | final DateTime? originalTime = |
| 130 | |
| 131 | // Prefer props, fallback to FlightAware |
| 132 | final String? faTerminal = |
| 133 | ? flightAwareDataEntity?.terminalOrigin |
| 134 | : flightAwareDataEntity?.terminalDestination; |
| 135 | final String? faGate = |
| 136 | ? flightAwareDataEntity?.gateOrigin |
| 137 | : flightAwareDataEntity?.gateDestination; |
| 138 | final String? faBaggage = |
| 139 | |
| 140 | final String? effTerminal = |
| 141 | (terminal != null && terminal.isNotEmpty) ? |
| 142 | final String? effGate = |
| 143 | final String? effBaggage = |
| 144 | (baggage != null && baggage.isNotEmpty) ? |
| 145 | |
| 146 | return Row( |
| 147 | crossAxisAlignment: CrossAxisAlignment.start, |
| 148 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 149 | children: [ |
| 150 | Expanded( |
| 151 | child: Column( |
| 152 | crossAxisAlignment: CrossAxisAlignment.start, |
| 153 | children: [ |
| 154 | // Airport code and name on same line with reduced opacity |
| 155 | Text( |
| 156 | '$code • $name |
| 157 | style: TextStyle( |
| 158 | fontSize: 16, |
| 159 | fontWeight: FontWeight.w600, |
| 160 | color: Colors.black.withOpacity |
| 161 | ), |
| 162 | ), |
| 163 | if (subtitle.isNotEmpty) ...[ |
| 164 | const SizedBox(height: |
| 165 | Text( |
| 166 | subtitle, |
| 167 | style: TextStyle( |
| 168 | fontSize: 12, |
| 169 | color: Colors.grey.shade500 |
| 170 | ), |
| 171 | ), |
| 172 | ], |
| 173 | const SizedBox(height: |
| 174 | // Time row with original time if delayed |
| 175 | Row( |
| 176 | mainAxisSize: MainAxisSize.min, |
| 177 | crossAxisAlignment: CrossAxisAlignment.baseline, |
| 178 | textBaseline: TextBaseline.alphabetic, |
| 179 | children: [ |
| 180 | Text( |
| 181 | time.toTimezoneString( |
| 182 | timezone, |
| 183 | use24Hrs: use24Hrs, |
| 184 | ), |
| 185 | style: TextStyle( |
| 186 | fontSize: 24, |
| 187 | color: (flightAwareDataEntity?.cancelled == true) |
| 188 | ? Colors.red |
| 189 | : timeColor, |
| 190 | fontWeight: FontWeight.bold, |
| 191 | decoration: (flightAwareDataEntity?.cancelled == true) |
| 192 | ? TextDecoration.lineThrough |
| 193 | : null, |
| 194 | decorationColor: (flightAwareDataEntity?.cancelled == true) |
| 195 | ? Colors.red |
| 196 | : null, |
| 197 | ), |
| 198 | ), |
| 199 | if (originalTime != null) |
| 200 | Padding( |
| 201 | padding: const EdgeInsets. |
| 202 | child: Text( |
| 203 | originalTime.toTimezoneString( |
| 204 | timezone, |
| 205 | use24Hrs: use24Hrs, |
| 206 | ), |
| 207 | style: TextStyle( |
| 208 | fontSize: 16, |
| 209 | color: Colors.grey.shade500 |
| 210 | ), |
| 211 | ), |
| 212 | ), |
| 213 | ], |
| 214 | ), |
| 215 | const SizedBox(height: |
| 216 | // Status text |
| 217 | if (flightAwareDataEntity?.cancelled == true) |
| 218 | Text( |
| 219 | 'Cancelled', |
| 220 | style: TextStyle( |
| 221 | fontSize: 13, |
| 222 | color: Colors.red, |
| 223 | fontWeight: FontWeight.w700, |
| 224 | ), |
| 225 | ) |
| 226 | else if (hasDelay) |
| 227 | Text( |
| 228 | isEarly |
| 229 | ? "${_formatMinutesOnly(d |
| 230 | : "${_formatMinutesOnly(d |
| 231 | style: TextStyle( |
| 232 | fontSize: 13, |
| 233 | color: timeColor, |
| 234 | fontWeight: FontWeight.w700, |
| 235 | ), |
| 236 | ) |
| 237 | else |
| 238 | Text( |
| 239 | 'On Time', |
| 240 | style: TextStyle( |
| 241 | fontSize: 13, |
| 242 | color: Colors.green, |
| 243 | fontWeight: FontWeight.w700, |
| 244 | ), |
| 245 | ), |
| 246 | ], |
| 247 | ), |
| 248 | ), |
| 249 | const SizedBox(width: |
| 250 | Column( |
| 251 | crossAxisAlignment: CrossAxisAlignment.end, |
| 252 | mainAxisAlignment: MainAxisAlignment.start, |
| 253 | children: [ |
| 254 | // Baggage belt (for arrivals) or Gate (for departures) |
| 255 | if (!isDeparture) |
| 256 | _buildInfoBadge( |
| 257 | icon: Icons.luggage, |
| 258 | label: effBaggage ?? '--', |
| 259 | ) |
| 260 | else |
| 261 | _buildInfoBadge( |
| 262 | icon: Icons.door_front_door, |
| 263 | label: effGate ?? '--', |
| 264 | ), |
| 265 | const SizedBox(height: |
| 266 | // Terminal |
| 267 | _buildInfoBadge( |
| 268 | icon: Icons.title_rounded, |
| 269 | label: effTerminal ?? '--', |
| 270 | ), |
| 271 | ], |
| 272 | ), |
| 273 | ], |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | Widget _buildMidDivider( |
| 278 | {required Duration totalDuration, |
| 279 | final String durationText = totalDuration |
| 280 | String centerText = durationText; |
| 281 | if (distanceKm != null) { |
| 282 | final numberFormatter = NumberFormat. |
| 283 | final String kmFormatted = numberFormatter |
| 284 | centerText = "$durationText • $ |
| 285 | } |
| 286 | |
| 287 | return Row( |
| 288 | children: [ |
| 289 | Expanded(child: Divider(color |
| 290 | Container( |
| 291 | padding: const EdgeInsets. |
| 292 | margin: const EdgeInsets. |
| 293 | horizontal: 4, |
| 294 | vertical: 4, |
| 295 | ), |
| 296 | child: Text( |
| 297 | centerText, |
| 298 | style: TextStyle( |
| 299 | fontSize: 12, |
| 300 | color: Colors.grey.shade800 |
| 301 | fontWeight: FontWeight.w600, |
| 302 | ), |
| 303 | ), |
| 304 | ), |
| 305 | Expanded(child: Divider(color |
| 306 | ], |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | Widget _buildInfoBadge({ |
| 311 | required IconData icon, |
| 312 | required String label, |
| 313 | }) { |
| 314 | return Row( |
| 315 | mainAxisSize: MainAxisSize.min, |
| 316 | children: [ |
| 317 | Icon(icon, size: 16 |
| 318 | const SizedBox(width: |
| 319 | Text( |
| 320 | label, |
| 321 | style: TextStyle( |
| 322 | fontSize: 13, |
| 323 | color: Colors.grey.shade700 |
| 324 | fontWeight: FontWeight.w600, |
| 325 | ), |
| 326 | ), |
| 327 | ], |
| 328 | ); |
| 329 | } |
| 330 | |
| 331 | String _formatMinutesOnly(Duration |
| 332 | final Duration abs = d |
| 333 | final int hours = abs |
| 334 | final int minutes = abs |
| 335 | if (hours > 0 && minutes > |
| 336 | return "$hours |
| 337 | } else if (hours > 0) { |
| 338 | return "$hours |
| 339 | } else if (minutes > 0) { |
| 340 | return "$minutes mins" |
| 341 | } else { |
| 342 | return "just now"; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | } |