Fakultas Ilmu Komputer UI

dashboard.dart 8.33 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:bisaGo/utils/location_turn_on_dialog.dart';
6
import 'package:flutter/material.dart';
7
import 'package:geolocator/geolocator.dart';
8
import 'package:google_maps_flutter/google_maps_flutter.dart';
9
10
11
12
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';
13
14

class Dashboard extends StatefulWidget {
15
  const Dashboard({Key key}) : super(key: key);
16
  @override
17
18
19
20
  DashboardState createState() => DashboardState();
}

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

27
28
  LokasiResponseBloc bloc = LokasiResponseBloc();

29
30
31
  @override
  void initState() {
    super.initState();
32
    _showTurnOnLocationDialog(context);
33
    geolocator = Geolocator()..forceAndroidLocationManager;
34
    setInitialLocation();
35
36
  }

37
  void _navigateToPencarianPage(BuildContext context) {
38
    final route = MaterialPageRoute(builder: (_) => const Pencarian());
39
40
41
    Navigator.of(context).push(route);
  }

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

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

  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);
                    }
                  });
                },
              );
            },
          );
        }
      },
    );
  }

74
75
76
  @override
  Widget build(BuildContext context) {
    return Scaffold(
77
      drawer: BisaGoDrawer(),
78
79
80
81
82
83
84
85
86
87
88
89
      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),
90
              ),
91
            ),
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
            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'),
112
                      decoration: const InputDecoration(
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                        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),
                      ),
                    ),
                  ),
                ),
130
                const SizedBox(height: doubleSpace),
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                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,
148
149
                              child: const Center(
                                child: CircularProgressIndicator(
150
151
152
153
154
155
156
                                  valueColor: AlwaysStoppedAnimation<Color>(
                                      greenPrimary),
                                ),
                              ),
                            );
                            break;
                          case Status.completed:
157
                            final places = snapshot.data.data.listLokasi;
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
                            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();
                    }),
              ],
            ),
175
          ),
176
        ],
177
      ),
178
179
180
181
182
183
184
185
186
187
      // floatingActionButton: FloatingActionButton.extended(
      //   key: const Key('FloatingActionButton'),
      //   onPressed: () {
      //     _navigateToInformasiLayananDisabilitasPage(context);
      //   },
      //   label: Text('Informasi Layanan Disabilitas'),
      //   icon: Icon(Icons.search),
      //   backgroundColor: greenPrimary,
      // ),
      // floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
188
189
      appBar: const PreferredSize(
        preferredSize: Size.fromHeight(55),
190
        child: BisaGoAppBar(),
191
        key: Key('Scaffold Text Field'),
Agnes Handoko's avatar
Agnes Handoko committed
192
      ),
193
194
195
    );
  }

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  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()),
        ],
      ),
    );
  }

213
  Widget _buildGoogleMap(BuildContext context) {
214
    if (currentLocation == null) {
215
      return const Center(
216
        child: CircularProgressIndicator(),
217
      );
218
219
220
221
    } else {
      return GoogleMap(
        initialCameraPosition:
            CameraPosition(target: currentLocation, zoom: cameraZoom),
222
        onMapCreated: (GoogleMapController controller) {
223
          mapController = controller;
224
        },
225
        myLocationEnabled: true,
226
        markers: markers,
227
228
229
        onCameraMove: _onCameraMove,
      );
    }
230
231
  }

232
  void _onCameraMove(CameraPosition position) {
233
    setState(() {
234
      currentLocation = position.target;
235
236
237
    });
  }

238
  void setInitialLocation() async {
239
    final position = await geolocator.getCurrentPosition(
240
241
242
243
244
        desiredAccuracy: LocationAccuracy.high);

    setState(() {
      currentLocation = LatLng(position.latitude, position.longitude);
    });
245
246
  }
}