Fakultas Ilmu Komputer UI

dashboard.dart 7.79 KB
Newer Older
1
2
3
4
import 'package:bisaGo/bloc/lokasi_response_bloc.dart';
import 'package:bisaGo/model/lokasi.dart';
import 'package:bisaGo/network/data/network_model.dart';
import 'package:bisaGo/utils/custom_dashboard_location_button.dart';
5
import 'package:flutter/material.dart';
6
import 'package:geolocator/geolocator.dart';
7
import 'package:google_maps_flutter/google_maps_flutter.dart';
8
9
10
11
import 'package:bisaGo/component/bisago_appbar.dart';
import 'package:bisaGo/component/bisago_drawer.dart';
import 'package:bisaGo/config/styles.dart';
import 'package:bisaGo/page/pencarian/pencarian.dart';
12
13

class Dashboard extends StatefulWidget {
14
  @override
15
16
17
18
  DashboardState createState() => DashboardState();
}

class DashboardState extends State<Dashboard> {
Fakhira Devina's avatar
Fakhira Devina committed
19
  final double cameraZoom = 15;
20
21
22
  GoogleMapController mapController;
  Geolocator geolocator;
  LatLng currentLocation;
23
  Set<Marker> markers = {};
24

25
26
  LokasiResponseBloc bloc = LokasiResponseBloc();

27
28
29
  @override
  void initState() {
    super.initState();
30
    geolocator = Geolocator()..forceAndroidLocationManager;
31
    setInitialLocation();
32
33
  }

34
  void _navigateToPencarianPage(BuildContext context) {
35
36
37
38
    final route = MaterialPageRoute(builder: (_) => Pencarian());
    Navigator.of(context).push(route);
  }

39
40
41
42
  // void _navigateToInformasiLayananDisabilitasPage(BuildContext context) {
  //   final route = MaterialPageRoute(builder: (_) => ListSekolah());
  //   Navigator.of(context).push(route);
  // }
43

44
  static const textFieldKey = Key('Text Field Mau Kemana');
45
46
47
  @override
  Widget build(BuildContext context) {
    return Scaffold(
48
      drawer: BisaGoDrawer(),
49
50
51
52
53
54
55
56
57
58
59
60
      body: Stack(
        key: const Key('Stack'),
        alignment: AlignmentDirectional.bottomStart,
        children: <Widget>[
          _buildGoogleMap(context),
          Container(
            height: 285,
            alignment: Alignment.bottomCenter,
            decoration: const BoxDecoration(
              color: white,
              borderRadius: BorderRadius.all(
                Radius.circular(10.0),
61
              ),
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
            padding: const EdgeInsets.symmetric(
                horizontal: doubleSpace, vertical: doubleSpace),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                InkWell(
                  key: const Key('Navigate to Pencarian'),
                  onTap: () => _navigateToPencarianPage(context),
                  child: Container(
                    key: const Key('Container Text Field'),
                    padding: EdgeInsets.zero,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: regularBorderRadius,
                        border: Border.all(color: greenPrimary),
                        boxShadow: regularShadow),
                    child: TextFormField(
                      enabled: false,
                      key: const Key('Text Field Mau Kemana'),
                      decoration: InputDecoration(
                        prefixIcon: Icon(
                          Icons.search,
                          color: greenPrimary,
                          size: 25,
                        ),
                        border: InputBorder.none,
                        fillColor: Colors.white,
                        labelText: 'Tekan untuk mencari tempat',
                        labelStyle: TextStyle(
                            color: greenPrimary,
                            fontSize: 20,
                            fontFamily: 'Muli',
                            fontWeight: FontWeight.w700),
                      ),
                    ),
                  ),
                ),
                SizedBox(height: doubleSpace),
                const Text(
                  'Paling sering dicari',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 10),
                StreamBuilder<NetworkModel<LokasiListResponse>>(
                    stream: bloc.lokasiListStream,
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        switch (snapshot.data.status) {
                          case Status.loading:
                            return Container(
                              height: 130,
                              child: Center(
                                child: const CircularProgressIndicator(
                                  valueColor: AlwaysStoppedAnimation<Color>(
                                      greenPrimary),
                                ),
                              ),
                            );
                            break;
                          case Status.completed:
                            final places =
                                _sortLocation(snapshot.data.data.listLokasi);
                            return _buildLokasiWidget(places);
                            break;
                          case Status.error:
                            return Container(
                              height: 130,
                              child: const Center(
                                child: Text('Gagal untuk mendapatkan tempat'),
                              ),
                            );
                            break;
                        }
                        return Container();
                      }
                      return Container();
                    }),
              ],
            ),
147
          ),
148
        ],
149
      ),
150
151
152
153
154
155
156
157
158
159
      // floatingActionButton: FloatingActionButton.extended(
      //   key: const Key('FloatingActionButton'),
      //   onPressed: () {
      //     _navigateToInformasiLayananDisabilitasPage(context);
      //   },
      //   label: Text('Informasi Layanan Disabilitas'),
      //   icon: Icon(Icons.search),
      //   backgroundColor: greenPrimary,
      // ),
      // floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
160
      appBar: PreferredSize(
Fakhira Devina's avatar
Fakhira Devina committed
161
        preferredSize: const Size.fromHeight(55),
162
        child: BisaGoAppBar(),
163
        key: const Key('Scaffold Text Field'),
Agnes Handoko's avatar
Agnes Handoko committed
164
      ),
165
166
167
    );
  }

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  Widget _buildLokasiWidget(List<Lokasi> lokasilistresponse) {
    return Container(
      height: 130,
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: [
          Row(
              children: lokasilistresponse
                  .map<Widget>((k) => LocationIconButton(
                        location: k,
                      ))
                  .toList()),
        ],
      ),
    );
  }

  List<Lokasi> _sortLocation(List<Lokasi> lokasi) {
    lokasi.sort((a, b) => a.counter.compareTo(b.counter));
    if (lokasi.length >= 3) {
      return lokasi.reversed.toList().sublist(0, 3);
    }
    return lokasi.reversed.toList();
  }

193
  Widget _buildGoogleMap(BuildContext context) {
194
    if (currentLocation == null) {
195
      return const Center(
196
        child: CircularProgressIndicator(),
197
      );
198
199
200
201
    } else {
      return GoogleMap(
        initialCameraPosition:
            CameraPosition(target: currentLocation, zoom: cameraZoom),
202
        onMapCreated: (GoogleMapController controller) {
203
          mapController = controller;
204
        },
205
        myLocationEnabled: true,
206
        markers: markers,
207
208
209
        onCameraMove: _onCameraMove,
      );
    }
210
211
  }

212
  void _onCameraMove(CameraPosition position) {
213
    setState(() {
214
      currentLocation = position.target;
215
216
217
    });
  }

218
219
  void setInitialLocation() async {
    var position = await geolocator.getCurrentPosition(
220
221
222
223
224
        desiredAccuracy: LocationAccuracy.high);

    setState(() {
      currentLocation = LatLng(position.latitude, position.longitude);
    });
225
226
  }
}