Skip to content

Instantly share code, notes, and snippets.

@CoderJava
Created April 21, 2019 18:28
Show Gist options
  • Select an option

  • Save CoderJava/f8499f7d3663e2940fd22513c153005f to your computer and use it in GitHub Desktop.

Select an option

Save CoderJava/f8499f7d3663e2940fd22513c153005f to your computer and use it in GitHub Desktop.
Flutter super app with single file
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
http.Client _client;
Future<List<Profile>> _futureProfiles;
@override
void initState() {
_client = http.Client();
_futureProfiles = _getProfiles();
super.initState();
}
Future<List<Profile>> _getProfiles() async {
String url = "http://api.bengkelrobot.net:8001";
final response = await _client.get("$url/profile");
if (response == 200) {
return profileFromJson(response.body);
} else {
throw Exception("failed get profiles");
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: FutureBuilder(
future: _futureProfiles,
builder: (BuildContext context, AsyncSnapshot<List<Profile>> snapshot) {
if (snapshot.hasError) {
return Center(
child: Text(
"Something wrong with message: ${snapshot.error.toString()}"),
);
} else if (snapshot.connectionState == ConnectionState.done) {
List<Profile> profiles = snapshot.data;
return _buildListView(profiles);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
Widget _buildListView(List<Profile> profiles) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: ListView.builder(
itemBuilder: (context, index) {
Profile profile = profiles[index];
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
profile.name,
style: Theme.of(context).textTheme.title,
),
Text(profile.email),
Text(profile.age.toString()),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {
// TODO: do something in here
},
child: Text(
"Delete",
style: TextStyle(color: Colors.red),
),
),
FlatButton(
onPressed: () {
// TODO: do something in here
},
child: Text(
"Edit",
style: TextStyle(color: Colors.blue),
),
),
],
),
],
),
),
),
);
},
itemCount: profiles.length,
),
);
}
}
class Profile {
int id;
String name;
String email;
int age;
Profile({this.id = 0, this.name, this.email, this.age});
factory Profile.fromJson(Map<String, dynamic> map) {
return Profile(
id: map["id"], name: map["name"], email: map["email"], age: map["age"]);
}
Map<String, dynamic> toJson() {
return {"id": id, "name": name, "email": email, "age": age};
}
@override
String toString() {
return 'Profile{id: $id, name: $name, email: $email, age: $age}';
}
}
List<Profile> profileFromJson(String jsonData) {
final data = json.decode(jsonData);
return List<Profile>.from(data.map((item) => Profile.fromJson(item)));
}
String profileToJson(Profile data) {
final jsonData = data.toJson();
return json.encode(jsonData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment