Skip to content

Instantly share code, notes, and snippets.

@onliniak
Created May 12, 2022 22:05
Show Gist options
  • Save onliniak/130d4024c60e3bb8ca2562a84c207634 to your computer and use it in GitHub Desktop.
Save onliniak/130d4024c60e3bb8ca2562a84c207634 to your computer and use it in GitHub Desktop.
Flutter RealWorld Demo
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: HTTP().getUser(),
builder: // https://stackoverflow.com/questions/51983011/when-should-i-use-a-futurebuilder
(BuildContext context,
AsyncSnapshot<Response<dynamic>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Text('Loading....');
default:
// if (snapshot.hasError) {
// return Text('Error: ${snapshot.error}');
// } else {
List articles = snapshot.data?.body['articles'];
return ListView.builder(
itemCount: articles.length,
itemBuilder: (context, index) {
var article = Article.fromJson(articles[index]);
DateTime formatDate =
DateTime.parse(article.createdAtString);
String createdAt =
DateFormat("MMMM dd, yyyy").format(formatDate);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(children: [
Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
Row(
children: [
Image.network(article.author['image']),
Column(
children: [
Text(article.author['username']),
Text(createdAt)
],
)
],
),
Row(
children: [
Text(article.favoritesCount.toString())
],
)
],
),
Row(
children: [
Text(article.title),
Text(article.description)
],
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [Text('Read More'), Text('data')],
),
],
),
Column(
children: [Text('datadatafafaffafaf')],
)
]));
},
);
}
}
// },
),
);
}
}
class Controller extends GetxController {}
class HTTP extends GetConnect {
Future<Response> getUser() => get('https://api.realworld.io/api/articles');
}
class Article {
final String slug;
final String title;
final String description;
final String body;
final List? tagList;
final String createdAtString;
final String updatedAt;
final bool favorited;
final int favoritesCount;
final Map<String, dynamic> author;
Article(
this.author,
this.body,
this.createdAtString,
this.description,
this.favorited,
this.favoritesCount,
this.slug,
this.tagList,
this.title,
this.updatedAt);
Article.fromJson(Map<String, dynamic> json)
: author = {
"username": json['author']['username'],
"bio": json['author']['bio'],
"image": json['author']['image'],
"following": json['author']['following']
},
body = json['body'],
createdAtString = json['createdAt'],
description = json['description'],
favorited = json['favorited'],
favoritesCount = json['favoritesCount'],
slug = json['slug'],
tagList = json['tagList'],
title = json['title'],
updatedAt = json['updatedAt'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment