[dart] addform

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.  
  7.   @override
  8.   State<AddForm> createState() => _AddFormState();
  9. }
  10.  
  11. class _AddFormState extends State<AddForm> {
  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.   ContactService contactService = ContactService();
  21.  
  22.   @override
  23.   Widget build(BuildContext context) {
  24.     return Scaffold(
  25.       appBar: AppBar(
  26.         title: Text('Form Tambah Data'),
  27.       ),
  28.       body: Padding(
  29.         padding: EdgeInsets.all(16),
  30.         child: Form(
  31.           key: _formKey,
  32.           child: Column(
  33.             crossAxisAlignment: CrossAxisAlignment.stretch,
  34.             children: [
  35.               TextFormField(
  36.                 decoration: InputDecoration(labelText: 'Nama'),
  37.                 controller: name,
  38.                 validator: (value) {
  39.                   if (value!.isEmpty) {
  40.                     return 'Masukkan Nama';
  41.                   }
  42.                   return null;
  43.                 },
  44.               ),
  45.               SizedBox(height: 15),
  46.               DropdownButtonFormField<String>(
  47.                 items: ['Laki-Laki', 'Perempuan'].map((String option) {
  48.                   return DropdownMenuItem<String>(
  49.                     value: option,
  50.                     child: Text(option),
  51.                   );
  52.                 }).toList(),
  53.                 onChanged: (String? newValue) {
  54.                   setState(() {
  55.                     gender.text = newValue!;
  56.                   });
  57.                 },
  58.                 decoration: InputDecoration(
  59.                   labelText: 'Jenis Kelamin',
  60.                 ),
  61.               ),
  62.               SizedBox(height: 15),
  63.               TextFormField(
  64.                 decoration: InputDecoration(labelText: 'Email'),
  65.                 controller: email,
  66.                 keyboardType: TextInputType.emailAddress,
  67.                 validator: (value) {
  68.                   if (value!.isEmpty) {
  69.                     return 'Masukkan Email';
  70.                   }
  71.                   return null;
  72.                 },
  73.               ),
  74.               SizedBox(height: 15),
  75.               TextFormField(
  76.                 decoration: InputDecoration(labelText: 'Nomor Handphone'),
  77.                 controller: phone,
  78.                 keyboardType: TextInputType.phone,
  79.                 validator: (value) {
  80.                   if (value!.isEmpty) {
  81.                     return 'Masukkan Nomor Handphone';
  82.                   }
  83.                   return null;
  84.                 },
  85.               ),
  86.               SizedBox(height: 15),
  87.               TextFormField(
  88.                 decoration: InputDecoration(labelText: 'Alamat'),
  89.                 controller: address,
  90.                 validator: (value) {
  91.                   if (value!.isEmpty) {
  92.                     return 'Masukkan Alamat';
  93.                   }
  94.                   return null;
  95.                 },
  96.               ),
  97.               SizedBox(height: 15),
  98.               TextFormField(
  99.                 decoration: InputDecoration(labelText: 'Hobi'),
  100.                 controller: hobby,
  101.                 validator: (value) {
  102.                   if (value!.isEmpty) {
  103.                     return 'Masukkan Hobi';
  104.                   }
  105.                   return null;
  106.                 },
  107.               ),
  108.               SizedBox(height: 15),
  109.               ElevatedButton(
  110.                 onPressed: () {
  111.                   if (_formKey.currentState!.validate()) {
  112.                     contactService.saveData(
  113.                       name.text,
  114.                       gender.text,
  115.                       email.text,
  116.                       phone.text,
  117.                       address.text,
  118.                       hobby.text,
  119.                     );
  120.                     Navigator.of(context).pushNamedAndRemoveUntil(
  121.                       '/home',
  122.                       (Route<dynamic> route) => false,
  123.                     );
  124.                   }
  125.                 },
  126.                 child: Text('Tambah Data'),
  127.               )
  128.             ],
  129.           ),
  130.         ),
  131.       ),
  132.     );
  133.   }
  134. }
  135.  

Editor

You can edit this paste and save as new:


File Description
  • addform
  • Paste Code
  • 10 Dec-2023
  • 4.46 Kb
You can Share it: