import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:intl/intl.dart'; class TaxiListWidget extends StatelessWidget { final List> taxiList; final Function(bool, Map, String) onOpen; final Function(Map) onDeleteTaxi; final Map? apiData; final Function(String, bool) onAddNew; final bool isViewMode; const TaxiListWidget({ super.key, required this.taxiList, required this.apiData, required this.onOpen, required this.onDeleteTaxi, required this.onAddNew, required this.isViewMode, }); @override Widget build(BuildContext context) { final isDesktop = MediaQuery.of(context).size.width > 1024; return Padding( padding: const EdgeInsets.all(16.0), child: Container( margin: const EdgeInsets.only(top: 16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Header with title and button Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ if ((!isDesktop) && taxiList.isNotEmpty) Text( " ", style: const TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), MouseRegion( cursor: isViewMode ? SystemMouseCursors.forbidden : SystemMouseCursors.click, child: GestureDetector( onTap: isViewMode ? null : () { print("New data"); onAddNew("Taxi", true); }, child: Row( mainAxisSize: MainAxisSize.min, // Ensures content fits nicely children: [ // Text( // "Add New", // style: TextStyle(fontSize: 13), // ), // SizedBox(width: 8), // spacing between icon and text Icon( Icons.add_circle_sharp, size: 30, color: Color(0xFF114D8B), ), ], ), ), // child: ElevatedButton( // style: ElevatedButton.styleFrom( // backgroundColor: Color(0xFF114D8B), // foregroundColor: Colors.white, // disabledBackgroundColor: Color(0xFF114D8B), // disabledForegroundColor: Colors.white, // shape: RoundedRectangleBorder( // borderRadius: BorderRadius.circular(8), // side: BorderSide(color: Color(0xFF114D8B), width: 2), // ), // padding: // EdgeInsets.symmetric(horizontal: 20, vertical: 12), // ), // onPressed: isViewMode // ? null // : () { // print("New data"); // onAddNew("Taxi", true); // }, // child: Row( // mainAxisSize: // MainAxisSize.min, // Ensures content fits nicely // children: [ // Icon( // Icons.add_circle_sharp, // size: 30, // color: Color(0xFF114D8B), // ) // ], // ), // ), ), ], ), const SizedBox(height: 16), // Scroll behavior based on device _buildData(context, isDesktop), ], ), ), ); } Widget _buildData(BuildContext context, bool isDesktop) { List> filteredList = taxiList.where((item) => item["is_active"] == "1").toList(); print("filteredList- $filteredList"); List taxiClassList = apiData?['taxt_car_type'] ?? []; List taxirequiredFor = apiData?['taxi_car_required_for'] ?? []; String getRequestForTaxiClass(String? specialRequestKey) { if (specialRequestKey == null) return "N/A"; return taxiClassList .firstWhere( (element) => element["dropdown_key"].toString() == specialRequestKey, orElse: () => {"dropdown_value": "N/A"}, )["dropdown_value"] .toString(); } String getRequestFortaxirequiredFor(String? specialRequestKey) { if (specialRequestKey == null) return "N/A"; return taxirequiredFor .firstWhere( (element) => element["dropdown_key"].toString() == specialRequestKey, orElse: () => {"dropdown_value": "N/A"}, )["dropdown_value"] .toString(); } // String formatDate(String dateString) { // try { // DateTime date = DateTime.parse(dateString); // return DateFormat('d MMM yy').format(date); // Example: 4 Apr 25 // } catch (e) { // return "Invalid Date"; // } // } // // String formatTime(String timeString) { // try { // final parts = timeString.split(':'); // final now = DateTime.now(); // final dateTime = DateTime( // now.year, // now.month, // now.day, // int.parse(parts[0]), // int.parse(parts[1]), // ); // return DateFormat.jm().format(dateTime); // e.g., 12:16 PM or 12 AM // } catch (e) { // return "Invalid Time"; // } // } String formatDate(String dateString) { try { DateTime date = DateFormat("dd-MM-yyyy").parse(dateString); return DateFormat('dd MMM yy').format(date); // Example: 23 May 25 } catch (e) { return "Invalid Date"; } } String formatTime(String timeString) { try { final parts = timeString.split(':'); int hour = int.parse(parts[0]); int minute = int.parse(parts[1]); final now = DateTime.now(); DateTime dateTime; if (hour == 24 && minute == 0) { // 24:00 is treated as 00:00 on the next day dateTime = DateTime( now.year, now.month, now.day, ).add(const Duration(days: 1)); } else { if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { throw FormatException("Invalid hour or minute"); } dateTime = DateTime(now.year, now.month, now.day, hour, minute); } return DateFormat('HH:mm').format(dateTime); // 24-hour format } catch (e) { return "Invalid Time"; } } return ListView.builder( shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemCount: filteredList.length, itemBuilder: (context, index) { final item = filteredList[index]; return Container( margin: EdgeInsets.symmetric(horizontal: 8, vertical: 6), decoration: BoxDecoration( borderRadius: BorderRadius.circular(12), // color: Colors.orange.shade50, color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black12, blurRadius: 3, offset: Offset(0, 1), ), ], border: Border( top: BorderSide( color: Colors.white70, // Change color to match your theme width: 2, ), ), ), child: Padding( padding: EdgeInsets.all(12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.end, children: [ Text( "${getRequestForTaxiClass(item["car_type"]!.toString())} For ${item["no_of_passengers"]!} Passenger", style: GoogleFonts.poppins( fontSize: 13, fontWeight: FontWeight.w800, color: !isDesktop ? Color(0xFF114D8B) : Color(0xFF575A74) ), ), Spacer(), GestureDetector( onTap: () => onOpen(true, item, "Taxi"), child: Tooltip( message: 'Edit Taxi Details', child: Image.asset( 'assets/images/IconsImg/edit.png', width: 20, height: 15, color: Color(0xFF114D8B), ), ), ), SizedBox(width: 10), GestureDetector( onTap: () => onDeleteTaxi(item), child: Tooltip( message: 'Delete Taxi Details', child: Image.asset( 'assets/images/IconsImg/delete.png', width: 20, height: 15, color: Colors.red, ), ), ), ], ), Divider(color: Colors.blueGrey.shade50), SizedBox(height: 4), isDesktop ? Row( children: [ Expanded( flex: 2, child: Text( "City", style: GoogleFonts.poppins(fontSize: 11), ), ), Expanded( flex: 2, child: Text( " Location", style: GoogleFonts.poppins(fontSize: 11), ), ), Expanded( flex: 2, child: Text( " Date", style: GoogleFonts.poppins(fontSize: 11), ), ), Expanded( flex: 2, child: Text( "Comments", style: GoogleFonts.poppins(fontSize: 11), ), ), ], ) : SizedBox.shrink(), SizedBox(height: 4), isDesktop ? Row( children: [ Expanded( flex: 2, child: Text( "${item["destination_city"]}", style: GoogleFonts.poppins( fontSize: 12, fontWeight: FontWeight.w600, ), ), ), SizedBox(width: 10), Expanded( flex: 2, child: Text( "${item["location_of_pickup"]}", style: GoogleFonts.poppins( fontSize: 12, fontWeight: FontWeight.w600, ), ), ), SizedBox(width: 10), Expanded( flex: 2, child: Text( "${formatDate(item["date"]!)} ${formatTime(item["time"]!)}", style: GoogleFonts.poppins( fontSize: 12, fontWeight: FontWeight.w600, ), ), ), SizedBox(width: 10), Expanded( flex: 2, child: _buildComments( " Comments:", item["comments"] ?? "N/A", ), ), ], ) : Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // First row: City & Location Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ _infoColumn("City", item["destination_city"] ?? "N/A"), _infoColumn("Location", item["location_of_pickup"] ?? "N/A",alignEnd: true), ], ), const SizedBox(height: 16), // Second row: Date & Time Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ _infoColumn("Date", "${formatDate(item["date"]!)}" ?? "N/A"), _infoColumn("Time", "${formatTime(item["time"]!)}" ?? "N/A",alignEnd: true), ], ), const SizedBox(height: 16), // Comment label Text( "Comment", style: GoogleFonts.poppins( fontSize: 14, fontWeight: FontWeight.w600, ), ), const SizedBox(height: 8), // Comment box if (item["comments"] != null && item["comments"].toString().trim().isNotEmpty) Container( width: double.infinity, padding: const EdgeInsets.all(12), decoration: BoxDecoration( border: Border.all(color: const Color(0xFF7098DD), width: 1), borderRadius: BorderRadius.circular(6), ), child: Text( item["comments"] ?? "N/A", style: GoogleFonts.poppins(fontSize: 12, color: Colors.black87), ), )else const Text( "N/A", style: TextStyle(fontSize: 12), ), ] ) // Column( // crossAxisAlignment: CrossAxisAlignment.start, // children: [ // _buildRow("City:", item["destination_city"]), // SizedBox(width: 10), // _buildRow("Pickup:", item["location_of_pickup"]!), // SizedBox(width: 10), // _buildDateTimeRow( // "Date:", // formatDate(item["date"]!), // formatTime(item["time"]!), // ), // _buildRow("TaxiFor:", item["car_required_for"]!), // _buildRow("Comments:", item["comments"] ?? "N/A"), // ], // ), ], ), ), ); }, ); } Widget _buildComments(String title, String value) { // Define character limits based on title Map limits = { // "Special Request:": 30, "Comments:": 10, // Add more keys and limits if needed }; int limit = limits[title] ?? 50; // Default limit if title not found bool exceedsLimit = value.length > limit; String wrapText(String text, int maxLineLength) { final pattern = RegExp('.{1,$maxLineLength}(\\s+|\$)'); return pattern.allMatches(text).map((m) => m.group(0)!).join('\n'); } return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: exceedsLimit ? Tooltip( message: wrapText(value, 50), decoration: BoxDecoration( color: Colors.grey.shade200, // Black background borderRadius: BorderRadius.circular(6), ), textStyle: TextStyle( color: Colors.black, ), // Tooltip text color padding: EdgeInsets.all(8), preferBelow: false, child: Text( value.substring(0, limit) + "...", style: GoogleFonts.poppins( fontSize: 12, fontWeight: FontWeight.w600, ), overflow: TextOverflow.ellipsis, ), ) : Text( value, style: GoogleFonts.poppins( fontSize: 12, fontWeight: FontWeight.w600, ), ), ), ], ); } Widget _buildRow(String title, String value) { // Define character limits based on title Map limits = { // "Special Request:": 30, "Comments:": 10, // Add more keys and limits if needed }; int limit = limits[title] ?? 50; // Default limit if title not found bool exceedsLimit = value.length > limit; String wrapText(String text, int maxLineLength) { final pattern = RegExp('.{1,$maxLineLength}(\\s+|\$)'); return pattern.allMatches(text).map((m) => m.group(0)!).join('\n'); } return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( title, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 12), ), SizedBox(width: 8), Expanded( child: exceedsLimit ? Tooltip( message: wrapText(value, 50), decoration: BoxDecoration( color: Colors.grey.shade200, // Black background borderRadius: BorderRadius.circular(6), ), textStyle: TextStyle( color: Colors.black, ), // Tooltip text color padding: EdgeInsets.all(8), preferBelow: false, child: Text( value.substring(0, limit) + "...", style: TextStyle(fontSize: 12), overflow: TextOverflow.ellipsis, ), ) : Text(value, style: GoogleFonts.poppins(fontSize: 12)), ), ], ); } Widget _buildDateTimeRow(String title, String date, String time) { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( title, style: GoogleFonts.poppins(fontSize: 12, fontWeight: FontWeight.w600), ), SizedBox(width: 8), Expanded( child: Text("$date, $time", style: GoogleFonts.poppins(fontSize: 12)), ), ], ); } Widget _infoColumn(String label, String value, {bool alignEnd = false}) { return Column( // crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: alignEnd ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [ Text( label, style: GoogleFonts.poppins( fontSize: 12, fontWeight: FontWeight.w600, color: Colors.black87, ), ), const SizedBox(height: 4), Text( value, style: GoogleFonts.poppins( fontSize: 12, color: Colors.black87, ), ), ], ); } }