Last active
May 27, 2018 15:23
-
-
Save alfianlosari/07eea1514ad3ae77b42c0141d5eafbd3 to your computer and use it in GitHub Desktop.
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
class Home extends StatefulWidget { | |
Home({Key key}) : super(key: key); | |
@override | |
State<StatefulWidget> createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
List<Repo> _repos = List(); | |
bool _isFetching = false; | |
String _error; | |
@override | |
void initState() { | |
super.initState(); | |
loadTrendingRepos(); | |
} | |
void loadTrendingRepos() async { | |
setState(() { | |
_isFetching = true; | |
_error = null; | |
}); | |
final repos = await Api.getTrendingRepositories(); | |
setState(() { | |
_isFetching = false; | |
if (repos != null) { | |
this._repos = repos; | |
} else { | |
_error = 'Error fetching repos'; | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Container( | |
margin: EdgeInsets.only(top: 4.0), | |
child: Column( | |
children: <Widget>[ | |
Text('Github Repos', | |
style: Theme | |
.of(context) | |
.textTheme | |
.headline | |
.apply(color: Colors.white)), | |
Text('Trending', | |
style: Theme | |
.of(context) | |
.textTheme | |
.subhead | |
.apply(color: Colors.white)) | |
], | |
)), | |
centerTitle: true, | |
actions: <Widget>[ | |
IconButton(icon: Icon(Icons.search), onPressed: () {}), | |
], | |
), | |
body: buildBody(context), | |
); | |
} | |
Widget buildBody(BuildContext context) { | |
if (_isFetching) { | |
return Container( | |
alignment: Alignment.center, child: Icon(Icons.timelapse)); | |
} else if (_error != null) { | |
return Container( | |
alignment: Alignment.center, | |
child: Text( | |
_error, | |
style: Theme.of(context).textTheme.headline, | |
)); | |
} else { | |
return ListView.builder( | |
padding: EdgeInsets.symmetric(vertical: 8.0), | |
itemCount: _repos.length, | |
itemBuilder: (BuildContext context, int index) { | |
return GithubItem(_repos[index]); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment