| 1 | import 'package:built_collection/built_collection.dart'; |
| 2 | import 'package:cached_network_image/cached_network_image.dart'; |
| 3 | import 'package:flutter/material.dart'; |
| 4 | import 'package:material_symbols_icons/symbols.dart'; |
| 5 | import 'package:openapi/openapi.dart'; |
| 6 | import 'package:intl/intl.dart'; |
| 7 | import 'package:aero/formatters/time.dart'; |
| 8 | |
| 9 | class FlightSelectionList extends StatelessWidget |
| 10 | final BuiltList<FlightCandidateEntity |
| 11 | final Function(String |
| 12 | final VoidCallback onClose; |
| 13 | |
| 14 | const FlightSelectionList({ |
| 15 | super.key, |
| 16 | required this.flights, |
| 17 | required this.onFlightSelected, |
| 18 | required this.onClose, |
| 19 | }); |
| 20 | |
| 21 | String _formatRoute(FlightCandidateEntity |
| 22 | final originCode = flight.origin. |
| 23 | final destCode = flight.destination. |
| 24 | return '$originCode → |
| 25 | } |
| 26 | |
| 27 | String _formatAirportName(FlightCandidateEntity |
| 28 | if (isOrigin) { |
| 29 | final origin = flight.origin; |
| 30 | if (origin.name != null && |
| 31 | if (origin.city != null && |
| 32 | return '${origin. |
| 33 | } |
| 34 | return origin.name!; |
| 35 | } |
| 36 | return origin.codeIata ?? origin. |
| 37 | } else { |
| 38 | final destination = flight.destination; |
| 39 | if (destination.name != null && |
| 40 | if (destination.city != null && |
| 41 | return '${destination. |
| 42 | } |
| 43 | return destination.name!; |
| 44 | } |
| 45 | return destination.codeIata ?? destination. |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | String? _formatDepartureTime(FlightCandidateEntity |
| 50 | final scheduledTime = flight.scheduledOut ?? |
| 51 | if (scheduledTime == null) return |
| 52 | try { |
| 53 | return formatTime(scheduledTime); |
| 54 | } catch (e) { |
| 55 | return DateFormat('HH:mm') |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | Widget _buildAirlineImage(FlightSummaryAirlineEntity |
| 60 | // Prefer database image (which may have secondary CDN URL) |
| 61 | if (airline.image != null && |
| 62 | return CachedNetworkImage( |
| 63 | imageUrl: airline.image!, |
| 64 | fit: BoxFit.contain, |
| 65 | errorWidget: (context, url, error) { |
| 66 | // Fallback to primary CDN if database image fails |
| 67 | return _buildFallbackAirlineImage(airline); |
| 68 | }, |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | // Fallback to primary CDN |
| 73 | return _buildFallbackAirlineImage(airline); |
| 74 | } |
| 75 | |
| 76 | Widget _buildFallbackAirlineImage(FlightSummaryAirlineEntity |
| 77 | if (airline.iata != null && |
| 78 | return CachedNetworkImage( |
| 79 | imageUrl: "https://www.flightaware.com/images/airline_logos/180px/${airline.icao |
| 80 | fit: BoxFit.contain, |
| 81 | errorWidget: (context, url, error) { |
| 82 | return Icon( |
| 83 | Symbols.flight, |
| 84 | size: 24, |
| 85 | color: Colors.grey, |
| 86 | ); |
| 87 | }, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return Icon( |
| 92 | Symbols.flight, |
| 93 | size: 24, |
| 94 | color: Colors.grey, |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | @override |
| 99 | Widget build(BuildContext |
| 100 | if (flights.isEmpty) { |
| 101 | return SizedBox.shrink |
| 102 | } |
| 103 | |
| 104 | return Column( |
| 105 | crossAxisAlignment: CrossAxisAlignment.start, |
| 106 | children: [ |
| 107 | Container( |
| 108 | padding: const EdgeInsets. |
| 109 | decoration: BoxDecoration( |
| 110 | border: Border( |
| 111 | bottom: BorderSide( |
| 112 | color: Colors.grey[300] |
| 113 | width: 1, |
| 114 | ), |
| 115 | ), |
| 116 | ), |
| 117 | child: Row( |
| 118 | children: [ |
| 119 | Expanded( |
| 120 | child: Column( |
| 121 | crossAxisAlignment: CrossAxisAlignment.start, |
| 122 | children: [ |
| 123 | Text( |
| 124 | 'Select Your Flight', |
| 125 | style: TextStyle( |
| 126 | fontSize: 18, |
| 127 | fontWeight: FontWeight.bold, |
| 128 | color: Colors.grey[900] |
| 129 | ), |
| 130 | ), |
| 131 | SizedBox(height: 2), |
| 132 | Text( |
| 133 | 'We found ${flights.length} |
| 134 | style: TextStyle( |
| 135 | fontSize: 13, |
| 136 | color: Colors.grey[600] |
| 137 | ), |
| 138 | ), |
| 139 | ], |
| 140 | ), |
| 141 | ), |
| 142 | IconButton( |
| 143 | icon: Icon(Icons. |
| 144 | onPressed: onClose, |
| 145 | tooltip: 'Cancel', |
| 146 | padding: EdgeInsets.all( |
| 147 | constraints: BoxConstraints(), |
| 148 | ), |
| 149 | ], |
| 150 | ), |
| 151 | ), |
| 152 | ListView.separated( |
| 153 | shrinkWrap: true, |
| 154 | physics: NeverScrollableScrollPhysics(), |
| 155 | separatorBuilder: (context, index) { |
| 156 | return Divider( |
| 157 | height: 1, |
| 158 | thickness: 0.5, |
| 159 | color: Colors.grey.withOpacity |
| 160 | ); |
| 161 | }, |
| 162 | itemBuilder: (context, index) { |
| 163 | final flight = flights[index]; |
| 164 | final departureTime = _formatDepartureTime(flight) |
| 165 | |
| 166 | return InkWell( |
| 167 | onTap: () => onFlightSelected(flight. |
| 168 | child: Container( |
| 169 | padding: const EdgeInsets. |
| 170 | child: Row( |
| 171 | crossAxisAlignment: CrossAxisAlignment.start, |
| 172 | children: [ |
| 173 | Container( |
| 174 | width: 48, |
| 175 | height: 48, |
| 176 | padding: EdgeInsets.all( |
| 177 | child: _buildAirlineImage(flight.airline), |
| 178 | ), |
| 179 | SizedBox(width: 12), |
| 180 | Expanded( |
| 181 | child: Column( |
| 182 | crossAxisAlignment: CrossAxisAlignment.start, |
| 183 | children: [ |
| 184 | Row( |
| 185 | children: [ |
| 186 | Expanded( |
| 187 | child: Text( |
| 188 | flight.airline.name, |
| 189 | style: TextStyle( |
| 190 | fontWeight: FontWeight.w600, |
| 191 | fontSize: 14, |
| 192 | color: Colors.grey[800] |
| 193 | ), |
| 194 | maxLines: 1, |
| 195 | overflow: TextOverflow.ellipsis, |
| 196 | ), |
| 197 | ), |
| 198 | Container( |
| 199 | padding: EdgeInsets.symmetric( |
| 200 | horizontal: 8, |
| 201 | vertical: 2, |
| 202 | ), |
| 203 | decoration: BoxDecoration( |
| 204 | color: Colors.blue[50] |
| 205 | borderRadius: BorderRadius.circular( |
| 206 | ), |
| 207 | child: Text( |
| 208 | flight.ident, |
| 209 | style: TextStyle( |
| 210 | fontSize: 12, |
| 211 | fontWeight: FontWeight.w600, |
| 212 | color: Colors.blue[700] |
| 213 | ), |
| 214 | ), |
| 215 | ), |
| 216 | ], |
| 217 | ), |
| 218 | SizedBox(height: 8), |
| 219 | Row( |
| 220 | children: [ |
| 221 | Text( |
| 222 | _formatRoute(flight), |
| 223 | style: TextStyle( |
| 224 | fontWeight: FontWeight.bold, |
| 225 | fontSize: 16, |
| 226 | ), |
| 227 | ), |
| 228 | ], |
| 229 | ), |
| 230 | SizedBox(height: 4), |
| 231 | Text( |
| 232 | '${_formatAirportName(flight, |
| 233 | style: TextStyle( |
| 234 | fontSize: 12, |
| 235 | color: Colors.grey[600] |
| 236 | ), |
| 237 | maxLines: 2, |
| 238 | overflow: TextOverflow.ellipsis, |
| 239 | ), |
| 240 | if (departureTime != null) ...[ |
| 241 | SizedBox(height: 6), |
| 242 | Row( |
| 243 | children: [ |
| 244 | Icon( |
| 245 | Icons.schedule, |
| 246 | size: 14, |
| 247 | color: Colors.grey[600] |
| 248 | ), |
| 249 | SizedBox(width: 4), |
| 250 | Text( |
| 251 | 'Departs at $departureTime', |
| 252 | style: TextStyle( |
| 253 | fontSize: 12, |
| 254 | color: Colors.grey[700] |
| 255 | fontWeight: FontWeight.w500, |
| 256 | ), |
| 257 | ), |
| 258 | ], |
| 259 | ), |
| 260 | ], |
| 261 | ], |
| 262 | ), |
| 263 | ), |
| 264 | Icon( |
| 265 | Icons.chevron_right, |
| 266 | color: Colors.grey[400] |
| 267 | size: 24, |
| 268 | ), |
| 269 | ], |
| 270 | ), |
| 271 | ), |
| 272 | ); |
| 273 | }, |
| 274 | itemCount: flights.length, |
| 275 | ), |
| 276 | ], |
| 277 | ); |
| 278 | } |
| 279 | } |