support new fields in adding
All checks were successful
All checks were successful
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:mileograph_flutter/objects/objects.dart';
|
||||
@@ -38,19 +36,72 @@ class _LegCardState extends State<LegCard> {
|
||||
title: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isWide = constraints.maxWidth > 520;
|
||||
final routeText = Text('${leg.start} → ${leg.end}');
|
||||
final timeText =
|
||||
Text(_formatDateTime(leg.beginTime, includeDate: widget.showDate));
|
||||
final beginTimeWidget = _timeWithDelay(
|
||||
context,
|
||||
leg.beginTime,
|
||||
leg.beginDelayMinutes,
|
||||
includeDate: widget.showDate,
|
||||
);
|
||||
final endTimeWidget = leg.endTime == null
|
||||
? null
|
||||
: _timeWithDelay(
|
||||
context,
|
||||
leg.endTime!,
|
||||
leg.endDelayMinutes,
|
||||
includeDate: widget.showDate,
|
||||
);
|
||||
|
||||
final routeText = Text(
|
||||
'${leg.start} → ${leg.end}',
|
||||
softWrap: true,
|
||||
);
|
||||
if (!isWide) {
|
||||
return routeText;
|
||||
final timeStyle = Theme.of(context).textTheme.labelSmall;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
routeText,
|
||||
const SizedBox(height: 2),
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 4,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
_timeWithDelay(
|
||||
context,
|
||||
leg.beginTime,
|
||||
leg.beginDelayMinutes,
|
||||
includeDate: widget.showDate,
|
||||
style: timeStyle,
|
||||
),
|
||||
if (endTimeWidget != null) ...[
|
||||
const Text('·'),
|
||||
_timeWithDelay(
|
||||
context,
|
||||
leg.endTime!,
|
||||
leg.endDelayMinutes,
|
||||
includeDate: widget.showDate,
|
||||
style: timeStyle,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return Row(
|
||||
|
||||
return Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 4,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
timeText,
|
||||
const SizedBox(width: 6),
|
||||
beginTimeWidget,
|
||||
const Text('·'),
|
||||
const SizedBox(width: 6),
|
||||
Expanded(child: routeText),
|
||||
routeText,
|
||||
if (endTimeWidget != null) ...[
|
||||
const Text('·'),
|
||||
endTimeWidget,
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
@@ -58,8 +109,12 @@ class _LegCardState extends State<LegCard> {
|
||||
subtitle: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isWide = constraints.maxWidth > 520;
|
||||
final timeWidget =
|
||||
Text(_formatDateTime(leg.beginTime, includeDate: widget.showDate));
|
||||
final timeWidget = _timeWithDelay(
|
||||
context,
|
||||
leg.beginTime,
|
||||
leg.beginDelayMinutes,
|
||||
includeDate: widget.showDate,
|
||||
);
|
||||
final tractionWrap = !_expanded && leg.locos.isNotEmpty
|
||||
? Wrap(
|
||||
spacing: 8,
|
||||
@@ -90,9 +145,7 @@ class _LegCardState extends State<LegCard> {
|
||||
children.add(tractionWrap);
|
||||
}
|
||||
} else {
|
||||
children.add(timeWidget);
|
||||
if (tractionWrap != null) {
|
||||
children.add(const SizedBox(height: 4));
|
||||
children.add(tractionWrap);
|
||||
}
|
||||
}
|
||||
@@ -191,6 +244,12 @@ class _LegCardState extends State<LegCard> {
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
if (_hasTrainDetails(leg)) ...[
|
||||
Text('Train', style: textTheme.titleSmall),
|
||||
const SizedBox(height: 6),
|
||||
..._buildTrainDetails(leg, textTheme),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
if (routeSegments.isNotEmpty) ...[
|
||||
Text('Route', style: textTheme.titleSmall),
|
||||
const SizedBox(height: 6),
|
||||
@@ -238,6 +297,40 @@ class _LegCardState extends State<LegCard> {
|
||||
}
|
||||
}
|
||||
|
||||
Widget _timeWithDelay(
|
||||
BuildContext context,
|
||||
DateTime time,
|
||||
int? delay, {
|
||||
bool includeDate = true,
|
||||
TextStyle? style,
|
||||
}) {
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
final delayMinutes = delay ?? 0;
|
||||
final delayText =
|
||||
delayMinutes == 0 ? null : '${delayMinutes > 0 ? '+' : ''}$delayMinutes';
|
||||
final delayColor = delayMinutes == 0
|
||||
? null
|
||||
: (delayMinutes < 0 ? Colors.green : colorScheme.error);
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
_formatDateTime(time, includeDate: includeDate),
|
||||
style: style,
|
||||
),
|
||||
if (delayText != null) ...[
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'$delayText m',
|
||||
style:
|
||||
(style ?? textTheme.labelSmall)?.copyWith(color: delayColor),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime? date) {
|
||||
if (date == null) return '';
|
||||
return '${date.year.toString().padLeft(4, '0')}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
@@ -286,6 +379,51 @@ class _LegCardState extends State<LegCard> {
|
||||
.toList();
|
||||
}
|
||||
|
||||
bool _hasTrainDetails(Leg leg) {
|
||||
return leg.headcode.isNotEmpty ||
|
||||
leg.origin.isNotEmpty ||
|
||||
leg.destination.isNotEmpty ||
|
||||
leg.originTime != null ||
|
||||
leg.destinationTime != null;
|
||||
}
|
||||
|
||||
List<Widget> _buildTrainDetails(Leg leg, TextTheme textTheme) {
|
||||
final widgets = <Widget>[];
|
||||
if (leg.headcode.isNotEmpty) {
|
||||
widgets.add(
|
||||
Text(
|
||||
'Headcode: ${leg.headcode}',
|
||||
style: textTheme.bodyMedium,
|
||||
),
|
||||
);
|
||||
}
|
||||
final originLine = _locationLine(
|
||||
'Origin',
|
||||
leg.origin,
|
||||
leg.originTime,
|
||||
);
|
||||
if (originLine != null) {
|
||||
widgets.add(Text(originLine, style: textTheme.bodyMedium));
|
||||
}
|
||||
final destinationLine = _locationLine(
|
||||
'Destination',
|
||||
leg.destination,
|
||||
leg.destinationTime,
|
||||
);
|
||||
if (destinationLine != null) {
|
||||
widgets.add(Text(destinationLine, style: textTheme.bodyMedium));
|
||||
}
|
||||
return widgets;
|
||||
}
|
||||
|
||||
String? _locationLine(String label, String location, DateTime? time) {
|
||||
final parts = <String>[];
|
||||
if (location.trim().isNotEmpty) parts.add(location.trim());
|
||||
if (time != null) parts.add(_formatDateTime(time));
|
||||
if (parts.isEmpty) return null;
|
||||
return '$label: ${parts.join(' · ')}';
|
||||
}
|
||||
|
||||
Widget _buildRouteList(List<String> segments) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -306,38 +444,7 @@ class _LegCardState extends State<LegCard> {
|
||||
);
|
||||
}
|
||||
|
||||
List<String> _parseRouteSegments(String route) {
|
||||
final trimmed = route.trim();
|
||||
if (trimmed.isEmpty) return [];
|
||||
try {
|
||||
final decoded = jsonDecode(trimmed);
|
||||
if (decoded is List) {
|
||||
return decoded.map((e) => e.toString()).toList();
|
||||
}
|
||||
} catch (_) {}
|
||||
if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
|
||||
try {
|
||||
final replaced = trimmed.replaceAll("'", '"');
|
||||
final decoded = jsonDecode(replaced);
|
||||
if (decoded is List) {
|
||||
return decoded.map((e) => e.toString()).toList();
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
if (trimmed.contains('->')) {
|
||||
return trimmed
|
||||
.split('->')
|
||||
.map((e) => e.trim())
|
||||
.where((e) => e.isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
if (trimmed.contains(',')) {
|
||||
return trimmed
|
||||
.split(',')
|
||||
.map((e) => e.trim())
|
||||
.where((e) => e.isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
return [trimmed];
|
||||
List<String> _parseRouteSegments(List<String> route) {
|
||||
return route.map((e) => e.toString()).where((e) => e.trim().isNotEmpty).toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user