Fakultas Ilmu Komputer UI

dashboard.dart 11.6 KB
Newer Older
1
2
import 'package:bisaGo/bloc/lokasi_response_bloc.dart';
import 'package:bisaGo/model/lokasi.dart';
3
4
5
import 'package:bisaGo/bloc/kegiatan_terdekat_bloc.dart';
import 'package:bisaGo/model/kegiatan.dart';
import 'package:bisaGo/utils/custom_kegiatan_terdekat_button.dart';
6
7
import 'package:bisaGo/network/data/network_model.dart';
import 'package:bisaGo/utils/custom_dashboard_location_button.dart';
8
import 'package:bisaGo/utils/location_turn_on_dialog.dart';
9
import 'package:flutter/material.dart';
10
import 'package:geolocator/geolocator.dart';
11
import 'package:google_maps_flutter/google_maps_flutter.dart';
12
13
14
15
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';
16
17

class Dashboard extends StatefulWidget {
18
  const Dashboard({Key key}) : super(key: key);
19
  @override
20
21
22
23
  DashboardState createState() => DashboardState();
}

class DashboardState extends State<Dashboard> {
Fakhira Devina's avatar
Fakhira Devina committed
24
  final double cameraZoom = 15;
25
26
27
  GoogleMapController mapController;
  Geolocator geolocator;
  LatLng currentLocation;
28
  Set<Marker> markers = {};
29

30
  LokasiResponseBloc bloc = LokasiResponseBloc();
31
  KegiatanTerdekatBloc blocKegiatanTerdekat = KegiatanTerdekatBloc();
32

33
34
35
  @override
  void initState() {
    super.initState();
36
    _showTurnOnLocationDialog(context);
37
    geolocator = Geolocator()..forceAndroidLocationManager;
38
    setInitialLocation();
39
40
  }

41
  void _navigateToPencarianPage(BuildContext context) {
42
    final route = MaterialPageRoute(builder: (_) => const Pencarian());
43
44
45
    Navigator.of(context).push(route);
  }

46
47
48
49
  // void _navigateToInformasiLayananDisabilitasPage(BuildContext context) {
  //   final route = MaterialPageRoute(builder: (_) => ListSekolah());
  //   Navigator.of(context).push(route);
  // }
50

51
  static const textFieldKey = Key('Text Field Mau Kemana');
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

  void _showTurnOnLocationDialog(BuildContext context) {
    bloc.isLocationServiceTurnedOn().then(
      (value) {
        if (!value) {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return LocationTurnOnDialog(
                message: 'Mohon Menyalakan Lokasi',
                title: 'Lokasi',
                onPressed: () {
                  bloc.requestLocationService().then((loc) {
                    if (loc) {
                      Navigator.pop(context);
                    }
                  });
                },
              );
            },
          );
        }
      },
    );
  }

78
79
80
  @override
  Widget build(BuildContext context) {
    return Scaffold(
81
      drawer: BisaGoDrawer(),
82
83
      body: Stack(
        key: const Key('Stack'),
84
        //alignment: AlignmentDirectional.bottomStart,
85
86
        children: <Widget>[
          Container(
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
              padding: EdgeInsets.only(top: 35),
              child:_buildGoogleMap(context)),
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(
                height: 40,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(10.0),
                        bottomRight: Radius.circular(10.0)
                    ),
                ),
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                    horizontal: doubleSpace),
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
                  child: StreamBuilder<NetworkModel>(
                    stream: blocKegiatanTerdekat.kegiatanTerdekatStream,
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        switch (snapshot.data.status) {
                          case Status.loading:
                            return Container(
                              child: const Center(
                                child: LinearProgressIndicator(
                                  valueColor: AlwaysStoppedAnimation<Color>(
                                    greenPrimary),
                                )
                              )
                            );
                            break;
                          case Status.completed:
                            final kegiatanTerdekat =
                              snapshot.data.data;
                            return _buildKegiatanTerdekatWidget(
                              kegiatanTerdekat);
                            break;
                          case Status.error:
                            return Container(
                              child: const Center(child:
                              Text('Tidak ada kegiatan dalam waktu dekat',
                                  style: TextStyle(
                                    color: greenPrimary,
                                    fontSize: 16,
                                    fontFamily: 'Muli',
                                  )),));
                            break;
                        }
                      return Container();
                      }
                    return Container();
                    }
141
142
143
144
145
146
147
148
149
150
                  )
                )
              ), // Container info kegiatan
              Container(
                height: 285,
                alignment: Alignment.bottomCenter,
                decoration: const BoxDecoration(
                  color: white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(10.0),
151
152
                  ),
                ),
153
154
155
156
157
158
159
160
161
162
163
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
                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: const 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),
                          ),
                        ),
                      ),
                    ),
                    const 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: const Center(
                                    child: CircularProgressIndicator(
                                      valueColor: AlwaysStoppedAnimation<Color>(
                                          greenPrimary),
                                    ),
                                  ),
                                );
                                break;
                              case Status.completed:
                                final places = 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();
                        }),
                  ],
235
                ),
236
          ),
237
238
            ]
          )
239
        ],
240
      ),
241
242
243
244
245
246
247
248
249
250
      // floatingActionButton: FloatingActionButton.extended(
      //   key: const Key('FloatingActionButton'),
      //   onPressed: () {
      //     _navigateToInformasiLayananDisabilitasPage(context);
      //   },
      //   label: Text('Informasi Layanan Disabilitas'),
      //   icon: Icon(Icons.search),
      //   backgroundColor: greenPrimary,
      // ),
      // floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
251
252
      appBar: const PreferredSize(
        preferredSize: Size.fromHeight(55),
253
        child: BisaGoAppBar(),
254
        key: Key('Scaffold Text Field'),
Agnes Handoko's avatar
Agnes Handoko committed
255
      ),
256
257
258
    );
  }

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  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()),
        ],
      ),
    );
  }

276
277
278
279
280
281
  Widget _buildKegiatanTerdekatWidget(KegiatanModel kegiatanModel) {
    return Container(
      child: KegiatanTerdekatButton(kegiatan:kegiatanModel)
    );
  }

282
  Widget _buildGoogleMap(BuildContext context) {
283
    if (currentLocation == null) {
284
      return const Center(
285
        child: CircularProgressIndicator(),
286
      );
287
288
289
290
    } else {
      return GoogleMap(
        initialCameraPosition:
            CameraPosition(target: currentLocation, zoom: cameraZoom),
291
        onMapCreated: (GoogleMapController controller) {
292
          mapController = controller;
293
        },
294
        myLocationEnabled: true,
295
        markers: markers,
296
297
298
        onCameraMove: _onCameraMove,
      );
    }
299
300
  }

301
  void _onCameraMove(CameraPosition position) {
302
    setState(() {
303
      currentLocation = position.target;
304
305
306
    });
  }

307
  void setInitialLocation() async {
308
    final position = await geolocator.getCurrentPosition(
309
310
311
312
313
        desiredAccuracy: LocationAccuracy.high);

    setState(() {
      currentLocation = LatLng(position.latitude, position.longitude);
    });
314
315
  }
}