import 'dart:async';
import 'dart:typed_data';
import 'package:fast_contacts/fast_contacts.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
class FastContactList extends StatefulWidget {
const FastContactList({
Key? key,
this.width,
this.height,
}) : super(key: key);
final double? width;
final double? height;
@override
FastContactListState createState() => FastContactListState();
}
class FastContactListState extends State<FastContactList> {
@override
List<Contact> contacts = const [];
String? text;
final ctrl = ScrollController();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
await Permission.contacts.request();
final sw = Stopwatch()..start();
final contacts = await FastContacts.allContacts;
sw.stop();
contacts = contacts;
text = 'Contacts: ${contacts.length}\nTook: ${sw.elapsedMilliseconds}ms';
} on PlatformException catch (e) {
text = 'Failed to get contacts:\n${e.details}';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('fastcontacts'),
),
body: Column(
children: [
Text(_text ?? ''),
Expanded(
child: Scrollbar(
controller: ctrl,
isAlwaysShown: true,
interactive: true,
showTrackOnHover: true,
thickness: 24,
child: ListView.builder(
controller: ctrl,