diff --git a/lib/screens/home.dart b/lib/screens/home.dart index f1be41d..27230b7 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -21,91 +21,117 @@ class Home extends StatefulWidget { State createState() => HomeState(); } -class HomeState extends State { +class HomeState extends State with TickerProviderStateMixin { + static String title = "InfoProvas"; + bool isSearching = false; + List _subject = []; List _professor = []; List _searchList = []; - String title = "InfoProvas"; - @override - void initState() { - super.initState(); - listenForProfessor(); - listenForSubject(); - } + TextEditingController _query = TextEditingController(); + AnimationController _animationController; - @override - Widget build(BuildContext context) { - double screenWidth = MediaQuery.of(context).size.width; + Widget leading; - return Scaffold( - key: _scaffoldKey, - drawer: DrawerScreen(), - appBar: AppBar( - centerTitle: true, - backgroundColor: Style.mainTheme.primaryColor, - title: Text("InfoProvas"), - elevation: 0.0, - actions: [ - IconButton( - icon: Icon(Icons.search, color: Colors.white), - onPressed: () { - populateSearchList(); - showSearch( - context: context, - delegate: SearchPage( - professor: _professor, - searchList: _searchList, - subject: _subject), - ); - }, - ), + Widget actionAnimated() => AnimatedCrossFade( + firstChild: searchButton(), + secondChild: clearButton(), + crossFadeState: + isSearching ? CrossFadeState.showSecond : CrossFadeState.showFirst, + duration: Duration(milliseconds: 500), + ); + + Widget searchButton() => IconButton( + icon: Icon(Icons.search, color: Colors.white), + onPressed: () { + _animationController.forward(); + populateSearchList(); + setState(() { + isSearching = true; + leading = backButton(); + }); + }, + ); + + Widget clearButton() => IconButton( + icon: Icon( + Icons.close, + color: Colors.white, + ), + onPressed: () { + _query.text = ''; + }, + ); + + Widget appBarTitle() => Row( + mainAxisSize: MainAxisSize.max, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text(title), ], + ); + + Widget appBarContent() { + return AnimatedCrossFade( + firstChild: appBarTitle(), + secondChild: textField(), + duration: Duration(milliseconds: 500), + crossFadeState: + isSearching ? CrossFadeState.showSecond : CrossFadeState.showFirst, + ); + } + + Widget textField() { + return TextField( + onChanged: (value) { + setState(() { + SearchScreen( + query: _query.text, + subject: _subject, + professor: _professor, + searchList: _searchList); + }); + }, + controller: _query, + enabled: isSearching, + decoration: InputDecoration.collapsed( + hintStyle: TextStyle(color: Colors.white70), hintText: 'Busca'), + cursorColor: Colors.white, + style: TextStyle( + color: Colors.white, ), - body: DefaultTabController( - length: 2, - child: Column( - children: [ - Material( - elevation: 2.0, - child: Container( - width: screenWidth, - color: Style.mainTheme.primaryColor, - child: Column( - children: [ - TabBar( - isScrollable: true, - indicatorSize: TabBarIndicatorSize.label, - indicator: BubbleTabIndicator( - indicatorColor: Colors.white, - indicatorHeight: 20.0, - tabBarIndicatorSize: TabBarIndicatorSize.label, - ), - unselectedLabelColor: Colors.white, - labelColor: Style.mainTheme.primaryColor, - tabs: [ - Tab(child: Text("Disciplinas")), - Tab(child: Text("Professores")), - ], - ), - ], - ), - ), - ), - Expanded( - child: TabBarView( - children: [ - SubjectsTab(_subject), - ProfessorTab(_professor), - ], - ), - ), - ], + ); + } + + Widget menuButton() { + return IconButton( + icon: AnimatedIcon( + icon: AnimatedIcons.menu_arrow, + progress: _animationController, ), + onPressed: () { + _scaffoldKey.currentState.openDrawer(); + }); + } + + Widget backButton() { + return IconButton( + icon: AnimatedIcon( + icon: AnimatedIcons.menu_arrow, + progress: _animationController, ), + onPressed: () => closeSearch(), ); } + void closeSearch(){ + _animationController.reverse(); + setState(() { + isSearching = false; + }); + } + void populateSearchList() { _searchList.clear(); _subject.forEach((subject) => @@ -117,9 +143,10 @@ class HomeState extends State { void listenForSubject() async { try { _subject.addAll(await getSubject()); - _subject - .sort((a, b) => removeAccent(a.name).compareTo(removeAccent(b.name))); - setState(() {}); + setState(() { + _subject.sort( + (a, b) => removeAccent(a.name).compareTo(removeAccent(b.name))); + }); } catch (e) { onFailedConnection(); } @@ -176,4 +203,95 @@ class HomeState extends State { return name; } } + + @override + void initState() { + super.initState(); + _animationController = AnimationController( + vsync: this, + duration: Duration(milliseconds: 500), + ); + leading = menuButton(); + listenForProfessor(); + listenForSubject(); + } + + @override + void dispose() { + super.dispose(); + _animationController.dispose(); + } + + @override + Widget build(BuildContext context) { + return WillPopScope( + onWillPop: () async { + if (isSearching) { + closeSearch(); + return false; + } + return true; + }, + child: Scaffold( + key: _scaffoldKey, + drawer: DrawerScreen(), + appBar: AppBar( + backgroundColor: Style.mainTheme.primaryColor, + title: appBarContent(), + leading: isSearching ? backButton() : menuButton(), + elevation: 0.0, + actions: [actionAnimated()], + ), + body: isSearching + ? SearchScreen( + searchList: _searchList, + professor: _professor, + subject: _subject, + query: _query.text, + ) + : DefaultTabController( + length: 2, + child: Column( + children: [ + Material( + elevation: 2.0, + child: Container( + width: double.maxFinite, + color: Style.mainTheme.primaryColor, + child: Column( + children: [ + TabBar( + isScrollable: true, + indicatorSize: TabBarIndicatorSize.label, + indicator: BubbleTabIndicator( + indicatorColor: Colors.white, + indicatorHeight: 20.0, + tabBarIndicatorSize: + TabBarIndicatorSize.label, + ), + unselectedLabelColor: Colors.white, + labelColor: Style.mainTheme.primaryColor, + tabs: [ + Tab(child: Text("Disciplinas")), + Tab(child: Text("Professores")), + ], + ), + ], + ), + ), + ), + Expanded( + child: TabBarView( + children: [ + SubjectsTab(_subject), + ProfessorTab(_professor), + ], + ), + ), + ], + ), + ), + ), + ); + } } diff --git a/lib/screens/search.dart b/lib/screens/search.dart index cf62567..12840d4 100644 --- a/lib/screens/search.dart +++ b/lib/screens/search.dart @@ -1,73 +1,29 @@ -import 'dart:async'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:infoprovas/model/professor.dart'; import 'package:infoprovas/model/search_item.dart'; import 'package:infoprovas/model/subject.dart'; -import 'package:infoprovas/styles/style.dart'; import 'package:infoprovas/widgets/search_tile.dart'; -class SearchPage extends SearchDelegate { +class SearchScreen extends StatefulWidget { final List searchList; final List professor; final List subject; + final String query; - SearchPage({this.searchList, this.professor, this.subject}); + SearchScreen({this.searchList, this.professor, this.subject, this.query}); @override - ThemeData appBarTheme(BuildContext context) { - assert(context != null); - final ThemeData theme = Theme.of(context); - assert(theme != null); - return theme.copyWith( - primaryColor: Style.mainTheme.primaryColor, - primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.white), - primaryColorBrightness: theme.primaryColorBrightness, - hintColor: Style.mainTheme.hintColor, - textTheme: theme.textTheme.copyWith( - title: theme.textTheme.title - .copyWith(color: theme.primaryTextTheme.title.color)), - primaryTextTheme: theme.textTheme, - ); - } - - @override - List buildActions(BuildContext context) { - return [ - IconButton( - icon: Icon(Icons.clear, color: Colors.white,), - onPressed: () { - query = ''; - }, - ), - ]; - } - - @override - Widget buildLeading(BuildContext context) { - return IconButton( - icon: Icon(Icons.arrow_back, color: Colors.white,), - onPressed: () { - close(context, null); - }, - ); - } + _SearchScreenState createState() => _SearchScreenState(); +} +class _SearchScreenState extends State { @override - Widget buildResults(BuildContext context) { - return returnList(); - } + Widget build(BuildContext context) { + var results = widget.searchList + .where((a) => a.name.toLowerCase().contains(widget.query)) + .toList(); - @override - Widget buildSuggestions(BuildContext context) { - return returnList(); - } - - Widget returnList() { - var results = - searchList.where((a) => a.name.toLowerCase().contains(query)).toList(); - - return results.length == 0 || results.length == searchList.length + return results.length == 0 || results.length == widget.searchList.length ? Center( child: Text("Sem resultados"), ) @@ -77,13 +33,13 @@ class SearchPage extends SearchDelegate { name: results[index].name, type: results[index].type, professor: identical(results[index].type, "professor") - ? professor + ? widget.professor .where((professor) => identical(results[index].name, professor.name)) .first : null, subject: identical(results[index].type, "subject") - ? subject + ? widget.subject .where((subject) => identical(results[index].name, subject.name)) .first @@ -92,26 +48,3 @@ class SearchPage extends SearchDelegate { ); } } - -class CustomLocalizationDelegate extends LocalizationsDelegate { - const CustomLocalizationDelegate(); - - @override - bool isSupported(Locale locale) => locale.languageCode == 'pt'; - - @override - Future load(Locale locale) => SynchronousFuture(const CustomLocalization()); - - @override - bool shouldReload(CustomLocalizationDelegate old) => false; - - @override - String toString() => 'CustomLocalization.delegate(pt_BR)'; -} - -class CustomLocalization extends DefaultMaterialLocalizations { - const CustomLocalization(); - - @override - String get searchFieldLabel => "Busca"; -} diff --git a/lib/widgets/search_tile.dart b/lib/widgets/search_tile.dart index 039912d..3df8350 100644 --- a/lib/widgets/search_tile.dart +++ b/lib/widgets/search_tile.dart @@ -17,12 +17,12 @@ class SearchTile extends StatelessWidget { @override Widget build(BuildContext context) { return ListTile( - leading: - identical(type, "professor") ? Icon(OMIcons.person) : Icon(OMIcons.book), + leading: identical(type, "professor") + ? Icon(OMIcons.person) + : Icon(OMIcons.book), title: Text(name), onTap: () { if (identical(type, "professor")) { - Navigator.of(context).pop(); Navigator.push( context, MaterialPageRoute( @@ -30,7 +30,6 @@ class SearchTile extends StatelessWidget { ), ); } else if (identical(type, "subject")) { - Navigator.of(context).pop(); Navigator.push( context, MaterialPageRoute(