change app name, add route calculator
This commit is contained in:
286
lib/components/calculator/calculator.dart
Normal file
286
lib/components/calculator/calculator.dart
Normal file
@@ -0,0 +1,286 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:mileograph_flutter/objects/objects.dart';
|
||||
import 'package:mileograph_flutter/services/apiService.dart';
|
||||
import 'package:mileograph_flutter/services/dataService.dart';
|
||||
import './routeSummaryWidget.dart';
|
||||
|
||||
class RouteCalculatorLoader extends StatelessWidget {
|
||||
const RouteCalculatorLoader({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final data = context.read<DataService>();
|
||||
|
||||
return FutureBuilder<List<Station>>(
|
||||
future: data.fetchStations(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return Center(child: Text('Error: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
final stations = snapshot.data!;
|
||||
return RouteCalculator(allStations: stations);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class StationAutocomplete extends StatefulWidget {
|
||||
const StationAutocomplete({
|
||||
super.key,
|
||||
required this.allStations,
|
||||
this.initialValue,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
final List<Station> allStations;
|
||||
final String? initialValue;
|
||||
final ValueChanged<String> onChanged;
|
||||
|
||||
@override
|
||||
State<StationAutocomplete> createState() => _StationAutocompleteState();
|
||||
}
|
||||
|
||||
class _StationAutocompleteState extends State<StationAutocomplete> {
|
||||
late final TextEditingController _controller;
|
||||
|
||||
// Simulated list of over 10,000 stations
|
||||
final List<String> stations = List.generate(10000, (i) => 'Station $i');
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = TextEditingController(text: widget.initialValue ?? '');
|
||||
_controller.addListener(() {
|
||||
widget.onChanged(_controller.text);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Autocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) {
|
||||
if (textEditingValue.text.isEmpty) {
|
||||
return const Iterable<String>.empty();
|
||||
}
|
||||
final query = textEditingValue.text.toLowerCase();
|
||||
final matches = widget.allStations
|
||||
.map((s) => s.name)
|
||||
.where((name) => name.toLowerCase().contains(query))
|
||||
.toList();
|
||||
|
||||
matches.sort((a, b) => a.length.compareTo(b.length));
|
||||
|
||||
return matches.take(10);
|
||||
},
|
||||
onSelected: (String selection) {
|
||||
_controller.text = selection;
|
||||
widget.onChanged(selection);
|
||||
},
|
||||
fieldViewBuilder:
|
||||
(context, textEditingController, focusNode, onFieldSubmitted) {
|
||||
textEditingController.value = _controller.value;
|
||||
return TextField(
|
||||
controller: textEditingController,
|
||||
focusNode: focusNode,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Select station',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RouteCalculator extends StatefulWidget {
|
||||
final List<Station> allStations;
|
||||
|
||||
const RouteCalculator({super.key, required this.allStations});
|
||||
|
||||
@override
|
||||
State<RouteCalculator> createState() => _RouteCalculatorState();
|
||||
}
|
||||
|
||||
class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
List<String> stations = [''];
|
||||
|
||||
RouteResult? _routeResult;
|
||||
RouteResult? get result => _routeResult;
|
||||
String? _errorMessage;
|
||||
|
||||
bool _showDetails = false;
|
||||
|
||||
Future<void> _calculateRoute(List<String> stations) async {
|
||||
setState(() {
|
||||
_errorMessage = null;
|
||||
_routeResult = null;
|
||||
});
|
||||
final api = context.read<ApiService>(); // context is valid here
|
||||
final res = await api.post('/route/distance2', {
|
||||
'route': stations.where((s) => s.trim().isNotEmpty).toList(),
|
||||
});
|
||||
|
||||
if (res['error'] == false) {
|
||||
setState(() {
|
||||
_routeResult = RouteResult.fromJson(res);
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
_errorMessage =
|
||||
RouteError.fromJson(res["error_obj"][0]).msg ??
|
||||
'Unknown error occurred';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _addStation() {
|
||||
setState(() {
|
||||
stations.add('');
|
||||
});
|
||||
}
|
||||
|
||||
void _removeStation(int index) {
|
||||
setState(() {
|
||||
stations.removeAt(index);
|
||||
});
|
||||
}
|
||||
|
||||
void _updateStation(int index, String value) {
|
||||
setState(() {
|
||||
stations[index] = value;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_showDetails && _routeResult != null) {
|
||||
return RouteDetailsView(
|
||||
route: _routeResult!.calculatedRoute,
|
||||
costs: _routeResult!.costs,
|
||||
onBack: () => setState(() => _showDetails = false),
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ReorderableListView(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
onReorder: (oldIndex, newIndex) {
|
||||
if (newIndex > oldIndex) newIndex -= 1;
|
||||
setState(() {
|
||||
final moved = stations.removeAt(oldIndex);
|
||||
stations.insert(newIndex, moved);
|
||||
});
|
||||
},
|
||||
children: List.generate(stations.length, (index) {
|
||||
return Container(
|
||||
key: ValueKey('$index-${stations[index]}'),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ReorderableDragStartListener(
|
||||
index: index,
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Icon(Icons.drag_indicator),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: StationAutocomplete(
|
||||
allStations: widget.allStations,
|
||||
initialValue: stations[index],
|
||||
onChanged: (val) =>
|
||||
_updateStation(index, val),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: () => _removeStation(index),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
if (_errorMessage != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
_errorMessage!,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
)
|
||||
else if (_routeResult != null)
|
||||
RouteSummaryWidget(
|
||||
distance: _routeResult!.distance,
|
||||
onDetailsPressed: () => setState(() => _showDetails = true),
|
||||
)
|
||||
else
|
||||
SizedBox.shrink(),
|
||||
const SizedBox(height: 10),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton.icon(
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('Add Station'),
|
||||
onPressed: _addStation,
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
ElevatedButton.icon(
|
||||
icon: const Icon(Icons.route),
|
||||
label: const Text('Calculate Route'),
|
||||
onPressed: () async {
|
||||
await _calculateRoute(stations);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget debugPanel(List<String> stations) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
||||
child: Wrap(
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
spacing: 8,
|
||||
children: [
|
||||
const Text(
|
||||
'Current Order:',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
...stations.map((s) => Chip(label: Text(s.isEmpty ? '<empty>' : s))),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user