Guest User

Untitled

a guest
Dec 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() => runApp(new MyApp());
  4.  
  5. class MyApp extends StatelessWidget {
  6. // This widget is the root of your application.
  7. @override
  8. Widget build(BuildContext context) {
  9. return new MaterialApp(
  10. title: 'Contacts',
  11. theme: new ThemeData(
  12. primarySwatch: Colors.blue,
  13. ),
  14. home: new MyHomePage(title: 'Contacts App Flutter'),
  15. );
  16. }
  17. }
  18.  
  19. class MyHomePage extends StatefulWidget {
  20. MyHomePage({Key key, this.title}) : super(key: key);
  21.  
  22. final String title;
  23.  
  24. @override
  25. _MyHomePageState createState() => new _MyHomePageState();
  26. }
  27.  
  28. class _MyHomePageState extends State<MyHomePage> {
  29.  
  30. User _newUser = User();
  31. List<User> _contacts = [];
  32.  
  33. @override
  34. Widget build(BuildContext context) {
  35. return Scaffold(
  36. appBar: AppBar(
  37. title: Text(widget.title)
  38. ),
  39. body: Column(
  40. children: <Widget>[
  41. Form(
  42. child: Column(
  43. children: <Widget>[
  44. TextFormField(
  45. decoration: InputDecoration(
  46. hintText: 'Nome do contato',
  47. labelText: 'Nome'
  48. ),
  49. onSaved: (value) => setState(() => _newUser.name = value),
  50. ),
  51. TextFormField(
  52. keyboardType: TextInputType.numberWithOptions(),
  53. decoration: InputDecoration(
  54. hintText: '000000000',
  55. labelText: 'Número'
  56. ),
  57. onSaved: (value) => setState(() => _newUser.number = value as int),
  58. ),
  59. RaisedButton(
  60. child: Text('Add'),
  61. onPressed: () => submit()
  62. )
  63. ],
  64. ),
  65. ),
  66. Flexible(child: _buildList())
  67. ],
  68. )
  69. );
  70. }
  71.  
  72. submit() {
  73. setState(() => _contacts.add(_newUser));
  74. }
  75.  
  76. _buildList() {
  77. return ListView.builder(itemBuilder: (context, index){
  78. if (index < _contacts.length) {
  79. return ListTile(
  80. title: Text(_contacts[index].name),
  81. subtitle: Text(_contacts[index].number as String),
  82. );
  83. } //EndIf
  84. }
  85. );
  86. }
  87. }
  88.  
  89. class User {
  90. String name;
  91. int number;
  92.  
  93. }
Add Comment
Please, Sign In to add comment