78 lines
2.0 KiB
Dart
78 lines
2.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
|
import 'contact_service.dart';
|
|
|
|
class ContactState extends StatefulWidget {
|
|
final Widget child;
|
|
|
|
const ContactState({super.key, required this.child});
|
|
|
|
static _ContactStateState of(BuildContext context) {
|
|
return context.dependOnInheritedWidgetOfExactType<_InheritedContactState>()!.data;
|
|
}
|
|
|
|
@override
|
|
_ContactStateState createState() => _ContactStateState();
|
|
}
|
|
|
|
class _ContactStateState extends State<ContactState> {
|
|
final ContactService _contactService = ContactService();
|
|
List<Contact> _contacts = [];
|
|
bool _loading = true;
|
|
double _scrollOffset = 0.0;
|
|
|
|
List<Contact> get contacts => _contacts;
|
|
bool get loading => _loading;
|
|
double get scrollOffset => _scrollOffset;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchContacts();
|
|
}
|
|
|
|
Future<void> _fetchContacts() async {
|
|
List<Contact> contacts = await _contactService.fetchContacts();
|
|
contacts = contacts.where((contact) => contact.phones.isNotEmpty).toList();
|
|
contacts.sort((a, b) => a.displayName.compareTo(b.displayName));
|
|
// contacts.sort((a, b) {
|
|
// String aName = a.displayName.isNotEmpty ? a.displayName : '';
|
|
// String bName = b.displayName.isNotEmpty ? b.displayName : '';
|
|
// return aName.compareTo(bName);
|
|
// });
|
|
setState(() {
|
|
_contacts = contacts;
|
|
_loading = false;
|
|
});
|
|
}
|
|
|
|
|
|
Future<void> addNewContact(Contact contact) async {
|
|
await _contactService.addNewContact(contact);
|
|
await _fetchContacts();
|
|
}
|
|
|
|
void setScrollOffset(double offset) {
|
|
setState(() {
|
|
_scrollOffset = offset;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return _InheritedContactState(
|
|
data: this,
|
|
child: widget.child,
|
|
);
|
|
}
|
|
}
|
|
|
|
class _InheritedContactState extends InheritedWidget {
|
|
final _ContactStateState data;
|
|
|
|
const _InheritedContactState({required this.data, required super.child});
|
|
|
|
@override
|
|
bool updateShouldNotify(_InheritedContactState oldWidget) => true;
|
|
}
|