This repository has been archived on 2024-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
dialer/lib/features/contacts/contact_state.dart
2024-10-18 22:45:35 +03:00

79 lines
2.1 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({Key? key, required this.child}) : super(key: key);
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({Key? key, required this.data, required Widget child})
: super(key: key, child: child);
@override
bool updateShouldNotify(_InheritedContactState oldWidget) => true;
}