Fakultas Ilmu Komputer UI

dashboard.dart 3.96 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
import 'package:ppl_disabilitas/component/bisago_appbar.dart';
import 'package:ppl_disabilitas/component/bisago_drawer.dart';
7
import 'package:ppl_disabilitas/config/strings.dart';
8
import 'package:ppl_disabilitas/config/styles.dart';
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
38

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(
39
      drawer: BisaGoDrawer(),
40
41
42
      body: Stack(children: <Widget>[
        _buildGoogleMap(context),
        Container(
43
          key: Key("Container Text Field"),
44
45
          margin: EdgeInsets.only(
              left: doubleSpace, right: doubleSpace, top: doubleSpace),
46
47
          color: Colors.white,
          child: TextFormField(
48
            key: Key("Text Field Mau Kemana"),
49
50
51
            decoration: InputDecoration(
                prefixIcon: Icon(Icons.search),
                border: OutlineInputBorder(
52
                    borderRadius: regularBorderRadius,
53
54
55
                    borderSide:
                        BorderSide(color: Colors.transparent, width: 0)),
                fillColor: Colors.white,
56
57
                labelText: 'Kamu mau kemana?',
                suffixIcon: IconButton(
58
                  key: Key("IconButton Text Field"),
59
60
61
                  icon: Icon(Icons.mic),
                  onPressed: () {},
                )),
62
63
64
          ),
        ),
      ]),
65
      appBar: BisaGoAppBar(),
66
      key: Key("Scaffold Text Field"),
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
108
109
110
111
112
    );
  }

  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'),
113
          position: pinPosition,
114
115
116
117
118
119
120
121
          icon: currentLocationIcon));
    });
  }

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