[dart] jn

Viewer

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_contact_app/contact_service.dart';
  3.  
  4. class AddForm extends StatefulWidget {
  5.   const AddForm({super.key});
  6.   @override
  7.   State<AddForm> createState() => _AddFormState();
  8. }
  9.  
  10. class _AddFormState extends State<AddForm> {
  11.   //inisialisasi variabel
  12.   final _formKey = GlobalKey<FormState>();
  13.   TextEditingController name = TextEditingController();
  14.   TextEditingController email = TextEditingController();
  15.   TextEditingController gender = TextEditingController();
  16.   TextEditingController phone = TextEditingController();
  17.   TextEditingController address = TextEditingController();
  18.   TextEditingController hobby = TextEditingController();
  19.  
  20.   //inisialisasi class
  21.   ContactService contactService = ContactService();
  22.  
  23.   @override
  24.   Widget build(BuildContext context) {
  25.     return Scaffold(
  26.       appBar: AppBar(
  27.         title: Text('Form Tambah Data'),
  28.       ),
  29.       body: SingleChildScrollView(
  30.         padding: EdgeInsets.all(16),
  31.         child: Form(
  32.           // key adalah kunci unik untuk mengidentifikasi suatu form
  33.           // di bawah key ini tambahkan widget sesuai selera kalian
  34.           key: _formKey,
  35.           child: Column(
  36.             crossAxisAlignment: CrossAxisAlignment.stretch,
  37.             children: [
  38.               TextFormField(
  39.                 decoration: InputDecoration(labelText: 'Nama'),
  40.                 controller: name,
  41.                 validator: (value) {
  42.                   if (value!.isEmpty) {
  43.                     return 'Masukan Nama';
  44.                   }
  45.                   return null;
  46.                 },
  47.               ),
  48.               SizedBox(
  49.                 height: 15,
  50.               ),
  51.               //bentuk dropdown select dengan dua pilihan
  52.               DropdownButtonFormField<String>(
  53.                 items: ['Laki-Laki', 'Perempuan'].map((String option) {
  54.                   return DropdownMenuItem<String>(
  55.                     value: option,
  56.                     child: Text(option),
  57.                   );
  58.                 }).toList(),
  59.                 onChanged: (String? newValue) {
  60.                   setState(() {
  61.                     gender.text = newValue!;
  62.                   });
  63.                 },
  64.                 decoration: InputDecoration(
  65.                   labelText: 'Jenis Kelamin',
  66.                 ),
  67.               ),
  68.               SizedBox(
  69.                 height: 15,
  70.               ),
  71.               TextFormField(
  72.                 decoration: InputDecoration(labelText: 'Email'),
  73.                 controller: email,
  74.                 keyboardType: TextInputType.emailAddress,
  75.                 validator: (value) {
  76.                   if (value!.isEmpty) {
  77.                     return 'Masukan Email';
  78.                   }
  79.                   return null;
  80.                 },
  81.               ),
  82.               SizedBox(
  83.                 height: 15,
  84.               ),
  85.               TextFormField(
  86.                 decoration: InputDecoration(labelText: ' Nomor Handphone'),
  87.                 controller: phone,
  88.                 keyboardType: TextInputType.phone,
  89.                 validator: (value) {
  90.                   if (value!.isEmpty) {
  91.                     return 'Masukan Nomor Handphone';
  92.                   }
  93.                   return null;
  94.                 },
  95.               ),
  96.               SizedBox(
  97.                 height: 15,
  98.               ),
  99.               TextFormField(
  100.                 decoration: InputDecoration(labelText: 'Alamat'),
  101.                 controller: address,
  102.                 validator: (value) {
  103.                   if (value!.isEmpty) {
  104.                     return 'Masukan Alamat';
  105.                   }
  106.                   return null;
  107.                 },
  108.               ),
  109.               SizedBox(
  110.                 height: 15,
  111.               ),
  112.               TextFormField(
  113.                 decoration: InputDecoration(labelText: 'Hobi'),
  114.                 controller: hobby,
  115.                 validator: (value) {
  116.                   if (value!.isEmpty) {
  117.                     return 'Masukan Hobi';
  118.                   }
  119.                   return null;
  120.                 },
  121.               ),
  122.               SizedBox(
  123.                 height: 15,
  124.               ),
  125.               ElevatedButton(
  126.                 onPressed: () {
  127.                   //jika validasi berhasil maka jalankan perintah di bawahnya
  128.                   //jika tidak maka tampilkan pesan kesalahan di tiap formnya
  129.                   if (_formKey.currentState!.validate()) {
  130.                     contactService.saveData(
  131.                       name.text,
  132.                       gender.text,
  133.                       email.text,
  134.                       phone.text,
  135.                       address.text,
  136.                       hobby.text,
  137.                     );
  138.                     Navigator.of(context).pushNamedAndRemoveUntil(
  139.                       '/',
  140.                       (Route<dynamic> route) =>
  141.                           false, // Ini akan menghapus semua halaman sebelumnya dari tumpukan navigasi.
  142.                     );
  143.                   }
  144.                 },
  145.                 child: Text('Add Data'),
  146.               )
  147.             ],
  148.           ),
  149.         ),
  150.       ),
  151.     );
  152.   }
  153. }

Editor

You can edit this paste and save as new:


File Description
  • jn
  • Paste Code
  • 10 Dec-2023
  • 5.16 Kb
You can Share it: