Fakultas Ilmu Komputer UI

dashboard.dart 3.73 KB
Newer Older
1
2
3
4
import 'dart:async';
import 'package:location/location.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
5
6
7
import 'package:ppl_disabilitas/component/bisago_appbar.dart';
import 'package:ppl_disabilitas/component/bisago_drawer.dart';
import 'package:ppl_disabilitas/config/styles.dart';
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

class Dashboard extends StatefulWidget {
  DashboardState createState() => DashboardState();
}

class DashboardState extends State<Dashboard> {
  final Completer<GoogleMapController> _controller = Completer();
  final double cameraZoom = 16;
  final LatLng defaultLocation = LatLng(-6.1753924, 106.8249641);
  final String currentLocationIconAsset = "assets/icon/current_loc.png";
  Location location;
  Set<Marker> _markers = Set<Marker>();
  LocationData currentLocation;
  BitmapDescriptor currentLocationIcon;

  @override
  void initState() {
    super.initState();
    location = new Location();
    location.onLocationChanged().listen((LocationData cLoc) {
      currentLocation = cLoc;
      updatePinOnMap();
    });
    setSourceAndDestinationIcons();
    setInitialLocation();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
38
      drawer: BisaGoDrawer(),
39
40
41
      body: Stack(children: <Widget>[
        _buildGoogleMap(context),
        Container(
42
43
          margin: EdgeInsets.only(
              left: doubleSpace, right: doubleSpace, top: doubleSpace),
44
45
46
47
48
          color: Colors.white,
          child: TextFormField(
            decoration: InputDecoration(
                prefixIcon: Icon(Icons.search),
                border: OutlineInputBorder(
49
                    borderRadius: regularBorderRadius,
50
51
52
                    borderSide:
                        BorderSide(color: Colors.transparent, width: 0)),
                fillColor: Colors.white,
53
54
55
56
57
                labelText: 'Kamu mau kemana?',
                suffixIcon: IconButton(
                  icon: Icon(Icons.mic),
                  onPressed: () {},
                )),
58
59
60
          ),
        ),
      ]),
61
      appBar: BisaGoAppBar(),
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    );
  }

  Widget _buildGoogleMap(BuildContext context) {
    CameraPosition initialCameraPosition = CameraPosition(
      target: defaultLocation,
      zoom: cameraZoom,
    );
    if (currentLocation != null) {
      initialCameraPosition = CameraPosition(
        target: LatLng(currentLocation.latitude, currentLocation.longitude),
        zoom: cameraZoom,
      );
    }
    return Container(
      height: MediaQuery.of(context).size.height,
      width: MediaQuery.of(context).size.width,
      child: GoogleMap(
        markers: _markers,
        mapType: MapType.normal,
        initialCameraPosition: initialCameraPosition,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
    );
  }

  void setSourceAndDestinationIcons() async {
    currentLocationIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(devicePixelRatio: 5), currentLocationIconAsset);
  }

  void updatePinOnMap() async {
    CameraPosition cPosition = CameraPosition(
      zoom: cameraZoom,
      target: LatLng(currentLocation.latitude, currentLocation.longitude),
    );
    final GoogleMapController controller = await _controller.future;
    controller.animateCamera(CameraUpdate.newCameraPosition(cPosition));
    setState(() {
      var pinPosition =
          LatLng(currentLocation.latitude, currentLocation.longitude);
      _markers.removeWhere((m) => m.markerId.value == 'currentLocationPin');
      _markers.add(Marker(
          markerId: MarkerId('currentLocationPin'),
108
          position: pinPosition,
109
110
111
112
113
114
115
116
          icon: currentLocationIcon));
    });
  }

  void setInitialLocation() async {
    currentLocation = await location.getLocation();
  }
}