Last active
June 4, 2019 13:23
-
-
Save churabou/6a34a0236bcf1ca69006b832cfbea789 to your computer and use it in GitHub Desktop.
Flutter Qiita API Example
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 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
class User { | |
final String id; | |
final String iconUrl; | |
User({this.id, this.iconUrl}); | |
factory User.fromJson(Map<String, dynamic> json) { | |
return User( | |
id: json['id'], | |
iconUrl: json['profile_image_url'], | |
); | |
} | |
} | |
class Article { | |
final String title; | |
final String url; | |
final User user; | |
Article({this.title, this.url, this.user}); | |
factory Article.fromJson(Map<String, dynamic> json) { | |
return Article( | |
title: json['title'], | |
url: json['url'], | |
user: User.fromJson(json['user']), | |
); | |
} | |
} | |
class QiitaClient { | |
static Future<List<Article>> fetchArticle() async { | |
final url = 'https://qiita.com/api/v2/items'; | |
final response = await http.get(url); | |
if (response.statusCode == 200) { | |
final List<dynamic> jsonArray = json.decode(response.body); | |
return jsonArray.map((json) => Article.fromJson(json)).toList(); | |
} else { | |
throw Exception('Failed to load article'); | |
} | |
} | |
} | |
class ArticleListView extends StatelessWidget { | |
final List<Article> articles; | |
ArticleListView({Key key, this.articles}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: articles.length, | |
itemBuilder: (BuildContext context, int index) { | |
final article = articles[index]; | |
return ListTile( | |
leading: CircleAvatar( | |
backgroundImage: NetworkImage(article.user.iconUrl), | |
), | |
title: Text(article.title), | |
onTap: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => ArticleDetailPage(article: article)), | |
); | |
}); | |
}, | |
); | |
} | |
} | |
class ArticleDetailPage extends StatelessWidget { | |
final Article article; | |
ArticleDetailPage({Key key, this.article}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Fetch Data Example', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Scaffold( | |
body: Center( | |
child: WebView( | |
initialUrl: article.url, | |
), | |
), | |
), | |
); | |
} | |
} | |
class ArticleListPage extends StatelessWidget { | |
final Future<List<Article>> articles = QiitaClient.fetchArticle(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Fetch Data Example', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Qiita API '), | |
), | |
body: Center( | |
child: FutureBuilder<List<Article>>( | |
future: articles, | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return ArticleListView(articles: snapshot.data); | |
} | |
return CircularProgressIndicator(); | |
}, | |
), | |
), | |
), | |
); | |
} | |
} | |
void main() { | |
runApp(ArticleListPage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment