Fakultas Ilmu Komputer UI

dashboard.dart 7.85 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
  const Dashboard({Key key}) : super(key: key);
15
  @override
16
17
18
19
  DashboardState createState() => DashboardState();
}

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

26
27
  LokasiResponseBloc bloc = LokasiResponseBloc();

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

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

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

45
  static const textFieldKey = Key('Text Field Mau Kemana');
46
47
48
  @override
  Widget build(BuildContext context) {
    return Scaffold(
49
      drawer: BisaGoDrawer(),
50
51
52
53
54
55
56
57
58
59
60
61
      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),
62
              ),
63
            ),
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            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'),
84
                      decoration: const InputDecoration(
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
                        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),
                      ),
                    ),
                  ),
                ),
102
                const SizedBox(height: doubleSpace),
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
                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,
120
121
                              child: const Center(
                                child: CircularProgressIndicator(
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
147
                                  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();
                    }),
              ],
            ),
148
          ),
149
        ],
150
      ),
151
152
153
154
155
156
157
158
159
160
      // floatingActionButton: FloatingActionButton.extended(
      //   key: const Key('FloatingActionButton'),
      //   onPressed: () {
      //     _navigateToInformasiLayananDisabilitasPage(context);
      //   },
      //   label: Text('Informasi Layanan Disabilitas'),
      //   icon: Icon(Icons.search),
      //   backgroundColor: greenPrimary,
      // ),
      // floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
161
162
      appBar: const PreferredSize(
        preferredSize: Size.fromHeight(55),
163
        child: BisaGoAppBar(),
164
        key: Key('Scaffold Text Field'),
Agnes Handoko's avatar
Agnes Handoko committed
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
  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();
  }

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

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

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

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