Skip to content

Instantly share code, notes, and snippets.

@TobiCrackIT
Created June 24, 2020 23:21
Show Gist options
  • Save TobiCrackIT/7a7a9ee603e046722b0fa9e1bec791e3 to your computer and use it in GitHub Desktop.
Save TobiCrackIT/7a7a9ee603e046722b0fa9e1bec791e3 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:githubapp/user.dart';
import 'package:url_launcher/url_launcher.dart';
import 'service.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
APIService apiService = APIService();
bool isLoading = true;
List<User> users;
@override
void initState() {
super.initState();
fetchGithubUsersList();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text(
'Github Users',
style: TextStyle(color: Colors.white, fontSize: 14),
),
centerTitle: false,
backgroundColor: Colors.black87,
),
body: isLoading
? Center(
child: Container(
margin: EdgeInsets.symmetric(vertical: 20),
alignment: Alignment.center,
child: SizedBox(
child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Colors.black),
strokeWidth: 3.0,
),
height: 40,
width: 40,
),
),
)
: ListView.separated(
itemBuilder: (context, index) => userTile(
users[index].login,
users[index].avatarUrl,
users[index].type,
users[index].htmlUrl),
separatorBuilder: (context, index) => Divider(
height: 1,
),
itemCount: users.length,
shrinkWrap: true,
),
);
}
Widget userTile(
String fullName, String avatarUrl, String location, String pageUrl) {
return ListTile(
leading: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
color: Colors.black87,
borderRadius: BorderRadius.all(Radius.circular(25))),
child: ClipRRect(
borderRadius: BorderRadius.circular(25),
child: Image.network(
//'https://picsum.photos/250?image=9',
avatarUrl,
fit: BoxFit.contain,
),
),
),
title: Text(
'$fullName',
style: TextStyle(
color: Colors.black87, fontSize: 15, fontWeight: FontWeight.w500),
),
subtitle: Text(
'$location',
style: TextStyle(
color: Colors.grey, fontSize: 12, fontWeight: FontWeight.w300),
),
trailing: GestureDetector(
//onTap: () => _launchURL('https://github.com/'),
onTap: () => _launchURL(pageUrl),
child: Container(
height: 25,
width: 80,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 1.5),
borderRadius: BorderRadius.all(Radius.circular(22))),
padding: EdgeInsets.all(3),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'View Profile',
style: TextStyle(
color: Colors.black87,
fontSize: 8,
fontWeight: FontWeight.w400),
),
Image.asset(
'assets/images/github.png',
width: 20,
height: 20,
),
],
),
),
),
);
}
_launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
fetchGithubUsersList() async {
setState(() {
isLoading = true;
});
var usersResponse = apiService.getUsers();
usersResponse.then((response) {
if (response.statusCode == 200) {
var decodedJson = json.decode(response.body);
debugPrint(decodedJson.toString());
setState(() {
isLoading = false;
users = UsersModel.fromJson(decodedJson).usersList;
debugPrint('Users list size: ${users.length}');
});
} else {
setState(() {
isLoading = false;
});
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Error"),
content: Text('Could not retrieve data'),
actions: [
FlatButton(
child: Text("Ok"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
}).catchError((onError) {
print(onError);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Error"),
content: Text('${onError.toString()}'),
actions: [
FlatButton(
child: Text("Ok"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
});
}
}
@Blezn
Copy link

Blezn commented Jul 12, 2020

thanks sir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment