add back button functionality
This commit is contained in:
@@ -5,31 +5,6 @@ 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,
|
||||
@@ -105,16 +80,14 @@ class _StationAutocompleteState extends State<StationAutocomplete> {
|
||||
}
|
||||
|
||||
class RouteCalculator extends StatefulWidget {
|
||||
final List<Station> allStations;
|
||||
|
||||
const RouteCalculator({super.key, required this.allStations});
|
||||
const RouteCalculator({super.key});
|
||||
|
||||
@override
|
||||
State<RouteCalculator> createState() => _RouteCalculatorState();
|
||||
}
|
||||
|
||||
class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
List<String> stations = [''];
|
||||
List<Station> allStations = [];
|
||||
|
||||
RouteResult? _routeResult;
|
||||
RouteResult? get result => _routeResult;
|
||||
@@ -122,6 +95,23 @@ class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
|
||||
bool _showDetails = false;
|
||||
|
||||
bool _fetched = false;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
if (!_fetched) {
|
||||
_fetched = true;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
final data = context.read<DataService>();
|
||||
final result = await data.fetchStations();
|
||||
if (mounted) {
|
||||
setState(() => allStations = result);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _calculateRoute(List<String> stations) async {
|
||||
setState(() {
|
||||
_errorMessage = null;
|
||||
@@ -146,25 +136,29 @@ class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
}
|
||||
|
||||
void _addStation() {
|
||||
final data = context.read<DataService>();
|
||||
setState(() {
|
||||
stations.add('');
|
||||
data.stations.add('');
|
||||
});
|
||||
}
|
||||
|
||||
void _removeStation(int index) {
|
||||
final data = context.read<DataService>();
|
||||
setState(() {
|
||||
stations.removeAt(index);
|
||||
data.stations.removeAt(index);
|
||||
});
|
||||
}
|
||||
|
||||
void _updateStation(int index, String value) {
|
||||
final data = context.read<DataService>();
|
||||
setState(() {
|
||||
stations[index] = value;
|
||||
data.stations[index] = value;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final data = context.watch<DataService>();
|
||||
if (_showDetails && _routeResult != null) {
|
||||
return RouteDetailsView(
|
||||
route: _routeResult!.calculatedRoute,
|
||||
@@ -180,13 +174,13 @@ class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
onReorder: (oldIndex, newIndex) {
|
||||
if (newIndex > oldIndex) newIndex -= 1;
|
||||
setState(() {
|
||||
final moved = stations.removeAt(oldIndex);
|
||||
stations.insert(newIndex, moved);
|
||||
final moved = data.stations.removeAt(oldIndex);
|
||||
data.stations.insert(newIndex, moved);
|
||||
});
|
||||
},
|
||||
children: List.generate(stations.length, (index) {
|
||||
children: List.generate(data.stations.length, (index) {
|
||||
return Container(
|
||||
key: ValueKey('$index-${stations[index]}'),
|
||||
key: ValueKey('$index-${data.stations[index]}'),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -207,8 +201,8 @@ class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
children: [
|
||||
Expanded(
|
||||
child: StationAutocomplete(
|
||||
allStations: widget.allStations,
|
||||
initialValue: stations[index],
|
||||
allStations: allStations,
|
||||
initialValue: data.stations[index],
|
||||
onChanged: (val) =>
|
||||
_updateStation(index, val),
|
||||
),
|
||||
@@ -244,24 +238,34 @@ class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
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);
|
||||
},
|
||||
),
|
||||
],
|
||||
LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
double screenWidth = constraints.maxWidth;
|
||||
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(right: screenWidth < 450 ? 70 : 0),
|
||||
child: 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(data.stations);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
);
|
||||
|
||||
@@ -5,6 +5,6 @@ import 'package:mileograph_flutter/services/dataService.dart';
|
||||
|
||||
class CalculatorPage extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return RouteCalculatorLoader();
|
||||
return RouteCalculator();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,11 @@ class DashboardHeader extends StatelessWidget {
|
||||
Expanded(
|
||||
child: ListView(
|
||||
scrollDirection: Axis.vertical,
|
||||
children: [TopTractionPanel(), LeaderboardPanel()],
|
||||
children: [
|
||||
TopTractionPanel(),
|
||||
LeaderboardPanel(),
|
||||
SizedBox(height: 80),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user