Fakultas Ilmu Komputer UI

dashboard.dart 5.97 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
import 'package:ppl_disabilitas/page/pencarian/pencarian.dart';
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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;
Fakhira Devina's avatar
Fakhira Devina committed
23
24
25
26
27
28
29
30
31
32
  bool _serviceEnabled;
  PermissionStatus _permissionGranted;
  Marker contohMarker = Marker(
    markerId: MarkerId("contoh1"),
    position: LatLng(-6.365474, 106.828157),
    infoWindow: InfoWindow(title: "Fasilkom"),
    icon: BitmapDescriptor.defaultMarkerWithHue(
      BitmapDescriptor.hueViolet,
    ),
  );
33
34
35
36

  @override
  void initState() {
    super.initState();
37
    location = Location();
38
    enableLocationService();
39
40
41
42
43
44
    location.onLocationChanged().listen((LocationData cLoc) {
      currentLocation = cLoc;
      updatePinOnMap();
    });
    setSourceAndDestinationIcons();
    setInitialLocation();
45
    _markers.add(contohMarker);
46
47
  }

48
  void enableLocationService() async {
49
    await location.changeSettings(accuracy: LocationAccuracy.HIGH);
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    _serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {
      _serviceEnabled = await location.requestService();
      if (!_serviceEnabled) {
        return;
      }
    }
    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.DENIED) {
      _permissionGranted = await location.requestPermission();
      if (_permissionGranted != PermissionStatus.GRANTED) {
        return;
      }
    }
  }

66
    void _navigateToPencarianPage(BuildContext context) {
67
68
69
70
    final route = MaterialPageRoute(builder: (_) => Pencarian());
    Navigator.of(context).push(route);
  }

71
  static const textFieldKey = Key('Text Field Mau Kemana');
72
73
74
  @override
  Widget build(BuildContext context) {
    return Scaffold(
75
      drawer: BisaGoDrawer(),
76
      body: Stack(key: Key("Stack"),children: <Widget>[
77
        _buildGoogleMap(context),
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
108
109
110
111
112
113
        InkWell(
          key: Key("Navigate to Pencarian"),
          onTap: () => _navigateToPencarianPage(context),
          child: Container(
            key: Key("Container Text Field"),
            margin: EdgeInsets.only(
                left: doubleSpace, right: doubleSpace, top: doubleSpace),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: doubleBorderRadius,
                boxShadow: regularShadow),
            child: TextFormField(
              enabled: false,
              key: Key("Text Field Mau Kemana"),
              decoration: InputDecoration(
                  prefixIcon: Icon(
                    Icons.search,
                    color: greenPrimary,
                    size: 25,
                  ),
                  border: InputBorder.none,
                  fillColor: Colors.white,
                  labelText: 'Kamu mau kemana?',
                  labelStyle: TextStyle(
                      color: greenPrimary,
                      fontSize: 20,
                      fontFamily: 'Muli',
                      fontWeight: FontWeight.w700),
                  suffixIcon: IconButton(
                      key: Key("IconButton Text Field"),
                      icon: Icon(
                        Icons.mic,
                        color: greenPrimary,
                        size: 25,
                      ),
                      onPressed: () {})),
114
              onTap: () => _navigateToPencarianPage(context),
115
            ),
116
117
118
          ),
        ),
      ]),
119
      appBar: PreferredSize(
120
        preferredSize: Size.fromHeight(55),
121
        child: BisaGoAppBar(),
Agnes Handoko's avatar
Agnes Handoko committed
122
123
        key: Key("Scaffold Text Field"),
      ),
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    );
  }

  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(
Agnes Handoko's avatar
Agnes Handoko committed
139
      key: Key("Container GoogleMap"),
140
141
142
      height: MediaQuery.of(context).size.height,
      width: MediaQuery.of(context).size.width,
      child: GoogleMap(
Agnes Handoko's avatar
Agnes Handoko committed
143
        key: Key("Google Map"),
144
        markers: _markers,
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        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;
165
    await controller.animateCamera(CameraUpdate.newCameraPosition(cPosition));
166
167
168
169
170
171
    setState(() {
      var pinPosition =
          LatLng(currentLocation.latitude, currentLocation.longitude);
      _markers.removeWhere((m) => m.markerId.value == 'currentLocationPin');
      _markers.add(Marker(
          markerId: MarkerId('currentLocationPin'),
172
          position: pinPosition,
173
174
175
176
177
178
179
180
          icon: currentLocationIcon));
    });
  }

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