[dart] addform
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 AddForm extends StatefulWidget {
- const AddForm({super.key});
- @override
- State<AddForm> createState() => _AddFormState();
- }
- class _AddFormState extends State<AddForm> {
- final _formKey = GlobalKey<FormState>();
- TextEditingController name = TextEditingController();
- TextEditingController email = TextEditingController();
- TextEditingController gender = TextEditingController();
- TextEditingController phone = TextEditingController();
- TextEditingController address = TextEditingController();
- TextEditingController hobby = TextEditingController();
- ContactService contactService = ContactService();
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Form Tambah 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>(
- 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.saveData(
- name.text,
- gender.text,
- email.text,
- phone.text,
- address.text,
- hobby.text,
- );
- Navigator.of(context).pushNamedAndRemoveUntil(
- '/home',
- (Route<dynamic> route) => false,
- );
- }
- },
- child: Text('Tambah Data'),
- )
- ],
- ),
- ),
- ),
- );
- }
- }
Editor
You can edit this paste and save as new: