Skip to content

Instantly share code, notes, and snippets.

@tiagolpadua
Created August 25, 2020 20:25
Show Gist options
  • Save tiagolpadua/693c7a4557f700b3af51d13770204b13 to your computer and use it in GitHub Desktop.
Save tiagolpadua/693c7a4557f700b3af51d13770204b13 to your computer and use it in GitHub Desktop.
Lista de Contatos
import 'package:bytebank/database/dao/contact_dao.dart';
import 'package:bytebank/models/contact.dart';
import 'package:bytebank/screens/contact/form.dart';
import 'package:flutter/material.dart';
class ContactsList extends StatefulWidget {
@override
_ContactsListState createState() => _ContactsListState();
}
class _ContactsListState extends State<ContactsList> {
final ContactDao _dao = ContactDao();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Contacts'),
),
body: FutureBuilder<List<Contact>>(
future:
Future.delayed(Duration(seconds: 1)).then((value) => _dao.findAll()),
builder: (context, snapshot) {
debugPrint('snapshot.connectionState: ${snapshot.connectionState}');
switch (snapshot.connectionState) {
case ConnectionState.none:
break;
case ConnectionState.waiting:
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircularProgressIndicator(),
Text('Loading'),
],
),
);
break;
case ConnectionState.active:
break;
case ConnectionState.done:
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, indice) {
return _ContactItem(snapshot.data[indice]);
},
);
break;
}
return Text('Unkown error');
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => ConctactForm()))
.then((newContact) {
setState(() {});
});
},
child: Icon(Icons.add),
),
);
}
}
class _ContactItem extends StatelessWidget {
final Contact contact;
const _ContactItem(this.contact);
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(
this.contact.name,
style: TextStyle(fontSize: 24),
),
subtitle: Text(
this.contact.accountNumber.toString(),
style: TextStyle(fontSize: 16),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment