Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'package:agenda_contatos/helpers/contact_helper.dart';
- import 'package:flutter/material.dart';
- class ContactPage extends StatefulWidget {
- @override
- _ContactPageState createState() => _ContactPageState();
- final Contact contact;
- ContactPage({this.contact});
- }
- class _ContactPageState extends State<ContactPage> {
- Contact _editedContact;
- bool _userEdited = false;
- final _nameController = TextEditingController();
- final _emailController = TextEditingController();
- final _phoneController = TextEditingController();
- final _nameFocus = FocusNode();
- @override
- void initState() {
- super.initState();
- if (widget.contact == null) {
- _editedContact = Contact();
- } else {
- _editedContact = Contact.fromMap(widget.contact.toMap());
- _nameController.text = _editedContact.name;
- _emailController.text = _editedContact.email;
- _phoneController.text = _editedContact.phone;
- }
- }
- @override
- Widget build(BuildContext context) {
- ImageProvider<dynamic> contactImg;
- if (_editedContact.img == null ||
- _editedContact.img.length == 0 ||
- !(File(_editedContact.img).existsSync())) {
- contactImg = AssetImage("images/default_contact_img.png");
- } else {
- contactImg = FileImage(File(_editedContact.img));
- }
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Colors.red,
- title: Text(_editedContact.name ?? "Novo Contato"),
- centerTitle: true,
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () {
- if (_editedContact.name != null && _editedContact.name.isNotEmpty) {
- Navigator.pop(context, _editedContact);
- } else {
- FocusScope.of(context).requestFocus(_nameFocus);
- }
- },
- child: Icon(Icons.save),
- backgroundColor: Colors.red,
- ),
- body: SingleChildScrollView(
- padding: EdgeInsets.all(10.0),
- child: Column(
- children: <Widget>[
- GestureDetector(
- child: Container(
- width: 140.0,
- height: 140.0,
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- image: DecorationImage(image: contactImg),
- ),
- ),
- ),
- TextField(
- decoration: InputDecoration(labelText: "Nome"),
- focusNode: _nameFocus,
- onChanged: (text) {
- _userEdited = true;
- setState(() {
- _editedContact.name = text;
- });
- },
- controller: _nameController,
- ),
- TextField(
- decoration: InputDecoration(labelText: "Email"),
- onChanged: (text) {
- _userEdited = true;
- _editedContact.email = text;
- },
- keyboardType: TextInputType.emailAddress,
- controller: _emailController,
- ),
- TextField(
- decoration: InputDecoration(labelText: "Telefone"),
- onChanged: (text) {
- _userEdited = true;
- _editedContact.phone = text;
- },
- keyboardType: TextInputType.phone,
- controller: _phoneController,
- ),
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement