Fakultas Ilmu Komputer UI

pencarian.dart 6.58 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
import 'package:ppl_disabilitas/model/lokasi.dart';
6
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

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

  /// To get API of places
26
  NetworkInterface networkInterface = NetworkInterface();
27
28

  /// List for places from API
29
  List places = [];
30
31

  /// List of places currently searched on the textFormField
32
  List currentSearch = [];
33
34

  /// Data from API
35
  Future<NetworkModel> data;
36
37
38
39

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


46
47
48
49
  @override
  void initState() {
    data = networkInterface.get(
        url: 'https://my.api.mockaroo.com/mall.json?key=dbcde960');
50
51
52
53
54
55
56
57
58
59
60
61
62
    myController.addListener(() {
      List tempList = new List();
      for (int i = 0; i < places.length; i++) {
        if (places[i]['nama']
            .toLowerCase()
            .contains(myController.text.toLowerCase())) {
          tempList.add(places[i]);
        }
      }
      setState(() {
        currentSearch = tempList;
      });
    });
63
64
    super.initState();
  }
65
66
67
68

  @override
  Widget build(BuildContext context) {
    return Scaffold(
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        appBar: AppBar(
          backgroundColor: greenPrimary,
          leading: IconButton(
              icon: Icon(Icons.arrow_back_ios, size: 25),
              key: Key('Back Icon Key'),
              onPressed: () => Navigator.pop(context, 'Take me back')),
          title: Container(
            margin: EdgeInsets.only(top: doubleSpace, bottom: doubleSpace),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: doubleBorderRadius,
                boxShadow: regularShadow),
            child: TextFormField(
              controller: myController,
              key: Key('Text Field Mau Kemana'),
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(0),
                  isDense: false,
                  prefixIcon: Icon(
                    Icons.search,
89
                    color: greenPrimary,
90
91
92
93
94
95
96
97
98
99
100
                    size: 25,
                  ),
                  border: InputBorder.none,
                  fillColor: Colors.white,
                  labelText: 'Kamu mau kemana?',
                  labelStyle: TextStyle(
                      color: greenPrimary,
                      fontSize: 18,
                      fontFamily: 'Muli',
                      fontWeight: FontWeight.w700),
                  suffixIcon: IconButton(
101
102
103
104
                    icon: Icon(
                      Icons.mic,
                      color: greenPrimary,
                      size: 25,
105
106
107
                    ), onPressed: () {},
                  )),
            ),
108
          ),
109
        ),
110
111
112
113
114
115
116
117
118
119
        body: Stack(children: [
          FutureBuilder<NetworkModel>(
            future: data,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                places = snapshot.data.response;
                return Container();
              } else if (snapshot.hasError) {
                return Text('${snapshot.error}');
              }
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
157
158
159
              // By default, show a loading spinner.
              return CircularProgressIndicator();
            },
          ),
          ListView.builder(
              itemCount: currentSearch.length,
              itemBuilder: (context, index) {
                return 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(
                                  currentSearch[index]['nama'],
                                  key: Key('Result text'),
                                  style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.w800,
                                    color: Colors.black,
                                    fontFamily: 'Muli',
160
                                  ),
161
162
163
164
165
166
167
                                ),
                                Text(
                                  currentSearch[index]['alamat'],
                                  style: TextStyle(
                                    fontSize: 15,
                                    color: Colors.black,
                                    fontFamily: 'Muli',
168
                                  ),
169
170
                                ),
                              ],
171
                            ),
172
173
174
175
176
177
178
179
180
                          ),
                        ],
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.grey[400],
                        size: 20,
                      )
                    ],
181
                  ),
182
183
184
185
186
                );
              })
        ]));
  }
}