Created
June 3, 2018 06:24
-
-
Save alfianlosari/e3fee667717a9042735efe44cf2bb61b to your computer and use it in GitHub Desktop.
Search List Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:github_search/api.dart'; | |
import 'package:github_search/repo.dart'; | |
import 'package:github_search/item.dart'; | |
import 'dart:async'; | |
class SearchList extends StatefulWidget { | |
SearchList({Key key}) : super(key: key); | |
@override | |
State<StatefulWidget> createState() => _SearchState(); | |
} | |
class _SearchState extends State<SearchList> { | |
final FocusNode myFocusNode = new FocusNode(); | |
final key = GlobalKey<ScaffoldState>(); | |
final TextEditingController _searchQuery = TextEditingController(); | |
bool _isSearching = false; | |
String _error; | |
List<Repo> _results = List(); | |
Timer debounceTimer; | |
_SearchState() { | |
_searchQuery.addListener(() { | |
if (debounceTimer != null) { | |
debounceTimer.cancel(); | |
} | |
debounceTimer = Timer(Duration(milliseconds: 500), () { | |
if (this.mounted) { | |
performSearch(_searchQuery.text); | |
} | |
}); | |
}); | |
} | |
void performSearch(String query) async { | |
if (query.isEmpty) { | |
setState(() { | |
_isSearching = false; | |
_error = null; | |
_results = List(); | |
}); | |
return; | |
} | |
setState(() { | |
_isSearching = true; | |
_error = null; | |
_results = List(); | |
}); | |
final repos = await Api.getRepositoriesWithSearchQuery(query); | |
if (this._searchQuery.text == query && this.mounted) { | |
setState(() { | |
_isSearching = false; | |
if (repos != null) { | |
_results = repos; | |
} else { | |
_error = 'Error searching repos'; | |
} | |
}); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
key: key, | |
appBar: AppBar( | |
centerTitle: true, | |
title: TextField( | |
autofocus: true, | |
controller: _searchQuery, | |
style: TextStyle(color: Colors.white), | |
decoration: InputDecoration( | |
border: InputBorder.none, | |
prefixIcon: Padding( | |
padding: EdgeInsetsDirectional.only(end: 16.0), | |
child: Icon( | |
Icons.search, | |
color: Colors.white, | |
)), | |
hintText: "Search repositories...", | |
hintStyle: TextStyle(color: Colors.white)), | |
), | |
), | |
body: buildBody(context)); | |
} | |
Widget buildBody(BuildContext context) { | |
if (_isSearching) { | |
return CenterTitle('Searching Github...'); | |
} else if (_error != null) { | |
return CenterTitle(_error); | |
} else if (_searchQuery.text.isEmpty) { | |
return CenterTitle('Begin Search by typing on search bar'); | |
} else { | |
return ListView.builder( | |
padding: EdgeInsets.symmetric(vertical: 8.0), | |
itemCount: _results.length, | |
itemBuilder: (BuildContext context, int index) { | |
return GithubItem(_results[index]); | |
}); | |
} | |
} | |
} | |
class CenterTitle extends StatelessWidget { | |
final String title; | |
CenterTitle(this.title); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0), | |
alignment: Alignment.center, | |
child: Text( | |
title, | |
style: Theme.of(context).textTheme.headline, | |
textAlign: TextAlign.center, | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment