Skip to content

Instantly share code, notes, and snippets.

@edwinmacalopu
Last active February 22, 2021 01:20
Show Gist options
  • Save edwinmacalopu/849894db75d84d4a486e1474cb741ba7 to your computer and use it in GitHub Desktop.
Save edwinmacalopu/849894db75d84d4a486e1474cb741ba7 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
class Addcontact extends StatefulWidget {
Addcontact({Key key}) : super(key: key);
@override
_AddcontactState createState() => _AddcontactState();
}
class _AddcontactState extends State<Addcontact> {
TextEditingController name = TextEditingController();
TextEditingController number = TextEditingController();
Future<dynamic> addcontact() async {
Contact contact = Contact();
contact.phones = [Item(label: "mobile", value: "${number.text}")];
contact.givenName = name.text;
await ContactsService.addContact(contact);
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(30),
child: Center(
child: Column(
children: [
TextField(
controller: name,
keyboardType: TextInputType.text,
),
SizedBox(height: 20),
TextField(
controller: number,
keyboardType: TextInputType.number,
),
SizedBox(height: 20),
RaisedButton(
onPressed: () async {
if (await Permission.contacts.request().isGranted) {
addcontact();
}
},
color: Colors.greenAccent,
child: Text('save contact'),
)
],
)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment