1
+
2
+ import 'package:flutter/material.dart' ;
3
+ import 'package:flutter_contacts/flutter_contacts.dart' ;
4
+ class ExtractedContacts extends StatefulWidget {
5
+
6
+ List <List <String ?>> list;
7
+ ExtractedContacts ({Key ? key, required this .list}) : super (key: key);
8
+
9
+ @override
10
+ State <ExtractedContacts > createState () => _ExtractedContactsState ();
11
+ }
12
+
13
+ class _ExtractedContactsState extends State <ExtractedContacts > {
14
+ Future <void > saveToPhone (list) async {
15
+ if (await FlutterContacts .requestPermission ()) {
16
+ for (int i = 0 ; i < list[0 ].length; i++ ) {
17
+ final newContact = Contact ()
18
+ ..name.first = list[0 ][i]
19
+ ..phones = [Phone (list[1 ][i])];
20
+ await newContact.insert ();
21
+ }
22
+ print ('Done with it' );
23
+ }
24
+ }
25
+
26
+ String newValue = '' ;
27
+
28
+ @override
29
+ Widget build (BuildContext context) {
30
+ return Scaffold (
31
+ appBar: AppBar (
32
+ title: Text ('CheckList' ,style: TextStyle (color: Colors .brown,fontWeight: FontWeight .bold),),
33
+ actions: [
34
+ TextButton (
35
+ onPressed: () {
36
+ showDialog (
37
+ context: context,
38
+ builder: (context) {
39
+ return AlertDialog (
40
+ content: Text (
41
+ 'Are you sure you want to Save the Cantacts !!' ),
42
+ actions: [
43
+ TextButton (
44
+ onPressed: () {
45
+ Navigator .pop (context);
46
+ },
47
+ child: Text (
48
+ 'Cancel' ,
49
+ style: TextStyle (color: Colors .white),
50
+ ),
51
+ style: TextButton .styleFrom (
52
+ backgroundColor: Colors .grey),
53
+ ),
54
+ TextButton (
55
+ style: TextButton .styleFrom (
56
+ backgroundColor: Colors .brown),
57
+ onPressed: () {
58
+ saveToPhone (widget.list);
59
+ Navigator .pop (context);
60
+ },
61
+ child: Text (
62
+ 'Save' ,
63
+ style: TextStyle (color: Colors .white),
64
+ )),
65
+ ],
66
+ );
67
+ });
68
+ },
69
+ child: Card (
70
+ elevation: 5 ,
71
+ color: Colors .brown,
72
+ child: Container (
73
+ height: 70 ,
74
+ width: 80 ,
75
+ child: Center (
76
+ child: Text (
77
+ 'Save' ,
78
+ style: TextStyle (color: Colors .white,fontWeight: FontWeight .bold,fontSize: 20 ),
79
+ ),
80
+ ),
81
+
82
+ ),
83
+ )),
84
+ ],
85
+ ),
86
+ floatingActionButton: FloatingActionButton (
87
+ backgroundColor: Colors .brown,
88
+ child: Padding (
89
+ padding: const EdgeInsets .all (8.0 ),
90
+ child: Text (
91
+ 'Add Prefix' ,
92
+ style: const TextStyle (color: Colors .white,fontWeight: FontWeight .bold,fontSize: 10 ),
93
+ ),
94
+ ),
95
+ onPressed: () {
96
+ showDialog (
97
+ context: context,
98
+ builder: (context) {
99
+ return AlertDialog (
100
+ title: Text ("Insert Prefix" ),
101
+ content: Column (
102
+ mainAxisSize: MainAxisSize .min,
103
+ children: [
104
+ TextField (
105
+ decoration:
106
+ InputDecoration (hintText: "Add Prefix to Name" ),
107
+ onChanged: (value) {
108
+ setState (() {
109
+ newValue = value;
110
+ });
111
+ },
112
+ ),
113
+ ],
114
+ ),
115
+ actions: [
116
+ TextButton (
117
+ onPressed: () {
118
+ Navigator .pop (context);
119
+ },
120
+ child: Text ("Cancel" )),
121
+ TextButton (
122
+ onPressed: () {
123
+ for (int i = 0 ; i < widget.list[0 ].length; i++ ) {
124
+ widget.list[0 ][i] =
125
+ newValue + ' ' + widget.list[0 ][i]! ;
126
+ }
127
+ setState (() {});
128
+ Navigator .pop (context);
129
+ },
130
+ child: Text ("Save" )),
131
+ ],
132
+ );
133
+ });
134
+ },
135
+ ),
136
+ body: ListView .builder (
137
+ itemCount: widget.list[0 ].length,
138
+ itemBuilder: (context, index) {
139
+ return Padding (
140
+ padding: const EdgeInsets .all (8.0 ),
141
+ child: Container (
142
+ decoration: BoxDecoration (
143
+ border: Border .all (color: Colors .grey),
144
+ borderRadius: BorderRadius .circular (10 ),
145
+ ),
146
+ child: ListTile (
147
+ title: Text (widget.list[0 ][index]! ),
148
+ subtitle: Text (widget.list[1 ][index]! ),
149
+ trailing: Row (
150
+ mainAxisSize: MainAxisSize .min,
151
+ children: [
152
+ IconButton (
153
+ onPressed: () {
154
+ showDialog (
155
+ context: context,
156
+ builder: (context) {
157
+ return AlertDialog (
158
+ title: Text ("Edit Contact" ),
159
+ content: Column (
160
+ mainAxisSize: MainAxisSize .min,
161
+ children: [
162
+ TextField (
163
+ controller: TextEditingController (
164
+ text: widget.list[0 ][index]),
165
+ decoration: InputDecoration (
166
+ hintText: "Enter Name" ),
167
+ onChanged: (value) {
168
+ setState (() {
169
+ widget.list[0 ][index] = value;
170
+ });
171
+ },
172
+ ),
173
+ TextField (
174
+ controller: TextEditingController (
175
+ text: widget.list[1 ][index]),
176
+ decoration: InputDecoration (
177
+ hintText: "Enter Number" ),
178
+ onChanged: (value) {
179
+ setState (() {
180
+ widget.list[1 ][index] = value;
181
+ });
182
+ },
183
+ ),
184
+ ],
185
+ ),
186
+ actions: [
187
+ TextButton (
188
+ onPressed: () {
189
+ Navigator .pop (context);
190
+ },
191
+ child: Text ("Cancel" )),
192
+ TextButton (
193
+ onPressed: () {
194
+ Navigator .pop (context);
195
+ },
196
+ child: Text ("Save" )),
197
+ ],
198
+ );
199
+ });
200
+ },
201
+ icon: Icon (Icons .edit),
202
+ ),
203
+ IconButton (
204
+ onPressed: () {
205
+ setState (() {
206
+ widget.list[0 ].removeAt (index);
207
+ widget.list[1 ].removeAt (index);
208
+ });
209
+ },
210
+ icon: Icon (Icons .delete),
211
+ ),
212
+ ],
213
+ )),
214
+ ),
215
+ );
216
+ },
217
+ ));
218
+ }
219
+ }
0 commit comments