diff --git a/lib/models/home.dart b/lib/models/home.dart index 923fc49..92112e8 100644 --- a/lib/models/home.dart +++ b/lib/models/home.dart @@ -11,6 +11,10 @@ import 'package:path_provider/path_provider.dart'; class HomeProvider with ChangeNotifier { int _currentIndex = 0; + bool isSearching; + Icon searchIcon = Icon(Icons.search); + final TextEditingController searchQuery = new TextEditingController(); + Widget appBarTitle = Text('Ğecko', style: TextStyle(color: Colors.grey[850])); get currentIndex => _currentIndex; @@ -100,9 +104,31 @@ class HomeProvider with ChangeNotifier { } } + searchAction() { + //TODO: OPEN SEARCH MODE !! + print('Search mode'); + } + T getRandomElement(List list) { - final random = new Random(); + final random = Random(); var i = random.nextInt(list.length); return list[i]; } + + void handleSearchStart() { + isSearching = true; + notifyListeners(); + } + + void handleSearchEnd() { + searchIcon = Icon( + Icons.search, + color: Colors.grey[850], + ); + appBarTitle = Text('Ğecko', style: TextStyle(color: Colors.grey[850])); + isSearching = false; + searchQuery.clear(); + + notifyListeners(); + } } diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 7841f60..4517e54 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -65,12 +65,36 @@ class HomeScreen extends StatelessWidget { icon: new Icon(Icons.menu, color: Colors.grey[850]), onPressed: () => Scaffold.of(context).openDrawer(), )), - title: Text('Ğecko', style: TextStyle(color: Colors.grey[850])), + title: _homeProvider.appBarTitle, actions: [ Padding( - padding: EdgeInsets.symmetric(horizontal: 16), - child: Icon(Icons.search, color: Colors.grey[850]), - ), + padding: EdgeInsets.symmetric(horizontal: 16), + child: IconButton( + icon: _homeProvider.searchIcon, + color: Colors.grey[850], + onPressed: () { + if (_homeProvider.searchIcon.icon == Icons.search) { + _homeProvider.searchIcon = Icon( + Icons.close, + color: Colors.grey[850], + ); + TextField( + controller: _homeProvider.searchQuery, + style: new TextStyle( + color: Colors.white, + ), + decoration: new InputDecoration( + prefixIcon: + new Icon(Icons.search, color: Colors.white), + hintText: "Search...", + hintStyle: new TextStyle(color: Colors.white)), + ); + _homeProvider.handleSearchStart(); + } else { + _homeProvider.handleSearchEnd(); + } + _homeProvider.searchAction(); + })) ], backgroundColor: Color(0xffFFD58D), ), diff --git a/lib/screens/search.dart b/lib/screens/search.dart new file mode 100644 index 0000000..2f7811a --- /dev/null +++ b/lib/screens/search.dart @@ -0,0 +1,152 @@ +import 'package:flutter/material.dart'; + +class SearchList extends StatefulWidget { + SearchList({ Key key }) : super(key: key); + @override + _SearchListState createState() => new _SearchListState(); + +} + +class _SearchListState extends State +{ + Widget appBarTitle = new Text("Search Sample", style: new TextStyle(color: Colors.white),); + Icon actionIcon = new Icon(Icons.search, color: Colors.white,); + final key = new GlobalKey(); + final TextEditingController _searchQuery = new TextEditingController(); + List _list; + bool _IsSearching; + String _searchText = ""; + + _SearchListState() { + _searchQuery.addListener(() { + if (_searchQuery.text.isEmpty) { + setState(() { + _IsSearching = false; + _searchText = ""; + }); + } + else { + setState(() { + _IsSearching = true; + _searchText = _searchQuery.text; + }); + } + }); + } + + @override + void initState() { + super.initState(); + _IsSearching = false; + init(); + + } + + void init() { + _list = List(); + _list.add("Google"); + _list.add("IOS"); + _list.add("Andorid"); + _list.add("Dart"); + _list.add("Flutter"); + _list.add("Python"); + _list.add("React"); + _list.add("Xamarin"); + _list.add("Kotlin"); + _list.add("Java"); + _list.add("RxAndroid"); + } + + @override + Widget build(BuildContext context) { + return new Scaffold( + key: key, + appBar: buildBar(context), + body: new ListView( + padding: new EdgeInsets.symmetric(vertical: 8.0), + children: _IsSearching ? _buildSearchList() : _buildList(), + ), + ); + } + + List _buildList() { + return _list.map((contact) => new ChildItem(contact)).toList(); + } + + List _buildSearchList() { + if (_searchText.isEmpty) { + return _list.map((contact) => new ChildItem(contact)) + .toList(); + } + else { + List _searchList = List(); + for (int i = 0; i < _list.length; i++) { + String name = _list.elementAt(i); + if (name.toLowerCase().contains(_searchText.toLowerCase())) { + _searchList.add(name); + } + } + return _searchList.map((contact) => new ChildItem(contact)) + .toList(); + } + } + + Widget buildBar(BuildContext context) { + return new AppBar( + centerTitle: true, + title: appBarTitle, + actions: [ + new IconButton(icon: actionIcon, onPressed: () { + setState(() { + if (this.actionIcon.icon == Icons.search) { + this.actionIcon = new Icon(Icons.close, color: Colors.white,); + this.appBarTitle = new TextField( + controller: _searchQuery, + style: new TextStyle( + color: Colors.white, + + ), + decoration: new InputDecoration( + prefixIcon: new Icon(Icons.search, color: Colors.white), + hintText: "Search...", + hintStyle: new TextStyle(color: Colors.white) + ), + ); + _handleSearchStart(); + } + else { + _handleSearchEnd(); + } + }); + },), + ] + ); + } + + void _handleSearchStart() { + setState(() { + _IsSearching = true; + }); + } + + void _handleSearchEnd() { + setState(() { + this.actionIcon = new Icon(Icons.search, color: Colors.white,); + this.appBarTitle = + new Text("Search Sample", style: new TextStyle(color: Colors.white),); + _IsSearching = false; + _searchQuery.clear(); + }); + } + +} + +class ChildItem extends StatelessWidget { + final String name; + ChildItem(this.name); + @override + Widget build(BuildContext context) { + return new ListTile(title: new Text(this.name)); + } + +} \ No newline at end of file