Newer
Older
import 'package:flutter/material.dart';
import 'loading_page.dart';
import '../widgets/main_layout.dart';
import '../data_classes.dart';
import '../server_requests/fetch_markers.dart';
import '../server_requests/fetch_relation.dart';
class DefaultPage extends StatefulWidget {
const DefaultPage({Key? key}) : super(key: key);
@override
_DefaultPageState createState() => _DefaultPageState();
}
class _DefaultPageState extends State<DefaultPage> {
late Timer _timer;
bool showBar = false;
bool serverConnection = true;
late Future<List<Measurement>> markerListFuture;
late Future<Uint8List> relationFuture;
@override
void initState() {
super.initState();
// Try to fetch measurement data from server
markerListFuture = fetchMeasurements().then((fetchResult) {
List<Measurement> measurements = fetchResult.measurements;
serverConnection = fetchResult.connected;
// Return the measurements
return measurements;
}).catchError((error) {
throw Exception("Failed to fetch measurements: $error");
});
// Attempt to fetch relation from server if app establishes connection
if (serverConnection){
relationFuture = fetchRelation();
} else { // Read last saved data
relationFuture = loadSavedRelation();
}
// Schedule fetchMarkerData to run periodically based on fetchInterval
const Duration interval = Duration(minutes: fetchInterval); // NB fetchInterval value to be determined
_timer = Timer.periodic(interval, (timer) {
fetchMeasurements();
});
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
/// Display message to user
void showConnectionMessage() {
showDialog(
context: context,
builder: (context) => AlertDialog(
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("Ok"),
)
],
title: const Center(
child: Text("No server connection")
),
contentPadding: const EdgeInsets.all(10.0),
content: const Text(
"The app may display outdated information. Use with caution!",
textAlign: TextAlign.center, // Align text center
),
),
);
}
@override
Widget build(BuildContext context) {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
appBar: AppBar(
backgroundColor: Colors.black87,
leading: IconButton(
icon: const Icon(
Icons.menu,
color: Colors.white54
),
onPressed: () {
// Not implemented
},
),
title: Text(
'Mjøsa',
style: regTextStyleBig,
),
actions: [
IconButton(
icon: const Icon(
Icons.search,
color: Colors.white54
),
onPressed: () {
setState(() {
showBar = !showBar;
});
},
),
],
),
body: FutureBuilder(
future: Future.wait([markerListFuture, relationFuture]),
builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
List<Measurement> markerList = snapshot.data![0] as List<Measurement>;
Uint8List relation = snapshot.data![1] as Uint8List;
return Container( // Return container with list view and background color
MapContainerWidget(
markerList: markerList,
relation: relation,
serverConnection: serverConnection,