Skip to content

Instantly share code, notes, and snippets.

@fleetimee
Created April 21, 2022 23:21
Show Gist options
  • Save fleetimee/96f558a7807c4969c6a5b996e1025664 to your computer and use it in GitHub Desktop.
Save fleetimee/96f558a7807c4969c6a5b996e1025664 to your computer and use it in GitHub Desktop.
L
class User{
int id;
String username, email, nama_lengkap;
User({this.id, this.username, this.email, this.nama_lengkap});
factory User.fromJson(Map<String, dynamic> json){
return User(
id: json['id'],
username: json['username'],
email: json['email'],
nama_lengkap: json['nama_lengkap'],
);
}
}
import 'package:flutter_auth/models/User.dart';
import 'api.dart';
import 'dart:convert';
class UserService{
static String baseUrl = "tbluser";
static Future<List<User>> getUser() async {
final response = await Network().getData(baseUrl);
List<User> list = parseResponse(response.body);
return list;
}
static List<User> parseResponse(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<User>((json) => User.fromJson(json)).toList();
}
}
import 'package:flutter/material.dart';
import 'package:flutter_auth/models/User.dart';
import 'package:flutter_auth/network/UserService.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_auth/network/api.dart';
import 'package:flutter_auth/screens/Menu/userdetail.dart';
import 'dart:convert';
class About extends StatefulWidget {
@override
_AboutState createState() => _AboutState();
}
class _AboutState extends State<About> {
String nama_lengkap = '';
List<User> _user = [];
@override
void initState() {
super.initState();
_loadUserData();
_getUser();
}
_loadUserData() async {
SharedPreferences localStorage = await SharedPreferences.getInstance();
var user = jsonDecode(localStorage.getString('user'));
if (user != null) {
setState(() {
nama_lengkap = user['nama_lengkap'];
});
}
}
_getUser() {
UserService.getUser().then((user) {
if (mounted) {
setState(() {
_user = user;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Home'),
backgroundColor: Color(0xff151515),
automaticallyImplyLeading: false,
),
body: SafeArea(
child: Container(
padding: EdgeInsets.all(15),
child: ListView(
children: [
SizedBox(
height: 24,
),
Text(
"User Data",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
SizedBox(
height: 12,
),
users(),
],
),
),
),
);
}
Table users() {
return Table(
border: TableBorder(
horizontalInside: BorderSide(
width: 1,
color: Colors.grey,
),
),
children: [
TableRow(children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 12),
child: Text(
"ID",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 12),
child: Text(
"NAMA",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 12),
child: Text(
"AKSI",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
),
),
),
]),
for (User user in _user)
TableRow(children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 12),
child: Text(
"${user.id}",
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 12),
child: Text(
"${user.nama_lengkap}",
),
),
FlatButton(
child: Icon(
Icons.chevron_right,
color: Colors.blue,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailUser(
user: user,
),
),
);
},
),
]),
],
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_auth/models/User.dart';
class DetailUser extends StatefulWidget{
User user;
DetailUser({this.user});
@override
_DetailUserState createState() => _DetailUserState(this.user);
}
class _DetailUserState extends State<DetailUser>{
final User user;
_DetailUserState(this.user);
final _scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState(){
super.initState();
}
@override
Widget build(BuildContext context){
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Detail User'),
backgroundColor: Color(0xff151515),
),
body: SafeArea(
child: SingleChildScrollView(
padding: EdgeInsets.all(24),
child: Card(
elevation: 4.0,
color: Colors.blue[700],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
child: Padding(
padding: EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"${user.id}",
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.w600,
),
),
],
),
SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"${user.username}",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
),
),
],
),
SizedBox(height: 12),
Row(
children: [
Text(
"${user.email}",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400,
),
)],
),
SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Nama Lengkap: ",
style: TextStyle(
fontSize: 16,
),
),
Text(
"${user.nama_lengkap}",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
)
],
),
],
),
),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment