[dart] a

Viewer

  1. import 'package:flutter/material.dart';
  2. import 'package:ocw/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.         backgroundColor: Colors.teal,
  29.       ),
  30.       body: SingleChildScrollView(
  31.         padding: EdgeInsets.all(16),
  32.         child: Form(
  33.           key: _formKey,
  34.           child: Column(
  35.             crossAxisAlignment: CrossAxisAlignment.stretch,
  36.             children: [
  37.               _buildFormField(
  38.                 label: 'Nama',
  39.                 controller: name,
  40.                 icon: Icons.person,
  41.               ),
  42.               SizedBox(height: 15),
  43.               _buildDropdownFormField(
  44.                 label: 'Jenis Kelamin',
  45.                 items: ['Laki-Laki', 'Perempuan'],
  46.                 onChanged: (String? newValue) {
  47.                   setState(() {
  48.                     gender.text = newValue!;
  49.                   });
  50.                 },
  51.                 icon: Icons.people,
  52.               ),
  53.               SizedBox(height: 15),
  54.               _buildFormField(
  55.                 label: 'Email',
  56.                 controller: email,
  57.                 keyboardType: TextInputType.emailAddress,
  58.                 icon: Icons.email,
  59.               ),
  60.               SizedBox(height: 15),
  61.               _buildFormField(
  62.                 label: 'Nomor Handphone',
  63.                 controller: phone,
  64.                 keyboardType: TextInputType.phone,
  65.                 icon: Icons.phone,
  66.               ),
  67.               SizedBox(height: 15),
  68.               _buildFormField(
  69.                 label: 'Alamat',
  70.                 controller: address,
  71.                 icon: Icons.location_on,
  72.               ),
  73.               SizedBox(height: 15),
  74.               _buildFormField(
  75.                 label: 'Hobi',
  76.                 controller: hobby,
  77.                 icon: Icons.favorite,
  78.               ),
  79.               ElevatedButton(
  80.                 onPressed: () {
  81.                   if (_formKey.currentState!.validate()) {
  82.                     contactService.saveData(
  83.                       name.text,
  84.                       gender.text,
  85.                       email.text,
  86.                       phone.text,
  87.                       address.text,
  88.                       hobby.text,
  89.                     );
  90.                     Navigator.of(context).pushNamedAndRemoveUntil(
  91.                       '/',
  92.                       (Route<dynamic> route) => false,
  93.                     );
  94.                   }
  95.                 },
  96.                 child: Text('Tambah Data'),
  97.                 style: ElevatedButton.styleFrom(
  98.                   primary: Colors.teal[400],
  99.                 ),
  100.               ),
  101.             ],
  102.           ),
  103.         ),
  104.       ),
  105.     );
  106.   }
  107.  
  108.   Widget _buildFormField({
  109.     required String label,
  110.     required TextEditingController controller,
  111.     TextInputType keyboardType = TextInputType.text,
  112.     required IconData icon,
  113.   }) {
  114.     return TextFormField(
  115.       decoration: InputDecoration(
  116.         labelText: label,
  117.         icon: Icon(icon),
  118.       ),
  119.       controller: controller,
  120.       keyboardType: keyboardType,
  121.       validator: (value) {
  122.         if (value!.isEmpty) {
  123.           return 'Masukkan $label';
  124.         }
  125.         return null;
  126.       },
  127.     );
  128.   }
  129.  
  130.   Widget _buildDropdownFormField({
  131.     required String label,
  132.     required List<String> items,
  133.     required Function(String?) onChanged,
  134.     required IconData icon,
  135.   }) {
  136.     return DropdownButtonFormField<String>(
  137.       items: items.map((String option) {
  138.         return DropdownMenuItem<String>(
  139.           value: option,
  140.           child: Text(option),
  141.         );
  142.       }).toList(),
  143.       onChanged: onChanged,
  144.       decoration: InputDecoration(
  145.         labelText: label,
  146.         icon: Icon(icon),
  147.       ),
  148.     );
  149.   }
  150. }
  151.  

Editor

You can edit this paste and save as new:


File Description
  • a
  • Paste Code
  • 10 Dec-2023
  • 4.49 Kb
You can Share it: