Skip to content
Permalink
Browse files

Tutorial Crud dengan Flutter Bagian 4

  • Loading branch information
bobykurniawan11 committed Aug 24, 2019
1 parent d2917ec commit c69313a674a47ad5e74701d13ff7d9ebb3dc12bb
Showing with 363 additions and 7 deletions.
  1. +358 −1 lib/MyList.dart
  2. +5 −6 lib/main.dart
@@ -3,7 +3,11 @@ import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';

import 'main.dart';

class MyList extends StatefulWidget {
@override
@@ -56,7 +60,7 @@ class _MyListState extends State<MyList> {

Future<List<ListPegawai>> fetchPegawai() async {
final response =
await http.post("http://192.168.1.9:84/tutorial_api/tampil.php");
await http.post("http://192.168.1.13:84/tutorial_api/tampil.php");
print(response.statusCode);
if (response.statusCode == 200) {
return compute(parsePegawai, response.body);
@@ -114,6 +118,7 @@ class MyListPopulate extends StatefulWidget {
@override
_MyListPopulateState createState() => _MyListPopulateState();
}

class _MyListPopulateState extends State<MyListPopulate> {
@override
void initState() {
@@ -128,6 +133,35 @@ class _MyListPopulateState extends State<MyListPopulate> {
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Action'),
actions: <Widget>[
FlatButton(
child: Text('Delete'),
onPressed: () {
deleteData(widget.listPegawai[index].userId);
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('Edit'),
onPressed: () {
Navigator.of(context).pop();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => EditPage(dataedit:widget.listPegawai[index])),
);
},
),
],
);
},
);
},
child: Card(
child: Container(
width: MediaQuery.of(context).size.width * 0.50,
@@ -146,4 +180,327 @@ class _MyListPopulateState extends State<MyListPopulate> {
},
);
}

deleteData(String userId) async {
try {
final response = await http
.post("http://192.168.1.13:84/tutorial_api/delete.php", body: {
"userId": userId,
});

if (response.statusCode < 200 || response.statusCode > 300) {
print(response.statusCode);
throw new Exception('Failed to insert data');
} else {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text("Berhasil"),
);
});
}
} catch (e) {
print(e.toString());
return null;
}
setState(() {
widget.listPegawai
.removeWhere((listpegawai) => listpegawai.userId == userId);
});
}
}

class EditPage extends StatefulWidget {
dynamic dataedit;
EditPage({Key key, this.title,this.dataedit}) : super(key: key);
final String title;

@override
_EditPageState createState() => _EditPageState();
}
class _EditPageState extends State<EditPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

TextEditingController namaLengkapController = new TextEditingController();
TextEditingController alamatController = new TextEditingController();
TextEditingController tanggalLahirController = new TextEditingController();
TextEditingController notelpController = new TextEditingController();

String _format = 'yyyy-MMMM-dd';
DateTime _dateTime;
DateTimePickerLocale _locale = DateTimePickerLocale.en_us;

@override
void initState() {
namaLengkapController.text = widget.dataedit.namaLengkap;
alamatController.text = widget.dataedit.alamatLengkap;
notelpController.text = widget.dataedit.namaLengkap;
tanggalLahirController.text = DateFormat('yyyy-MM-dd').format(widget.dataedit.tanggalLahir);
_dateTime =widget.dataedit.tanggalLahir;
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Edit Data"),
),
body: Form(
key: _formKey,
child: Container(
height: MediaQuery.of(context).size.height,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Divider(
height: 50.0,
color: Colors.white,
),
Container(
width: MediaQuery.of(context).size.width,
margin:
const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),
alignment: Alignment.center,
padding: const EdgeInsets.only(left: 0.0, right: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: new TextFormField(
controller: namaLengkapController,
textInputAction: TextInputAction.next,
decoration: new InputDecoration(
labelText: "Nama Lengkap",
filled: true,
hintStyle: new TextStyle(color: Colors.grey[800]),
fillColor: Colors.white70),
),
),
],
),
),
Divider(
height: 14.0,
color: Colors.white,
),
Container(
width: MediaQuery.of(context).size.width,
margin:
const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),
alignment: Alignment.center,
padding: const EdgeInsets.only(left: 0.0, right: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: new TextFormField(
controller: notelpController,
textInputAction: TextInputAction.next,
onFieldSubmitted: (v) {},
decoration: new InputDecoration(
labelText: "No Telp",
filled: true,
hintStyle: new TextStyle(color: Colors.grey[800]),
fillColor: Colors.white70),
),
),
],
),
),
Divider(
height: 14.0,
color: Colors.white,
),
Container(
width: MediaQuery.of(context).size.width,
margin:
const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),
alignment: Alignment.center,
padding: const EdgeInsets.only(left: 0.0, right: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: InkWell(
onTap: () {
_showDatePicker();
},
child: IgnorePointer(
child: TextFormField(
controller: tanggalLahirController,
textInputAction: TextInputAction.next,
onFieldSubmitted: (v) {},
decoration: new InputDecoration(
labelText: "Tanggal Lahir",
filled: true,
hintStyle:
new TextStyle(color: Colors.grey[800]),
fillColor: Colors.white70),
),
),
),
),
],
),
),
Divider(
height: 14.0,
color: Colors.white,
),
Container(
width: MediaQuery.of(context).size.width,
margin:
const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),
alignment: Alignment.center,
padding: const EdgeInsets.only(left: 0.0, right: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: new TextFormField(
controller: alamatController,
textInputAction: TextInputAction.next,
onFieldSubmitted: (v) {},
decoration: new InputDecoration(
labelText: "Alamat",
filled: true,
hintStyle: new TextStyle(color: Colors.grey[800]),
fillColor: Colors.white70),
),
),
],
),
),
Divider(
height: 14.0,
color: Colors.white,
),
Container(
width: MediaQuery.of(context).size.width,
margin:
const EdgeInsets.only(left: 30.0, right: 30.0, top: 20.0),
alignment: Alignment.center,
child: new Row(
children: <Widget>[
new Expanded(
child: new FlatButton(
color: Colors.red,
onPressed: () {
prosesUbah();
},
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
child: new Container(
padding: const EdgeInsets.symmetric(
vertical: 20.0,
horizontal: 20.0,
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(
child: Text(
"Ubah Data",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
],
),
),
),
),
],
),
),
],
),
),
),
),
);
}

void _showDatePicker() {
DatePicker.showDatePicker(
context,
pickerTheme: DateTimePickerTheme(
confirm: Text('Ok', style: TextStyle(color: Colors.cyan)),
cancel: Text('Close', style: TextStyle(color: Colors.red)),
),
initialDateTime: _dateTime,
dateFormat: _format,
locale: _locale,
onClose: () => print("----- onClose -----"),
onCancel: () => print('onCancel'),
onChange: (dateTime, List<int> index) {
setState(() {
_dateTime = dateTime;
});
},
onConfirm: (dateTime, List<int> index) {
setState(() {
_dateTime = dateTime;
tanggalLahirController.text =
DateFormat('yyyy-MM-dd').format(_dateTime);
});
},
);
}

prosesUbah() async {
String _namaInput = namaLengkapController.text;
String _noTelp = notelpController.text;
String _alamat = alamatController.text;
String _tanggalLahir = tanggalLahirController.text;
try {
final response = await http
.post("http://192.168.1.13:84/tutorial_api/edit.php/", body: {
"userId":widget.dataedit.userId,
"namaLengkap": _namaInput,
"notelp": _noTelp,
"alamatLengkap": _alamat,
"tanggallahir": _tanggalLahir
});

if (response.statusCode < 200 || response.statusCode > 300) {
print(response.statusCode);
throw new Exception('Failed to insert data');
} else {
showDialog(
barrierDismissible: true,
context: context,

builder: (BuildContext context) {
return AlertDialog(
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyHomePage()),
);
},
),
],
content: Text("Berhasil"),
);
});
}
} catch (e) {
print(e.toString());
return null;
}
}
}

0 comments on commit c69313a

Please sign in to comment.
You can’t perform that action at this time.