Skip to content

Instantly share code, notes, and snippets.

@castrors
Created February 20, 2018 17:15
Show Gist options
  • Save castrors/ab43defcc6490b556825a95bcc796a32 to your computer and use it in GitHub Desktop.
Save castrors/ab43defcc6490b556825a95bcc796a32 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(new MaterialApp(
home: new RepoListPage(),
routes: <String, WidgetBuilder>{
'/screen1': (BuildContext context) => new RepoListPage(),
'/screen2': (BuildContext context) => new Screen2()
},
));
}
class RepoListPage extends StatefulWidget {
@override
RepoListPageState createState() => new RepoListPageState();
}
class RepoListPageState extends State<RepoListPage> {
List data;
Future<String> getData() async {
var response = await http.get(
Uri.encodeFull(
"https://api.github.com/search/repositories?q=language:Java&sort=stars"),
headers: {"Accept": "application/json"});
this.setState(() {
data = JSON.decode(response.body)["items"];
});
print(data[1]["name"]);
return "Success!";
}
@override
void initState() {
this.getData();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Listviews"),
),
body: new ListView.builder(
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
return createListItem(data[index]);
},
),
);
}
Widget createListItem(data) => new GestureDetector(
onTap: () {
button1(context);
},
child: new Container(
padding: const EdgeInsets.all(32.0),
child: new Row(
children: [
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
new Container(
padding: const EdgeInsets.only(bottom: 8.0),
child: new Text(
data["name"],
style: new TextStyle(
fontWeight: FontWeight.bold,
),
),
),
new Text(
data["description"],
style: new TextStyle(
color: Colors.grey[500],
),
),
new Row(
children: <Widget>[
new Icon(
Icons.content_copy,
color: Colors.red[500],
),
new Text('${data["forks_count"]}'),
new Icon(
Icons.star,
color: Colors.red[500],
),
new Text('${data["stargazers_count"]}'),
],
)
],
),
),
// new Image.network(data["owner"]["avatar_url"]),
],
),
));
}
class Screen2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 2"),
),
body: new Center(
child: new Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new RaisedButton(
onPressed: () {
button2(context);
},
child: new Text("Back to Screen 1"),
)
],
),
),
);
}
}
void button1(BuildContext context) {
print("Button 1");
Navigator.of(context).pushNamed('/screen2');
}
void button2(BuildContext context) {
print("Button 2");
Navigator.of(context).pop(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment