0% found this document useful (0 votes)
59 views

CRUD Flutter Aplication With MySQL (Part 1 of 2)

Uploaded by

Jusuf Boeky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

CRUD Flutter Aplication With MySQL (Part 1 of 2)

Uploaded by

Jusuf Boeky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. halaman_produk.

dart

import 'dart:convert';

import 'package:app_produk/tambah_produk.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class HalamanProduk extends StatefulWidget {


const HalamanProduk({super.key});

@override
State<HalamanProduk> createState() => _HalamanProdukState();
}

class _HalamanProdukState extends State<HalamanProduk> {


List _listdata = [];
bool _loading = true;

Future _getdata() async {


try {
final respon =
await
http.get(Uri.parse('http://192.168.0.120/api_produk/read.php'));
if (respon.statusCode == 200) {
final data = jsonDecode(respon.body);
setState(() {
_listdata = data;
_loading = false;
});
}
} catch (e) {
print(e);
}
}

void initState() {
_getdata();
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Halaman Produk'),
backgroundColor: Colors.deepOrange,
),
body: _loading
? Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: _listdata.length,
itemBuilder: ((context, index) {
return Card(
child: ListTile(
title: Text(_listdata[index]['nama_produk']),
subtitle:
Text(_listdata[index]['harga_produk']),
),
);
}),
),
floatingActionButton: FloatingActionButton(
child: Text(
'+',
style: TextStyle(fontSize: 24),
),
backgroundColor: Colors.deepOrange,
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) =>
TambahProduk()));
}),
);
}
}
2. tambah_produk.dart

import 'package:app_produk/halaman_produk.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class TambahProduk extends StatefulWidget {


const TambahProduk({super.key});

@override
State<TambahProduk> createState() => _TambahProdukState();
}

class _TambahProdukState extends State<TambahProduk> {


final formKey = GlobalKey<FormState>();
TextEditingController nama_produk = TextEditingController();
TextEditingController harga_produk = TextEditingController();

Future _simpan() async {


final respon = await http

.post(Uri.parse('http://192.168.0.120/api_produk/create.php'),
body: {
'nama_produk': nama_produk.text,
'harga_produk': harga_produk.text,
});
if (respon.statusCode == 200) {
return true;
}
return false;
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tambah Produk'),
backgroundColor: Colors.deepOrange,
),
body: Form(
key: formKey,
child: Container(
padding: EdgeInsets.all(20),
child: Column(
children: [
TextFormField(
controller: nama_produk,
decoration: InputDecoration(
hintText: 'Nama Produk',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
)),
validator: (value) {
if (value!.isEmpty) {
return "Nama produk tidak boleh kosong!";
}
},
),
SizedBox(height: 10),
TextFormField(
controller: harga_produk,
decoration: InputDecoration(
hintText: 'Harga Produk',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
)),
validator: (value) {
if (value!.isEmpty) {
return "Harga produk tidak boleh kosong!";
}
},
),
SizedBox(height: 10),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.deepOrange,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
)),
onPressed: () {
if (formKey.currentState!.validate()) {
_simpan().then((value) {
if (value) {
final snackBar = SnackBar(
content: const Text('Data berhasil
disimpan'),
);
ScaffoldMessenger.of(context)
.showSnackBar(snackBar);
} else {
final snackBar = SnackBar(
content: const Text('Data gagal
disimpan'),
);
ScaffoldMessenger.of(context)
.showSnackBar(snackBar);
}
});
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: ((context) =>
HalamanProduk())),
(route) => false);
}
},
child: Text('Simpan'))
],
),
)),
);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy