Fakultas Ilmu Komputer UI

pencarian.dart 10.2 KB
Newer Older
1
import 'package:flutter/foundation.dart';
2
import 'package:flutter/material.dart';
3
import 'package:ppl_disabilitas/bloc/LokasiResponseBloc.dart';
4
import 'package:ppl_disabilitas/component/ImageHolder.dart';
5
import 'package:ppl_disabilitas/config/styles.dart';
6
import 'package:ppl_disabilitas/model/lokasi.dart';
7
import 'package:ppl_disabilitas/network/data/network_model.dart';
8
import 'package:ppl_disabilitas/page/filter_fasilitas/fasilitas.dart';
9

10
/// Create Pencarian page widget with a state
11
12
13
14
15
class Pencarian extends StatefulWidget {
  @override
  PencarianState createState() => PencarianState();
}

16
/// State of Pencacrian page
17
class PencarianState extends State<Pencarian> {
18
19
20
21
  /// Controller for textFormField
  TextEditingController myController = TextEditingController();

  /// Search Icon for textFormField
22
  Icon searchIcon = Icon(Icons.search);
23
24

  /// Text for appbar
25
  Widget appBarText = Text('Pencarian Lokasi');
26

27
28
  /// List of places currently searched on the textFormField
  List<Lokasi> currentSearch = [];
29
30

  /// List for places from API
31
  List<Lokasi> places = [];
32

33
  /// BLoC for pencarian
34
  LokasiResponseBloc _bloc = LokasiResponseBloc();
35
36
37
38

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
39
40
41
    properties
      ..add(DiagnosticsProperty<List>('places', places))
      ..add(DiagnosticsProperty<List>('currentSearch', currentSearch));
42
43
  }

44
45
  @override
  void initState() {
46
    myController.addListener(() {
47
      List<Lokasi> tempList = [];
48
      for (int i = 0; i < places.length; i++) {
49
50
        if (myController.text.isNotEmpty) {
          if (places[i]
51
              .name
52
53
54
55
              .toLowerCase()
              .contains(myController.text.toLowerCase())) {
            tempList.add(places[i]);
          }
56
57
58
        }
      }
      setState(() {
59
60
        // print(places);
        // print(places[0].nama);
61
62
63
        currentSearch = tempList;
      });
    });
64
65
    super.initState();
  }
66
67
68
69
70

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
71
        backgroundColor: greenPrimary,
72
        leading: IconButton(
73
            icon: Icon(Icons.arrow_back_ios, size: 25),
74
            key: Key('Back Icon Key'),
75
            onPressed: () => Navigator.pop(context, 'Take me back')),
76
77
78
79
80
81
82
        title: Container(
          margin: EdgeInsets.only(top: doubleSpace, bottom: doubleSpace),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: doubleBorderRadius,
              boxShadow: regularShadow),
          child: TextFormField(
83
84
            controller: myController,
            key: Key('Text Field Mau Kemana'),
85
            decoration: InputDecoration(
86
87
88
89
90
91
92
93
                contentPadding: EdgeInsets.all(0),
                isDense: false,
                prefixIcon: Icon(
                  Icons.search,
                  color: greenPrimary,
                  size: 25,
                ),
                border: InputBorder.none,
94
95
                fillColor: Colors.white,
                labelText: 'Kamu mau kemana?',
96
97
98
99
100
                labelStyle: TextStyle(
                    color: greenPrimary,
                    fontSize: 18,
                    fontFamily: 'Muli',
                    fontWeight: FontWeight.w700),
101
                suffixIcon: IconButton(
102
103
                  icon: Icon(
                    Icons.mic,
104
                    color: greenPrimary,
105
106
                    size: 25,
                  ),
107
108
                  onPressed: () {},
                )),
109
          ),
110
111
        ),
      ),
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          StreamBuilder<NetworkModel<LokasiListResponse>>(
            stream: _bloc.recentSearchStream,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                switch (snapshot.data.status) {
                  case Status.LOADING:
                    return Center(
                      child: CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation<Color>(greenPrimary),
                      ),
                    );
                    break;
                  case Status.COMPLETED:
128
                    final recentSearch = snapshot.data.data;
129
130
131
132
133
                    Widget displayWidget;
                    if (recentSearch.listLokasi.isEmpty) {
                      displayWidget = Center(
                          child: Text("Anda belum pernah melakukan pencarian"));
                    } else {
134
135
                      displayWidget = makeLokasiWidget(
                          "history", recentSearch.listLokasi.take(3).toList());
136
137
138
                    }
                    return Expanded(
                        child: Column(
139
140
141
142
143
144
145
146
147
148
149
150
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                            margin: EdgeInsets.all(doubleSpace),
                            child: Text(
                              "Pencarian terdahulu",
                              style:
                                  TextStyle(fontFamily: 'Muli', fontSize: 15),
                            )),
                        Flexible(child: displayWidget),
                      ],
                    ));
151
152
153
154
155
156
157
158
159
160
161
162
                    break;
                  case Status.ERROR:
                    return Center(
                      child: Text("${snapshot.data.status}"),
                    );
                    break;
                }
              }
              return Container();
            },
          ),
          Container(
163
164
            margin: EdgeInsets.only(
                left: doubleSpace, top: regularSpace, bottom: smallSpace),
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
            child: Text("Hasil Pencarian"),
          ),
          StreamBuilder<NetworkModel<LokasiListResponse>>(
            stream: _bloc.lokasiListStream,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                switch (snapshot.data.status) {
                  case Status.LOADING:
                    return Center(
                      child: CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation<Color>(greenPrimary),
                      ),
                    );
                    break;
                  case Status.COMPLETED:
180
                    places = snapshot.data.data.listLokasi;
181
                    return Expanded(
182
183
184
185
                        flex: 2,
                        child: currentSearch.isEmpty
                            ? Center(child: Text('Cari lokasi'))
                            : makeLokasiWidget("api", currentSearch));
186
187
188
189
190
191
192
193
194
195
196
197
                    break;
                  case Status.ERROR:
                    return Center(
                      child: Text(snapshot.data.data.toString()),
                    );
                    break;
                }
              }
              return Container();
            },
          ),
        ],
198
199
200
201
      ),
    );
  }

202
  Widget makeLokasiWidget(String key, List<Lokasi> places) {
203
204
    return ListView.builder(
        shrinkWrap: true,
205
        itemCount: places.length,
206
207
        itemBuilder: (context, index) {
          return InkWell(
208
            key: Key("$key-${places[index].name}"),
209
            onTap: () {
210
              _bloc.saveRecentSearch(places[index]);
Fakhira Devina's avatar
Fakhira Devina committed
211
                Navigator.of(context).push(MaterialPageRoute(
212
213
                  builder: (BuildContext context) => Fasilitas(
                        alamat: places[index].alamat,
214
215
                        nama: places[index].name,
                        telpon: places[index].no_telp,
216
                        url: places[index].image,
217
                        id: places[index].id,
Fakhira Devina's avatar
Fakhira Devina committed
218
                      ))).then((value) => _bloc.fetchRecentSearch());
219
220
221
222
223
224
225
226
227
228
            },
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.transparent,
                  border: Border(bottom: BorderSide(color: Colors.grey[400]))),
              margin: EdgeInsets.only(left: doubleSpace, right: doubleSpace),
              height: 90,
              child: Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
229
                children: <Widget>[
Fakhira Devina's avatar
Fakhira Devina committed
230
231
232
233
234
                  Expanded(
                    child: Row(
                      children: <Widget>[
                        CircleAvatar(
                          backgroundColor: greenPrimary,
235
                          child: ImageHolder(url: places[index].image,),
Fakhira Devina's avatar
Fakhira Devina committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
                        ),
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.all(doubleSpace),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Text(
                                  places[index].name,
                                  style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.w800,
                                    color: Colors.black,
                                    fontFamily: 'Muli',
                                  ),
Fakhira Devina's avatar
Fakhira Devina committed
252
                                  overflow: TextOverflow.ellipsis,
Fakhira Devina's avatar
Fakhira Devina committed
253
254
255
256
257
258
259
260
261
262
263
                                ),
                                Text(
                                  places[index].alamat,
                                  style: TextStyle(
                                    fontSize: 15,
                                    color: Colors.black,
                                    fontFamily: 'Muli',
                                  ),
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ],
264
                            ),
Fakhira Devina's avatar
Fakhira Devina committed
265
                          ),
266
                        ),
Fakhira Devina's avatar
Fakhira Devina committed
267
268
                      ],
                    ),
269
                  ),
270
271
272
273
274
                  Icon(
                    Icons.arrow_forward_ios,
                    color: Colors.grey[400],
                    size: 20,
                  )
275
276
                ],
              ),
277
            ),
278
279
280
          );
        });
  }
281
}