· 6 years ago · Oct 26, 2019, 02:54 PM
1import 'package:flutter/material.dart';
2import 'package:dio/dio.dart';
3
4void main() => runApp(new MyApp());
5
6class MyApp extends StatelessWidget {
7 /*
8 @override
9 Widget build(BuildContext context) {
10 return new MaterialApp(
11 title: 'Flutter Api Filter list Demo',
12 theme: new ThemeData(
13 primarySwatch: Colors.blue,
14 ),
15 home: new ExamplePage(),
16 );
17 }*/
18 final appTitle = 'Drawer Demo';
19 @override
20 Widget build(BuildContext context) {
21 return MaterialApp(
22 title: appTitle,
23 home: ExamplePage(title: appTitle),
24 );
25 }
26}
27
28class ExamplePage extends StatefulWidget {
29 final String title;
30
31 ExamplePage({Key key, this.title}) : super(key: key);
32 // ExamplePage({ Key key }) : super(key: key);
33 //@override
34 _ExamplePageState createState() => new _ExamplePageState();
35
36 @override
37 Widget build(BuildContext context) {
38 return Scaffold(
39 appBar: AppBar(title: Text(title)),
40 body: Center(child: Text('My Page!')),
41 drawer: Drawer(
42 // Add a ListView to the drawer. This ensures the user can scroll
43 // through the options in the drawer if there isn't enough vertical
44 // space to fit everything.
45 child: ListView(
46 // Important: Remove any padding from the ListView.
47 padding: EdgeInsets.zero,
48 children: <Widget>[
49 DrawerHeader(
50 child: Text('Drawer Header'),
51 decoration: BoxDecoration(
52 color: Colors.blue,
53 ),
54 ),
55 ListTile(
56 title: Text('Item 1'),
57 onTap: () {
58 // Update the state of the app
59 // ...
60 // Then close the drawer
61 Navigator.pop(context);
62 },
63 ),
64 ListTile(
65 title: Text('Item 2'),
66 onTap: () {
67 // Update the state of the app
68 // ...
69 // Then close the drawer
70 Navigator.pop(context);
71 },
72 ),
73 ],
74 ),
75 ),
76 );
77 }
78}
79
80class _ExamplePageState extends State<ExamplePage> {
81 // final formKey = new GlobalKey<FormState>();
82 // final key = new GlobalKey<ScaffoldState>();
83 final TextEditingController _filter = new TextEditingController();
84 final dio = new Dio();
85 String _searchText = "";
86 List names = new List();
87 List filteredNames = new List();
88 Icon _searchIcon = new Icon(Icons.search);
89 Widget _appBarTitle = new Text( 'Test bar' );
90
91 _ExamplePageState() {
92 _filter.addListener(() {
93 if (_filter.text.isEmpty) {
94 setState(() {
95 _searchText = "";
96 filteredNames = names;
97 });
98 } else {
99 setState(() {
100 _searchText = _filter.text;
101 });
102 }
103 });
104 }
105
106 @override
107 void initState() {
108 this._getNames();
109 super.initState();
110 }
111
112 Widget build(BuildContext context) {
113 return Scaffold(
114 appBar: _buildBar(context),
115 body: Container(
116 child: _buildList();
117 ),
118
119 resizeToAvoidBottomPadding: false,
120 drawer: Drawer(child: ListView(
121 // Important: Remove any padding from the ListView.
122 padding: EdgeInsets.zero,
123 children: <Widget>[
124 DrawerHeader(
125 child: Text('Drawer Header'),
126 decoration: BoxDecoration(
127 color: Colors.blue,
128 ),
129 ),
130 ListTile(
131 title: Text('Item 1'),
132 onTap: () {
133 // Update the state of the app.
134 // ...
135 },
136 ),
137 ListTile(
138 title: Text('Item 2'),
139 onTap: () {
140 // Update the state of the app.
141 // ...
142 },
143 ),
144 ],
145 ),
146 ));
147 }
148
149 Widget _buildBar(BuildContext context) {
150 return new AppBar(
151 centerTitle: true,
152 title: _appBarTitle,
153 leading: new IconButton(
154 icon: _searchIcon,
155 onPressed: _searchPressed,
156
157 ),
158 actions: <Widget>[
159 IconButton(icon: Icon(Icons.menu), onPressed: () {},),
160 ],
161 );
162 }
163
164 Widget _buildList() {
165 if (!(_searchText.isEmpty)) {
166 List tempList = new List();
167 for (int i = 0; i < filteredNames.length; i++) {
168 //if (filteredNames[i]['name'].toLowerCase().contains(_searchText.toLowerCase())) {
169 if (filteredNames[i].toLowerCase().contains(_searchText.toLowerCase())) {
170 tempList.add(filteredNames[i]);
171 }
172 }
173 filteredNames = tempList;
174 }
175 return ListView.builder(
176 itemCount: names == null ? 0 : filteredNames.length,
177 itemBuilder: (BuildContext context, int index) {
178 return new ListTile(
179 //title: Text(filteredNames[index]['name']),
180 title: Text(filteredNames[index]),
181 //onTap: () => print(filteredNames[index]['name']),
182 //onTap: () => print(filteredNames[index]),
183 onTap: () {
184 Navigator.push(
185 context,
186 MaterialPageRoute(builder: (context) => SecondRoute()),
187 );
188 },
189 );
190 },
191 );
192 }
193
194 void _searchPressed() {
195 setState(() {
196 if (this._searchIcon.icon == Icons.search) {
197 this._searchIcon = new Icon(Icons.close);
198 this._appBarTitle = new TextField(
199 controller: _filter,
200 decoration: new InputDecoration(
201 prefixIcon: new Icon(Icons.search),
202 hintText: 'Search...'
203 ),
204 );
205 } else {
206 this._searchIcon = new Icon(Icons.search);
207 this._appBarTitle = new Text( 'Pozniejszyyy tekst' );
208 filteredNames = names;
209 _filter.clear();
210 }
211 });
212 }
213
214 void _getNames() async {
215 //final response = await dio.get('https://swapi.co/api/people');
216 List tempList = new List();
217 /*for (int i = 0; i < response.data['results'].length; i++) {
218 tempList.add(response.data['results'][i]);
219 }*/
220 for (int i =0; i<20; i++){
221 tempList.add("$i");
222 }
223 setState(() {
224 names = tempList;
225 //names.shuffle();
226 filteredNames = names;
227 });
228 }
229
230
231}
232
233class SecondRoute extends StatelessWidget {
234 @override
235 Widget build(BuildContext context) {
236 return Scaffold(
237 appBar: AppBar(
238 title: Text("Second Route"),
239 ),
240 body: Center(
241 child: RaisedButton(
242 onPressed: () {//onPressed idzie ekran dalej, onPop wraca
243 Navigator.push(
244 context,
245 MaterialPageRoute(builder: (context) => ThirdRoute()),
246 );
247 },
248 child: Text('Idziemy dalej'),
249 ),
250 ),
251 );
252 }
253}
254
255class ThirdRoute extends SecondRoute {
256 @override
257 Widget build(BuildContext context) {
258 return Scaffold(
259 appBar: AppBar(
260 title: Text("Third Route"),
261 ),
262 body: Center(
263 child: RaisedButton(
264 onPressed: () {
265 Navigator.pop(context);
266 },
267 child: Text('Go back!'),
268 ),
269 ),
270 );
271 }
272}