Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. //letak package folder flutter
  3. import 'package:flutter_web_sqlite/ui/entryform.dart';
  4. import 'package:flutter_web_sqlite/models/contact.dart';
  5. import 'package:flutter_web_sqlite/dbhelper.dart';
  6. import 'package:sqflite/sqflite.dart';
  7. //untuk memanggil fungsi yg terdapat di daftar pustaka sqflite
  8. import 'dart:async';
  9. import 'package:flutter_stetho/flutter_stetho.dart';
  10. //pendukung program asinkron
  11.  
  12. void main() {
  13. Stetho.initialize();
  14. return runApp(MaterialApp(
  15. debugShowCheckedModeBanner: false,
  16. title: "Kontak App",
  17. home: Home(),
  18. ));
  19. }
  20. class Home extends StatefulWidget {
  21. @override
  22. HomeState createState() => HomeState();
  23. }
  24.  
  25. class HomeState extends State<Home> {
  26.  
  27. DbHelper dbHelper = DbHelper();
  28. int count = 0;
  29. List<Contact> contactList;
  30.  
  31. @override
  32. Widget build(BuildContext context) {
  33. if (contactList == null) {
  34. contactList = List<Contact>();
  35. }
  36.  
  37. return Scaffold(
  38. appBar: AppBar(
  39. title: Text('Kontak App'),
  40. ),
  41. body: createListView(),
  42. floatingActionButton: FloatingActionButton(
  43. child: Icon(Icons.add),
  44. tooltip: 'Tambah Data',
  45. onPressed: () async {
  46. var contact = await navigateToEntryForm(context, null);
  47. if (contact != null) addContact(contact);
  48. },
  49. ),
  50. );
  51. }
  52.  
  53. Future<Contact> navigateToEntryForm(BuildContext context, Contact contact) async {
  54. var result = await Navigator.push(
  55. context,
  56. MaterialPageRoute(
  57. builder: (BuildContext context) {
  58. return EntryForm(contact);
  59. }
  60. )
  61. );
  62. return result;
  63. }
  64.  
  65. ListView createListView() {
  66.  
  67.  
  68. TextStyle textStyle = Theme.of(context).textTheme.subhead;
  69. return ListView.builder(
  70. itemCount: count,
  71. itemBuilder: (BuildContext context, int index) {
  72. return Card(
  73. color: Colors.white,
  74. elevation: 2.0,
  75. child: ListTile(
  76. leading: CircleAvatar(
  77. backgroundColor: Colors.red,
  78. child: Icon(Icons.people),
  79. ),
  80. title: Text(this.contactList[index].name, style: textStyle,),
  81. subtitle: Text(this.contactList[index].phone),
  82. trailing: GestureDetector(
  83. child: Icon(Icons.delete),
  84. onTap: () {
  85. deleteContact(contactList[index]);
  86. },
  87. ),
  88. onTap: () async {
  89. var contact = await navigateToEntryForm(context, this.contactList[index]);
  90. if (contact != null) editContact(contact);
  91. },
  92. ),
  93. );
  94. },
  95. );
  96. }
  97. //buat contact
  98. void addContact(Contact object) async {
  99. int result = await dbHelper.insert(object);
  100. if (result > 0) {
  101. updateListView();
  102. }
  103. }
  104. //edit contact
  105. void editContact(Contact object) async {
  106. int result = await dbHelper.update(object);
  107. if (result > 0) {
  108. updateListView();
  109. }
  110. }
  111. //delete contact
  112. void deleteContact(Contact object) async {
  113. int result = await dbHelper.delete(object.id);
  114. if (result > 0) {
  115. updateListView();
  116. }
  117. }
  118.  
  119. @override
  120. void initState() {
  121. // TODO: implement initState
  122. super.initState();
  123. updateListView();
  124. }
  125. //update contact
  126. void updateListView() {
  127. final Future<Database> dbFuture = dbHelper.initDb();
  128. dbFuture.then((database) {
  129. Future<List<Contact>> contactListFuture = dbHelper.getContactList();
  130.  
  131. contactListFuture.then((contactList) {
  132. if(!mounted) return;
  133. setState(() {
  134. this.contactList = contactList;
  135. for(int i=0; i < contactList.length ; i++) {
  136.  
  137. print("dapat contact ${contactList[i].name}");
  138. }
  139. print("dapat contact ${contactList[0].name}");
  140. this.count = contactList.length;
  141. });
  142. });
  143. });
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement