geo-smart-app/lib/main.dart
2019-10-31 22:51:22 +07:00

150 lines
4.7 KiB
Dart

import 'dart:async';
import 'package:background_location/background_location.dart';
import 'package:flutter/material.dart';
import 'package:geo_app/model/position.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Geo Smart App',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Geo Smart App'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Completer<GoogleMapController> _controller = Completer();
double latitude = 0.0;
double longitude = 0.0;
double altitude = 0.0;
double accuracy = 0.0;
double bearing = 0.0;
double speed = 0.0;
String message = "Connecting to Server";
bool _finish = false;
Position _position;
@override
void initState() {
super.initState();
BackgroundLocation.startLocationService();
message = "Connecting to Server";
Position.getPosition().then((position) {
setState(() {
_position = position;
_finish = this._position.isValid();
});
}, onError: (error) {
message = "Something wrong";
});
}
static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(-6.914744, 107.609810),
zoom: 14.4746,
);
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
myLocationEnabled: true,
myLocationButtonEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
BackgroundLocation.getLocationUpdates((location) async {
setState(() {
this.latitude = location.latitude;
this.longitude = location.longitude;
this.accuracy = location.accuracy;
this.altitude = location.altitude;
this.bearing = location.bearing;
this.speed = location.speed;
if (this._position != null) {
if (this._position.isValid()) {
this._position.lat = this.latitude.toString();
this._position.lng = this.longitude.toString();
this._position.sendPosition();
}
}
});
// Make Camera Follow the Marker
// final GoogleMapController controller = await _controller.future;
// controller.animateCamera(
// CameraUpdate.newCameraPosition(
// new CameraPosition(
// target: LatLng(this.latitude, this.longitude),
// zoom: 19.151926040649414,
// ),
// ),
// );
});
},
),
Visibility(
visible: !_finish,
child: Container(
decoration: BoxDecoration(
color: Colors.black26.withOpacity(0.8),
),
child: Center(
child: Text(
message,
style: TextStyle(color: Colors.white),
),
),
),
)
],
),
);
}
@override
void dispose() {
BackgroundLocation.stopLocationService();
super.dispose();
}
}