Created
          August 13, 2020 14:28 
        
      - 
      
- 
        Save tiagolpadua/3057960f99499ca4d7c3a2f49fa99bc3 to your computer and use it in GitHub Desktop. 
    Snackbar challange
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import 'package:bytebank/database/app_database.dart'; | |
| import 'package:bytebank/database/contact_dao.dart'; | |
| import 'package:bytebank/models/contact.dart'; | |
| import 'package:flutter/material.dart'; | |
| class ContactForm extends StatefulWidget { | |
| @override | |
| _ContactFormState createState() => _ContactFormState(); | |
| } | |
| class _ContactFormState extends State<ContactForm> { | |
| final TextEditingController _nameController = TextEditingController(); | |
| final TextEditingController _accountNumberController = | |
| TextEditingController(); | |
| final ContactDao _dao = ContactDao(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text('New Contact'), | |
| ), | |
| body: Padding( | |
| padding: const EdgeInsets.all(16.0), | |
| child: Column( | |
| children: <Widget>[ | |
| TextField( | |
| controller: _nameController, | |
| decoration: InputDecoration( | |
| labelText: 'Full Name', | |
| ), | |
| style: TextStyle(fontSize: 24.0), | |
| ), | |
| Padding( | |
| padding: const EdgeInsets.only(top: 8.0), | |
| child: TextField( | |
| controller: _accountNumberController, | |
| decoration: InputDecoration( | |
| labelText: 'Account Number', | |
| ), | |
| style: TextStyle(fontSize: 24.0), | |
| keyboardType: TextInputType.number, | |
| ), | |
| ), | |
| Padding( | |
| padding: const EdgeInsets.only(top: 16.0), | |
| child: SizedBox( | |
| width: double.maxFinite, | |
| // New code here | |
| child: CreateButton( | |
| _nameController, _accountNumberController, _dao), | |
| ), | |
| ) | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| // New code here | |
| class CreateButton extends StatelessWidget { | |
| final TextEditingController _nameController; | |
| final TextEditingController _accountNumberController; | |
| final ContactDao _dao; | |
| CreateButton( | |
| this._nameController, | |
| this._accountNumberController, | |
| this._dao, | |
| ); | |
| @override | |
| Widget build(BuildContext context) { | |
| return RaisedButton( | |
| child: Text('Create'), | |
| onPressed: () { | |
| final String name = _nameController.text; | |
| final int accountNumber = int.tryParse(_accountNumberController.text); | |
| if (name != null && name.length != 0 && accountNumber != null) { | |
| final Contact newContact = Contact(0, name, accountNumber); | |
| _dao.save(newContact).then((id) => Navigator.pop(context)); | |
| } else { | |
| final snackBar = SnackBar(content: Text('Invalid values...')); | |
| Scaffold.of(context).showSnackBar(snackBar); | |
| } | |
| }, | |
| ); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment