Fakultas Ilmu Komputer UI

pencarian.dart 9.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/config/styles.dart';
5
6
import 'package:ppl_disabilitas/model/lokasi.dart';
import 'package:ppl_disabilitas/network/data/network_model.dart';
7

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

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

  /// Search Icon for textFormField
20
  Icon searchIcon = Icon(Icons.search);
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

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

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

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

  /// BLoC for pencarian
  LokasiResponseBloc _bloc = LokasiResponseBloc();

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

  @override
  void initState() {
    myController.addListener(() {
      List<Lokasi> tempList = [];
      for (int i = 0; i < places.length; i++) {
        if (myController.text.isNotEmpty) {
          if (places[i]
              .nama
              .toLowerCase()
              .contains(myController.text.toLowerCase())) {
            tempList.add(places[i]);
          }
        }
      }
      setState(() {
        currentSearch = tempList;
      });
    });
    super.initState();
  }
62
63
64
65
66

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
67
        backgroundColor: greenPrimary,
68
        leading: IconButton(
69
            icon: Icon(Icons.arrow_back_ios, size: 25),
70
            key: Key('Back Icon Key'),
71
            onPressed: () => Navigator.pop(context, 'Take me back')),
72
73
74
75
76
77
78
        title: Container(
          margin: EdgeInsets.only(top: doubleSpace, bottom: doubleSpace),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: doubleBorderRadius,
              boxShadow: regularShadow),
          child: TextFormField(
79
80
            controller: myController,
            key: Key('Text Field Mau Kemana'),
81
            decoration: InputDecoration(
82
83
84
85
86
87
88
89
                contentPadding: EdgeInsets.all(0),
                isDense: false,
                prefixIcon: Icon(
                  Icons.search,
                  color: greenPrimary,
                  size: 25,
                ),
                border: InputBorder.none,
90
91
                fillColor: Colors.white,
                labelText: 'Kamu mau kemana?',
92
93
94
95
96
                labelStyle: TextStyle(
                    color: greenPrimary,
                    fontSize: 18,
                    fontFamily: 'Muli',
                    fontWeight: FontWeight.w700),
97
                suffixIcon: IconButton(
98
99
100
101
102
103
104
                  icon: Icon(
                    Icons.mic,
                    color: greenPrimary,
                    size: 25,
                  ),
                  onPressed: () {},
                )),
105
          ),
106
107
        ),
      ),
108
109
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
110
        children: <Widget>[
111
112
113
114
115
116
117
118
119
          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),
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
147
148
149
150
151
152
153
154
155
156
                    );
                    break;
                  case Status.COMPLETED:
                    final recentSearch = snapshot.data.data;
                    Widget displayWidget;
                    if (recentSearch.listLokasi.isEmpty) {
                      displayWidget = Center(
                          child: Text("Anda belum pernah melakukan pencarian"));
                    } else {
                      displayWidget = makeLokasiWidget(
                          "history", recentSearch.listLokasi.take(3).toList());
                    }
                    return Expanded(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                            margin: EdgeInsets.all(doubleSpace),
                            child: Text(
                              "Pencarian terdahulu",
                              style:
                                  TextStyle(fontFamily: 'Muli', fontSize: 15),
                            )),
                        Flexible(child: displayWidget),
                      ],
                    ));
                    break;
                  case Status.ERROR:
                    return Center(
                      child: Text("${snapshot.data.status}"),
                    );
                    break;
                }
              }
              return Container();
            },
157
158
          ),
          Container(
159
160
161
            margin: EdgeInsets.only(
                left: doubleSpace, top: regularSpace, bottom: smallSpace),
            child: Text("Hasil Pencarian"),
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
          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:
                    places = snapshot.data.data.listLokasi;
                    return Expanded(
                        flex: 2,
                        child: currentSearch.isEmpty
                            ? Center(child: Text('Cari lokasi'))
                            : makeLokasiWidget("api", currentSearch));
                    break;
                  case Status.ERROR:
                    return Center(
                      child: Text(snapshot.data.data.toString()),
                    );
                    break;
                }
              }
              return Container();
            },
          ),
        ],
      ),
    );
  }

  Widget makeLokasiWidget(String key, List<Lokasi> places) {
    print('$key - $places');
    return ListView.builder(
        shrinkWrap: true,
        itemCount: places.length,
        itemBuilder: (context, index) {
          return InkWell(
            key: Key("$key-${places[index].nama}"),
            onTap: () {
              _bloc.saveRecentSearch(places[index]);
            },
            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,
                children: <Widget>[
                  Row(
220
                    children: <Widget>[
221
222
223
                      CircleAvatar(
                        backgroundColor: greenPrimary,
                        child: Text('Test'),
224
                      ),
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
                      Container(
                        padding: EdgeInsets.all(doubleSpace),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text(
                              places[index].nama,
                              style: TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.w800,
                                color: Colors.black,
                                fontFamily: 'Muli',
                              ),
                            ),
                            Text(
                              places[index].alamat,
                              style: TextStyle(
                                fontSize: 15,
                                color: Colors.black,
                                fontFamily: 'Muli',
                              ),
                            ),
                          ],
249
250
251
252
                        ),
                      ),
                    ],
                  ),
253
254
255
256
257
258
259
                  Icon(
                    Icons.arrow_forward_ios,
                    color: Colors.grey[400],
                    size: 20,
                  )
                ],
              ),
260
            ),
261
262
          );
        });
263
  }
264
}