Advertisement
Guest User

contact_page.dart

a guest
Feb 21st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import 'dart:io';
  2.  
  3. import 'package:agenda_contatos/helpers/contact_helper.dart';
  4. import 'package:flutter/material.dart';
  5.  
  6. class ContactPage extends StatefulWidget {
  7. @override
  8. _ContactPageState createState() => _ContactPageState();
  9.  
  10. final Contact contact;
  11.  
  12. ContactPage({this.contact});
  13. }
  14.  
  15. class _ContactPageState extends State<ContactPage> {
  16. Contact _editedContact;
  17. bool _userEdited = false;
  18. final _nameController = TextEditingController();
  19. final _emailController = TextEditingController();
  20. final _phoneController = TextEditingController();
  21. final _nameFocus = FocusNode();
  22.  
  23. @override
  24. void initState() {
  25. super.initState();
  26. if (widget.contact == null) {
  27. _editedContact = Contact();
  28. } else {
  29. _editedContact = Contact.fromMap(widget.contact.toMap());
  30. _nameController.text = _editedContact.name;
  31. _emailController.text = _editedContact.email;
  32. _phoneController.text = _editedContact.phone;
  33. }
  34. }
  35.  
  36. @override
  37. Widget build(BuildContext context) {
  38. ImageProvider<dynamic> contactImg;
  39. if (_editedContact.img == null ||
  40. _editedContact.img.length == 0 ||
  41. !(File(_editedContact.img).existsSync())) {
  42. contactImg = AssetImage("images/default_contact_img.png");
  43. } else {
  44. contactImg = FileImage(File(_editedContact.img));
  45. }
  46.  
  47. return Scaffold(
  48. appBar: AppBar(
  49. backgroundColor: Colors.red,
  50. title: Text(_editedContact.name ?? "Novo Contato"),
  51. centerTitle: true,
  52. ),
  53. floatingActionButton: FloatingActionButton(
  54. onPressed: () {
  55. if (_editedContact.name != null && _editedContact.name.isNotEmpty) {
  56. Navigator.pop(context, _editedContact);
  57. } else {
  58. FocusScope.of(context).requestFocus(_nameFocus);
  59. }
  60. },
  61. child: Icon(Icons.save),
  62. backgroundColor: Colors.red,
  63. ),
  64. body: SingleChildScrollView(
  65. padding: EdgeInsets.all(10.0),
  66. child: Column(
  67. children: <Widget>[
  68. GestureDetector(
  69. child: Container(
  70. width: 140.0,
  71. height: 140.0,
  72. decoration: BoxDecoration(
  73. shape: BoxShape.circle,
  74. image: DecorationImage(image: contactImg),
  75. ),
  76. ),
  77. ),
  78. TextField(
  79. decoration: InputDecoration(labelText: "Nome"),
  80. focusNode: _nameFocus,
  81. onChanged: (text) {
  82. _userEdited = true;
  83. setState(() {
  84. _editedContact.name = text;
  85. });
  86. },
  87. controller: _nameController,
  88. ),
  89. TextField(
  90. decoration: InputDecoration(labelText: "Email"),
  91. onChanged: (text) {
  92. _userEdited = true;
  93. _editedContact.email = text;
  94. },
  95. keyboardType: TextInputType.emailAddress,
  96. controller: _emailController,
  97. ),
  98. TextField(
  99. decoration: InputDecoration(labelText: "Telefone"),
  100. onChanged: (text) {
  101. _userEdited = true;
  102. _editedContact.phone = text;
  103. },
  104. keyboardType: TextInputType.phone,
  105. controller: _phoneController,
  106. ),
  107. ],
  108. ),
  109. ),
  110. );
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement