538 lines
18 KiB
Dart
538 lines
18 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import '../../services/apiService.dart';
|
|
|
|
class TrainListWidget extends StatefulWidget {
|
|
final bool hasAction;
|
|
final String? tripType;
|
|
final List<Map<String, dynamic>> trainList;
|
|
final Function(bool, Map<String, dynamic>, String) onOpen;
|
|
final Function(Map<String, dynamic>) onDeleteTrain;
|
|
final Function(String, bool) onAddNew;
|
|
final bool isViewMode;
|
|
final Map<String, dynamic>? apiData;
|
|
|
|
const TrainListWidget({
|
|
Key? key,
|
|
required this.trainList,
|
|
required this.onOpen,
|
|
required this.onDeleteTrain,
|
|
required this.onAddNew,
|
|
required this.isViewMode,
|
|
required this.apiData,
|
|
required this.hasAction,
|
|
this.tripType,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_TrainListWidgetState createState() => _TrainListWidgetState();
|
|
}
|
|
|
|
class _TrainListWidgetState extends State<TrainListWidget> {
|
|
ApiService apiService = ApiService();
|
|
|
|
// late Map<String, String> countryMap;
|
|
Map<String, String> countryMap = {};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
loadCountryList(); // Call your method here
|
|
}
|
|
|
|
void checkClass() {
|
|
if (widget.hasAction) {
|
|
if (widget.tripType?.isNotEmpty == true) {
|
|
print("Teppp - $widget.tripType");
|
|
widget.onAddNew("Train", true);
|
|
} else {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: const Text('Select Trip Type'),
|
|
content: const Text(
|
|
'Please select a trip type before adding a Train.'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
} else {
|
|
print("Teppp No - $widget.tripType");
|
|
widget.onAddNew("Flight", true);
|
|
}
|
|
}
|
|
|
|
Future<void> loadCountryList() async {
|
|
// final result = await apiService.fetchFlightsCountryList();
|
|
final result = await apiService.fetchTrainCountryList();
|
|
|
|
print("ResultCountry : $result");
|
|
|
|
// Create a map: Country_Code -> "City, Airport"
|
|
Map<String, String> tempCountryMap = {};
|
|
|
|
for (var country in result) {
|
|
String displayName = '${country['Station_Name']}';
|
|
// String displayName = '${country['City']} | ${country['Airport']}';
|
|
|
|
// tempCountryMap[country['Code']] = displayName;
|
|
tempCountryMap[country['Station_Code']] = displayName;
|
|
}
|
|
|
|
setState(() {
|
|
countryMap = tempCountryMap; // Update the map
|
|
});
|
|
}
|
|
|
|
@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: [
|
|
MouseRegion(
|
|
cursor: widget.isViewMode
|
|
? SystemMouseCursors.forbidden
|
|
: SystemMouseCursors.click,
|
|
child: MouseRegion(
|
|
cursor: widget.isViewMode
|
|
? SystemMouseCursors.forbidden
|
|
: SystemMouseCursors.click,
|
|
|
|
child: GestureDetector(
|
|
onTap: widget.isViewMode
|
|
? null
|
|
: () {
|
|
print("New data");
|
|
checkClass();
|
|
},
|
|
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),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
// onPressed: isViewMode
|
|
// ? null
|
|
// : () {
|
|
// print("New data");
|
|
//
|
|
// },
|
|
// child: Row(
|
|
// mainAxisSize: MainAxisSize.min,
|
|
// children: [
|
|
// 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("Train", 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<Map<String, dynamic>> filteredList =
|
|
widget.trainList.where((item) => item["is_active"] == "1").toList();
|
|
// print("filteredList- $filteredList");
|
|
|
|
List<dynamic> trainClassList = widget.apiData?['train_class'] ?? [];
|
|
|
|
String getRequestForTrainClass(String? specialRequestKey) {
|
|
if (specialRequestKey == null) return "N/A";
|
|
|
|
return trainClassList
|
|
.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";
|
|
}
|
|
}
|
|
|
|
return ListView.builder(
|
|
shrinkWrap: true,
|
|
physics: NeverScrollableScrollPhysics(),
|
|
itemCount: filteredList.length,
|
|
itemBuilder: (context, index) {
|
|
final item = filteredList[index];
|
|
|
|
String fromPlaceCountry =
|
|
countryMap[item["from_station"].toString()] ?? "Unknown Country";
|
|
String toPlaceCountry =
|
|
countryMap[item["to_station"].toString()] ?? "Unknown Country";
|
|
|
|
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(
|
|
item["train_no"]?.toString() ?? "N/A",
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w800,
|
|
fontFamily: "Archivo"),
|
|
),
|
|
Spacer(),
|
|
GestureDetector(
|
|
onTap: () => widget.onOpen(true, item, "Train"),
|
|
child: Image.asset('assets/images/IconsImg/edit.png',
|
|
width: 20, height: 15),
|
|
),
|
|
SizedBox(width: 10),
|
|
GestureDetector(
|
|
onTap: () => widget.onDeleteTrain(item),
|
|
child: Image.asset('assets/images/IconsImg/delete.png',
|
|
width: 20, height: 15),
|
|
),
|
|
],
|
|
),
|
|
Divider(
|
|
color: Colors.blueGrey.shade50,
|
|
),
|
|
SizedBox(height: 4),
|
|
isDesktop
|
|
? Row(
|
|
children: [
|
|
Expanded(
|
|
flex: 2,
|
|
child: Text(
|
|
" Class",
|
|
style: TextStyle(
|
|
fontSize: 11, fontFamily: "Archivo"),
|
|
)),
|
|
Expanded(
|
|
flex: 2,
|
|
child: Text(
|
|
" Planned Trips",
|
|
style: TextStyle(
|
|
fontSize: 11, fontFamily: "Archivo"),
|
|
)),
|
|
Expanded(
|
|
flex: 2,
|
|
child: Text(
|
|
" Date",
|
|
style: TextStyle(
|
|
fontSize: 11, fontFamily: "Archivo"),
|
|
)),
|
|
Expanded(
|
|
flex: 2,
|
|
child: Text(
|
|
" Comments",
|
|
style: TextStyle(
|
|
fontSize: 11, fontFamily: "Archivo"),
|
|
)),
|
|
],
|
|
)
|
|
: SizedBox.shrink(),
|
|
SizedBox(height: 4),
|
|
isDesktop
|
|
? Row(
|
|
children: [
|
|
Expanded(
|
|
flex: 2,
|
|
child: Text(
|
|
"${getRequestForTrainClass(item["class"]!.toString())} ",
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
fontFamily: "Archivo")),
|
|
),
|
|
SizedBox(width: 10),
|
|
Expanded(
|
|
flex: 2,
|
|
child: Text("$fromPlaceCountry - $toPlaceCountry",
|
|
|
|
// "${item["from_station"]!} - ${(item["to_station"])}",
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
fontFamily: "Archivo"))),
|
|
SizedBox(width: 10),
|
|
Expanded(
|
|
flex: 2,
|
|
child: Text(
|
|
"${formatDate(item["date"]!)} ${formatTime(item["time"]!)}",
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
fontFamily: "Archivo")),
|
|
),
|
|
SizedBox(width: 10),
|
|
Expanded(
|
|
flex: 2,
|
|
child: _buildComments(
|
|
"Comments:", item["comments"] ?? "N/A"),
|
|
),
|
|
],
|
|
)
|
|
: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_buildRow(
|
|
"Class:",
|
|
getRequestForTrainClass(
|
|
item["class"]!.toString())),
|
|
_buildRow("From:", item["from_station"]!),
|
|
_buildRow("To:", item["to_station"]!),
|
|
_buildDateTimeRow(
|
|
"Date:",
|
|
formatDate(item["date"]!),
|
|
formatTime(item["time"]!),
|
|
),
|
|
_buildRow("Comments:", item["comments"] ?? "N/A"),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildComments(String title, String value) {
|
|
// Define character limits based on title
|
|
Map<String, int> limits = {
|
|
// "Special Request:": 30,
|
|
"Comments:": 30,
|
|
// 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: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
fontFamily: "Archivo"),
|
|
overflow: TextOverflow.ellipsis),
|
|
)
|
|
: Text(
|
|
value,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
fontFamily: "Archivo"),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildRow(String title, String value) {
|
|
// Define character limits based on title
|
|
Map<String, int> 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: TextStyle(fontSize: 12),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildDateTimeRow(String title, String date, String time) {
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
title,
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(
|
|
"$date, $time",
|
|
style: TextStyle(fontSize: 12),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|