@@ -21,91 +21,117 @@ class Home extends StatefulWidget {
21
21
State <StatefulWidget > createState () => HomeState ();
22
22
}
23
23
24
- class HomeState extends State <Home > {
24
+ class HomeState extends State <Home > with TickerProviderStateMixin {
25
+ static String title = "InfoProvas" ;
26
+ bool isSearching = false ;
27
+
25
28
List <Subject > _subject = < Subject > [];
26
29
List <Professor > _professor = < Professor > [];
27
30
List <SearchItem > _searchList = < SearchItem > [];
28
- String title = "InfoProvas" ;
29
31
30
- @override
31
- void initState () {
32
- super .initState ();
33
- listenForProfessor ();
34
- listenForSubject ();
35
- }
32
+ TextEditingController _query = TextEditingController ();
33
+ AnimationController _animationController;
36
34
37
- @override
38
- Widget build (BuildContext context) {
39
- double screenWidth = MediaQuery .of (context).size.width;
35
+ Widget leading;
40
36
41
- return Scaffold (
42
- key: _scaffoldKey,
43
- drawer: DrawerScreen (),
44
- appBar: AppBar (
45
- centerTitle: true ,
46
- backgroundColor: Style .mainTheme.primaryColor,
47
- title: Text ("InfoProvas" ),
48
- elevation: 0.0 ,
49
- actions: < Widget > [
50
- IconButton (
51
- icon: Icon (Icons .search, color: Colors .white),
52
- onPressed: () {
53
- populateSearchList ();
54
- showSearch (
55
- context: context,
56
- delegate: SearchPage (
57
- professor: _professor,
58
- searchList: _searchList,
59
- subject: _subject),
60
- );
61
- },
62
- ),
37
+ Widget actionAnimated () => AnimatedCrossFade (
38
+ firstChild: searchButton (),
39
+ secondChild: clearButton (),
40
+ crossFadeState:
41
+ isSearching ? CrossFadeState .showSecond : CrossFadeState .showFirst,
42
+ duration: Duration (milliseconds: 500 ),
43
+ );
44
+
45
+ Widget searchButton () => IconButton (
46
+ icon: Icon (Icons .search, color: Colors .white),
47
+ onPressed: () {
48
+ _animationController.forward ();
49
+ populateSearchList ();
50
+ setState (() {
51
+ isSearching = true ;
52
+ leading = backButton ();
53
+ });
54
+ },
55
+ );
56
+
57
+ Widget clearButton () => IconButton (
58
+ icon: Icon (
59
+ Icons .close,
60
+ color: Colors .white,
61
+ ),
62
+ onPressed: () {
63
+ _query.text = '' ;
64
+ },
65
+ );
66
+
67
+ Widget appBarTitle () => Row (
68
+ mainAxisSize: MainAxisSize .max,
69
+ mainAxisAlignment: MainAxisAlignment .center,
70
+ children: < Widget > [
71
+ Text (title),
63
72
],
73
+ );
74
+
75
+ Widget appBarContent () {
76
+ return AnimatedCrossFade (
77
+ firstChild: appBarTitle (),
78
+ secondChild: textField (),
79
+ duration: Duration (milliseconds: 500 ),
80
+ crossFadeState:
81
+ isSearching ? CrossFadeState .showSecond : CrossFadeState .showFirst,
82
+ );
83
+ }
84
+
85
+ Widget textField () {
86
+ return TextField (
87
+ onChanged: (value) {
88
+ setState (() {
89
+ SearchScreen (
90
+ query: _query.text,
91
+ subject: _subject,
92
+ professor: _professor,
93
+ searchList: _searchList);
94
+ });
95
+ },
96
+ controller: _query,
97
+ enabled: isSearching,
98
+ decoration: InputDecoration .collapsed (
99
+ hintStyle: TextStyle (color: Colors .white70), hintText: 'Busca' ),
100
+ cursorColor: Colors .white,
101
+ style: TextStyle (
102
+ color: Colors .white,
64
103
),
65
- body: DefaultTabController (
66
- length: 2 ,
67
- child: Column (
68
- children: < Widget > [
69
- Material (
70
- elevation: 2.0 ,
71
- child: Container (
72
- width: screenWidth,
73
- color: Style .mainTheme.primaryColor,
74
- child: Column (
75
- children: < Widget > [
76
- TabBar (
77
- isScrollable: true ,
78
- indicatorSize: TabBarIndicatorSize .label,
79
- indicator: BubbleTabIndicator (
80
- indicatorColor: Colors .white,
81
- indicatorHeight: 20.0 ,
82
- tabBarIndicatorSize: TabBarIndicatorSize .label,
83
- ),
84
- unselectedLabelColor: Colors .white,
85
- labelColor: Style .mainTheme.primaryColor,
86
- tabs: [
87
- Tab (child: Text ("Disciplinas" )),
88
- Tab (child: Text ("Professores" )),
89
- ],
90
- ),
91
- ],
92
- ),
93
- ),
94
- ),
95
- Expanded (
96
- child: TabBarView (
97
- children: [
98
- SubjectsTab (_subject),
99
- ProfessorTab (_professor),
100
- ],
101
- ),
102
- ),
103
- ],
104
+ );
105
+ }
106
+
107
+ Widget menuButton () {
108
+ return IconButton (
109
+ icon: AnimatedIcon (
110
+ icon: AnimatedIcons .menu_arrow,
111
+ progress: _animationController,
104
112
),
113
+ onPressed: () {
114
+ _scaffoldKey.currentState.openDrawer ();
115
+ });
116
+ }
117
+
118
+ Widget backButton () {
119
+ return IconButton (
120
+ icon: AnimatedIcon (
121
+ icon: AnimatedIcons .menu_arrow,
122
+ progress: _animationController,
105
123
),
124
+ onPressed: () => closeSearch (),
106
125
);
107
126
}
108
127
128
+ void closeSearch (){
129
+ _animationController.reverse ();
130
+ setState (() {
131
+ isSearching = false ;
132
+ });
133
+ }
134
+
109
135
void populateSearchList () {
110
136
_searchList.clear ();
111
137
_subject.forEach ((subject) =>
@@ -117,9 +143,10 @@ class HomeState extends State<Home> {
117
143
void listenForSubject () async {
118
144
try {
119
145
_subject.addAll (await getSubject ());
120
- _subject
121
- .sort ((a, b) => removeAccent (a.name).compareTo (removeAccent (b.name)));
122
- setState (() {});
146
+ setState (() {
147
+ _subject.sort (
148
+ (a, b) => removeAccent (a.name).compareTo (removeAccent (b.name)));
149
+ });
123
150
} catch (e) {
124
151
onFailedConnection ();
125
152
}
@@ -176,4 +203,95 @@ class HomeState extends State<Home> {
176
203
return name;
177
204
}
178
205
}
206
+
207
+ @override
208
+ void initState () {
209
+ super .initState ();
210
+ _animationController = AnimationController (
211
+ vsync: this ,
212
+ duration: Duration (milliseconds: 500 ),
213
+ );
214
+ leading = menuButton ();
215
+ listenForProfessor ();
216
+ listenForSubject ();
217
+ }
218
+
219
+ @override
220
+ void dispose () {
221
+ super .dispose ();
222
+ _animationController.dispose ();
223
+ }
224
+
225
+ @override
226
+ Widget build (BuildContext context) {
227
+ return WillPopScope (
228
+ onWillPop: () async {
229
+ if (isSearching) {
230
+ closeSearch ();
231
+ return false ;
232
+ }
233
+ return true ;
234
+ },
235
+ child: Scaffold (
236
+ key: _scaffoldKey,
237
+ drawer: DrawerScreen (),
238
+ appBar: AppBar (
239
+ backgroundColor: Style .mainTheme.primaryColor,
240
+ title: appBarContent (),
241
+ leading: isSearching ? backButton () : menuButton (),
242
+ elevation: 0.0 ,
243
+ actions: < Widget > [actionAnimated ()],
244
+ ),
245
+ body: isSearching
246
+ ? SearchScreen (
247
+ searchList: _searchList,
248
+ professor: _professor,
249
+ subject: _subject,
250
+ query: _query.text,
251
+ )
252
+ : DefaultTabController (
253
+ length: 2 ,
254
+ child: Column (
255
+ children: < Widget > [
256
+ Material (
257
+ elevation: 2.0 ,
258
+ child: Container (
259
+ width: double .maxFinite,
260
+ color: Style .mainTheme.primaryColor,
261
+ child: Column (
262
+ children: < Widget > [
263
+ TabBar (
264
+ isScrollable: true ,
265
+ indicatorSize: TabBarIndicatorSize .label,
266
+ indicator: BubbleTabIndicator (
267
+ indicatorColor: Colors .white,
268
+ indicatorHeight: 20.0 ,
269
+ tabBarIndicatorSize:
270
+ TabBarIndicatorSize .label,
271
+ ),
272
+ unselectedLabelColor: Colors .white,
273
+ labelColor: Style .mainTheme.primaryColor,
274
+ tabs: [
275
+ Tab (child: Text ("Disciplinas" )),
276
+ Tab (child: Text ("Professores" )),
277
+ ],
278
+ ),
279
+ ],
280
+ ),
281
+ ),
282
+ ),
283
+ Expanded (
284
+ child: TabBarView (
285
+ children: [
286
+ SubjectsTab (_subject),
287
+ ProfessorTab (_professor),
288
+ ],
289
+ ),
290
+ ),
291
+ ],
292
+ ),
293
+ ),
294
+ ),
295
+ );
296
+ }
179
297
}
0 commit comments