[dart] edit
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import 'package:flutter/material.dart';
- import 'package:flutter_contact_app/contact_service.dart';
- class EditForm extends StatefulWidget {
- final Map<dynamic, dynamic> data;
- EditForm({required this.data});
- @override
- State<EditForm> createState() => _EditFormState();
- }
- class _EditFormState extends State<EditForm> {
- final _formKey = GlobalKey<FormState>();
- TextEditingController name = TextEditingController();
- TextEditingController gender = TextEditingController();
- TextEditingController email = TextEditingController();
- TextEditingController phone = TextEditingController();
- TextEditingController address = TextEditingController();
- TextEditingController hobby = TextEditingController();
- ContactService contactService = ContactService();
- @override
- void initState() {
- super.initState();
- initializeControllers();
- }
- void initializeControllers() {
- name.text = widget.data['name']!;
- gender.text = widget.data['gender']!;
- email.text = widget.data['email']!;
- phone.text = widget.data['phone']!;
- address.text = widget.data['address']!;
- hobby.text = widget.data['hobby']!;
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Form Update Data'),
- ),
- body: Padding(
- padding: EdgeInsets.all(16),
- child: Form(
- key: _formKey,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- TextFormField(
- decoration: InputDecoration(labelText: 'Nama'),
- controller: name,
- validator: (value) {
- if (value!.isEmpty) {
- return 'Masukkan Nama';
- }
- return null;
- },
- ),
- SizedBox(height: 15),
- DropdownButtonFormField<String>(
- value: gender.text,
- items: ['Laki-Laki', 'Perempuan'].map((String option) {
- return DropdownMenuItem<String>(
- value: option,
- child: Text(option),
- );
- }).toList(),
- onChanged: (String? newValue) {
- setState(() {
- gender.text = newValue!;
- });
- },
- decoration: InputDecoration(
- labelText: 'Jenis Kelamin',
- ),
- ),
- SizedBox(height: 15),
- TextFormField(
- decoration: InputDecoration(labelText: 'Email'),
- controller: email,
- keyboardType: TextInputType.emailAddress,
- validator: (value) {
- if (value!.isEmpty) {
- return 'Masukkan Email';
- }
- return null;
- },
- ),
- SizedBox(height: 15),
- TextFormField(
- decoration: InputDecoration(labelText: 'Nomor Handphone'),
- controller: phone,
- keyboardType: TextInputType.phone,
- validator: (value) {
- if (value!.isEmpty) {
- return 'Masukkan Nomor Handphone';
- }
- return null;
- },
- ),
- SizedBox(height: 15),
- TextFormField(
- decoration: InputDecoration(labelText: 'Alamat'),
- controller: address,
- validator: (value) {
- if (value!.isEmpty) {
- return 'Masukkan Alamat';
- }
- return null;
- },
- ),
- SizedBox(height: 15),
- TextFormField(
- decoration: InputDecoration(labelText: 'Hobi'),
- controller: hobby,
- validator: (value) {
- if (value!.isEmpty) {
- return 'Masukkan Hobi';
- }
- return null;
- },
- ),
- SizedBox(height: 15),
- ElevatedButton(
- onPressed: () {
- if (_formKey.currentState!.validate()) {
- contactService.updateData(
- name.text,
- gender.text,
- email.text,
- phone.text,
- address.text,
- hobby.text,
- widget.data['id'],
- );
- Navigator.of(context).pushNamedAndRemoveUntil(
- '/home',
- (Route<dynamic> route) => false,
- );
- }
- },
- child: Text('Update Data'),
- )
- ],
- ),
- ),
- ),
- );
- }
- }
Editor
You can edit this paste and save as new: