[dart] edit

Viewer

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_contact_app/contact_service.dart';
  3.  
  4. class EditForm extends StatefulWidget {
  5.   final Map<dynamic, dynamic> data;
  6.  
  7.   EditForm({required this.data});
  8.  
  9.   @override
  10.   State<EditForm> createState() => _EditFormState();
  11. }
  12.  
  13. class _EditFormState extends State<EditForm> {
  14.   final _formKey = GlobalKey<FormState>();
  15.   TextEditingController name = TextEditingController();
  16.   TextEditingController gender = TextEditingController();
  17.   TextEditingController email = TextEditingController();
  18.   TextEditingController phone = TextEditingController();
  19.   TextEditingController address = TextEditingController();
  20.   TextEditingController hobby = TextEditingController();
  21.  
  22.   ContactService contactService = ContactService();
  23.  
  24.   @override
  25.   void initState() {
  26.     super.initState();
  27.     initializeControllers();
  28.   }
  29.  
  30.   void initializeControllers() {
  31.     name.text = widget.data['name']!;
  32.     gender.text = widget.data['gender']!;
  33.     email.text = widget.data['email']!;
  34.     phone.text = widget.data['phone']!;
  35.     address.text = widget.data['address']!;
  36.     hobby.text = widget.data['hobby']!;
  37.   }
  38.  
  39.   @override
  40.   Widget build(BuildContext context) {
  41.     return Scaffold(
  42.       appBar: AppBar(
  43.         title: Text('Form Update Data'),
  44.       ),
  45.       body: Padding(
  46.         padding: EdgeInsets.all(16),
  47.         child: Form(
  48.           key: _formKey,
  49.           child: Column(
  50.             crossAxisAlignment: CrossAxisAlignment.stretch,
  51.             children: [
  52.               TextFormField(
  53.                 decoration: InputDecoration(labelText: 'Nama'),
  54.                 controller: name,
  55.                 validator: (value) {
  56.                   if (value!.isEmpty) {
  57.                     return 'Masukkan Nama';
  58.                   }
  59.                   return null;
  60.                 },
  61.               ),
  62.               SizedBox(height: 15),
  63.               DropdownButtonFormField<String>(
  64.                 value: gender.text,
  65.                 items: ['Laki-Laki', 'Perempuan'].map((String option) {
  66.                   return DropdownMenuItem<String>(
  67.                     value: option,
  68.                     child: Text(option),
  69.                   );
  70.                 }).toList(),
  71.                 onChanged: (String? newValue) {
  72.                   setState(() {
  73.                     gender.text = newValue!;
  74.                   });
  75.                 },
  76.                 decoration: InputDecoration(
  77.                   labelText: 'Jenis Kelamin',
  78.                 ),
  79.               ),
  80.               SizedBox(height: 15),
  81.               TextFormField(
  82.                 decoration: InputDecoration(labelText: 'Email'),
  83.                 controller: email,
  84.                 keyboardType: TextInputType.emailAddress,
  85.                 validator: (value) {
  86.                   if (value!.isEmpty) {
  87.                     return 'Masukkan Email';
  88.                   }
  89.                   return null;
  90.                 },
  91.               ),
  92.               SizedBox(height: 15),
  93.               TextFormField(
  94.                 decoration: InputDecoration(labelText: 'Nomor Handphone'),
  95.                 controller: phone,
  96.                 keyboardType: TextInputType.phone,
  97.                 validator: (value) {
  98.                   if (value!.isEmpty) {
  99.                     return 'Masukkan Nomor Handphone';
  100.                   }
  101.                   return null;
  102.                 },
  103.               ),
  104.               SizedBox(height: 15),
  105.               TextFormField(
  106.                 decoration: InputDecoration(labelText: 'Alamat'),
  107.                 controller: address,
  108.                 validator: (value) {
  109.                   if (value!.isEmpty) {
  110.                     return 'Masukkan Alamat';
  111.                   }
  112.                   return null;
  113.                 },
  114.               ),
  115.               SizedBox(height: 15),
  116.               TextFormField(
  117.                 decoration: InputDecoration(labelText: 'Hobi'),
  118.                 controller: hobby,
  119.                 validator: (value) {
  120.                   if (value!.isEmpty) {
  121.                     return 'Masukkan Hobi';
  122.                   }
  123.                   return null;
  124.                 },
  125.               ),
  126.               SizedBox(height: 15),
  127.               ElevatedButton(
  128.                 onPressed: () {
  129.                   if (_formKey.currentState!.validate()) {
  130.                     contactService.updateData(
  131.                       name.text,
  132.                       gender.text,
  133.                       email.text,
  134.                       phone.text,
  135.                       address.text,
  136.                       hobby.text,
  137.                       widget.data['id'],
  138.                     );
  139.                     Navigator.of(context).pushNamedAndRemoveUntil(
  140.                       '/home',
  141.                       (Route<dynamic> route) => false,
  142.                     );
  143.                   }
  144.                 },
  145.                 child: Text('Update Data'),
  146.               )
  147.             ],
  148.           ),
  149.         ),
  150.       ),
  151.     );
  152.   }
  153. }
  154.  

Editor

You can edit this paste and save as new:


File Description
  • edit
  • Paste Code
  • 10 Dec-2023
  • 4.96 Kb
You can Share it: