Fakultas Ilmu Komputer UI

pencarian.dart 7.95 KB
Newer Older
1
import 'package:flutter/material.dart';
2
import 'package:ppl_disabilitas/bloc/LokasiResponseBloc.dart';
3
import 'package:ppl_disabilitas/config/styles.dart';
4
import 'package:ppl_disabilitas/model/lokasi.dart';
5
import 'package:ppl_disabilitas/network/data/network_model.dart';
6
7
8
9
10
11
12

class Pencarian extends StatefulWidget {
  @override
  PencarianState createState() => PencarianState();
}

class PencarianState extends State<Pencarian> {
13
14
  Icon searchIcon = Icon(Icons.search);
  Widget appBarText = Text("Pencarian Lokasi");
15
16
  LokasiResponseBloc _bloc = LokasiResponseBloc();
  LokasiListResponse lokasiFromApi;
17
  LokasiListResponse recentSearch;
18
19
20
21
  @override
  void initState() {
    super.initState();
  }
22
23
24
25
26

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
27
        backgroundColor: greenPrimary,
28
        leading: IconButton(
29
30
31
            icon: Icon(Icons.arrow_back_ios, size: 25),
            key: Key("Back Icon Key"),
            onPressed: () => Navigator.pop(context, 'Take me back')),
32
33
34
35
36
37
38
        title: Container(
          margin: EdgeInsets.only(top: doubleSpace, bottom: doubleSpace),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: doubleBorderRadius,
              boxShadow: regularShadow),
          child: TextFormField(
39
40
            key: Key("Text Field Mau Kemana"),
            decoration: InputDecoration(
41
42
43
44
45
46
47
48
                contentPadding: EdgeInsets.all(0),
                isDense: false,
                prefixIcon: Icon(
                  Icons.search,
                  color: greenPrimary,
                  size: 25,
                ),
                border: InputBorder.none,
49
50
                fillColor: Colors.white,
                labelText: 'Kamu mau kemana?',
51
52
53
54
55
                labelStyle: TextStyle(
                    color: greenPrimary,
                    fontSize: 18,
                    fontFamily: 'Muli',
                    fontWeight: FontWeight.w700),
56
                suffixIcon: IconButton(
57
58
59
60
61
62
                    icon: Icon(
                      Icons.mic,
                      color: greenPrimary,
                      size: 25,
                    ),
                    onPressed: () {})),
63
          ),
64
65
        ),
      ),
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
      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:
                    recentSearch = snapshot.data.data;
                    Widget displayWidget;
84
                    if (recentSearch.listLokasi.isEmpty) {
85
86
87
                      displayWidget = Center(
                          child: Text("Anda belum pernah melakukan pencarian"));
                    } else {
88
89
                      displayWidget =
                          makeLokasiWidget("history", recentSearch.listLokasi.take(3).toList());
90
91
92
                    }
                    return Expanded(
                        child: Column(
93
94
95
96
97
98
99
100
101
102
103
104
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                            margin: EdgeInsets.all(doubleSpace),
                            child: Text(
                              "Pencarian terdahulu",
                              style:
                                  TextStyle(fontFamily: 'Muli', fontSize: 15),
                            )),
                        Flexible(child: displayWidget),
                      ],
                    ));
105
106
107
108
109
110
111
112
113
114
115
116
                    break;
                  case Status.ERROR:
                    return Center(
                      child: Text("${snapshot.data.status}"),
                    );
                    break;
                }
              }
              return Container();
            },
          ),
          Container(
117
118
            margin: EdgeInsets.only(
                left: doubleSpace, top: regularSpace, bottom: smallSpace),
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
            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:
                    lokasiFromApi = snapshot.data.data;
                    return Expanded(
136
                        flex: 2, child: makeLokasiWidget("api", lokasiFromApi.listLokasi));
137
138
139
140
141
142
143
144
145
146
147
148
                    break;
                  case Status.ERROR:
                    return Center(
                      child: Text(snapshot.data.data.toString()),
                    );
                    break;
                }
              }
              return Container();
            },
          ),
        ],
149
150
151
152
      ),
    );
  }

153
154
  Widget makeLokasiWidget(String key, List<Lokasi> places) {
    print('$key - $places');
155
    return ListView.builder(
156
        shrinkWrap: true,
157
        itemCount: places.length,
158
        itemBuilder: (context, index) {
159
          return InkWell(
160
            key: Key("$key-${places[index].nama}"),
161
            onTap: () {
162
              _bloc.saveRecentSearch(places[index]);
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
            },
            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(
                    children: <Widget>[
                      CircleAvatar(
                        backgroundColor: greenPrimary,
                        child: Text('Test'),
                      ),
                      Container(
                        padding: EdgeInsets.all(doubleSpace),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text(
187
                              places[index].nama,
188
189
190
191
192
193
                              style: TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.w800,
                                color: Colors.black,
                                fontFamily: 'Muli',
                              ),
194
                            ),
195
                            Text(
196
                              places[index].alamat,
197
198
199
200
201
                              style: TextStyle(
                                fontSize: 15,
                                color: Colors.black,
                                fontFamily: 'Muli',
                              ),
202
                            ),
203
204
                          ],
                        ),
205
                      ),
206
207
208
209
210
211
212
213
214
                    ],
                  ),
                  Icon(
                    Icons.arrow_forward_ios,
                    color: Colors.grey[400],
                    size: 20,
                  )
                ],
              ),
215
            ),
216
217
218
          );
        });
  }
219
}