Fakultas Ilmu Komputer UI

pencarian.dart 5.85 KB
Newer Older
1
2
3
4
import 'package:flutter/material.dart';
import 'package:ppl_disabilitas/config/styles.dart';
import 'package:ppl_disabilitas/network/data/network_model.dart';
import 'package:ppl_disabilitas/network/network_interface.dart';
5

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

class PencarianState extends State<Pencarian> {
13
  final myController = TextEditingController();
14
  Icon searchIcon = Icon(Icons.search);
15
  Widget appBarText = Text('Pencarian Lokasi');
16
  NetworkInterface networkInterface = NetworkInterface();
17
18
  List places = [];
  List currentSearch = [];
19
20
21
22
23
  Future<NetworkModel> data;
  @override
  void initState() {
    data = networkInterface.get(
        url: 'https://my.api.mockaroo.com/mall.json?key=dbcde960');
24
25
26
27
28
29
30
31
32
33
34
35
36
    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;
      });
    });
37
38
    super.initState();
  }
39
40
41
42

  @override
  Widget build(BuildContext context) {
    return Scaffold(
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        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,
63
                    color: greenPrimary,
64
65
66
67
68
69
70
71
72
73
74
                    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(
75
76
77
78
                    icon: Icon(
                      Icons.mic,
                      color: greenPrimary,
                      size: 25,
79
80
81
                    ), onPressed: () {},
                  )),
            ),
82
          ),
83
        ),
84
85
86
87
88
89
90
91
92
93
        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}');
              }
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
              // 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',
134
                                  ),
135
136
137
138
139
140
141
                                ),
                                Text(
                                  currentSearch[index]['alamat'],
                                  style: TextStyle(
                                    fontSize: 15,
                                    color: Colors.black,
                                    fontFamily: 'Muli',
142
                                  ),
143
144
                                ),
                              ],
145
                            ),
146
147
148
149
150
151
152
153
154
                          ),
                        ],
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.grey[400],
                        size: 20,
                      )
                    ],
155
                  ),
156
157
158
159
160
                );
              })
        ]));
  }
}