From f3b22090d9e4b21507ef4489ed90ff5948a39b74 Mon Sep 17 00:00:00 2001 From: stcb <21@stcb.cc> Date: Wed, 6 Nov 2024 04:18:00 +0200 Subject: [PATCH] Add contact page two upper buttons: share QrCode and tobeimplemented --- lib/features/contacts/contact_state.dart | 1 - .../widgets/alphabet_scroll_page.dart | 176 ++++++++++++++---- pubspec.yaml | 1 + 3 files changed, 137 insertions(+), 41 deletions(-) diff --git a/lib/features/contacts/contact_state.dart b/lib/features/contacts/contact_state.dart index ed29bdc..bf52999 100644 --- a/lib/features/contacts/contact_state.dart +++ b/lib/features/contacts/contact_state.dart @@ -41,7 +41,6 @@ class _ContactStateState extends State { }); } - Future addNewContact(Contact contact) async { await _contactService.addNewContact(contact); await _fetchContacts(); diff --git a/lib/features/contacts/widgets/alphabet_scroll_page.dart b/lib/features/contacts/widgets/alphabet_scroll_page.dart index 37099c3..e772248 100644 --- a/lib/features/contacts/widgets/alphabet_scroll_page.dart +++ b/lib/features/contacts/widgets/alphabet_scroll_page.dart @@ -1,6 +1,7 @@ import 'package:dialer/widgets/username_color_generator.dart'; import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; +import 'package:qr_flutter/qr_flutter.dart'; import '../contact_state.dart'; class AlphabetScrollPage extends StatefulWidget { @@ -8,6 +9,15 @@ class AlphabetScrollPage extends StatefulWidget { final double scrollOffset; const AlphabetScrollPage({Key? key, required this.contacts, required this.scrollOffset}) : super(key: key); + + Contact? getSelfContact() { + for (var contact in contacts) { + if (contact.displayName.toLowerCase() == "user") { + return contact; + } + } + return null; + } @override _AlphabetScrollPageState createState() => _AlphabetScrollPageState(); @@ -30,6 +40,7 @@ class _AlphabetScrollPageState extends State { @override Widget build(BuildContext context) { + // Build the alphabetized contact map Map> alphabetizedContacts = {}; for (var contact in widget.contacts) { String firstLetter = contact.displayName.isNotEmpty ? contact.displayName[0].toUpperCase() : '#'; @@ -41,50 +52,135 @@ class _AlphabetScrollPageState extends State { List alphabetKeys = alphabetizedContacts.keys.toList()..sort(); - return ListView.builder( - controller: _scrollController, - itemCount: alphabetKeys.length, - itemBuilder: (context, index) { - String letter = alphabetKeys[index]; - List contacts = alphabetizedContacts[letter]!; - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Padding( - padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0), - child: Text( - letter, - style: TextStyle( - fontSize: 28, - fontWeight: FontWeight.bold, + return Scaffold( + backgroundColor: Colors.black, + body: Column( + children: [ + // Top buttons row + Container( + color: Colors.black, + padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + // Add Contact Button + IconButton( + icon: Icon(Icons.add, color: Colors.blue), + onPressed: () { + // Show pop-up with two mock choices + showDialog( + context: context, + barrierDismissible: true, // Allows dismissal by tapping outside + builder: (BuildContext context) { + return AlertDialog( + backgroundColor: Colors.black, + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + // Action for Option 1 + }, + child: Text("Option 1", style: TextStyle(color: Colors.white)), + ), + TextButton( + onPressed: () { + Navigator.of(context).pop(); + // Action for Option 2 + }, + child: Text("Option 2", style: TextStyle(color: Colors.white)), + ), + ], + ), + ); + }, + ); + }, ), - ), + // QR Code Button + IconButton( + icon: Icon(Icons.qr_code, color: widget.getSelfContact() != null ? Colors.blue : Colors.grey), + + onPressed: widget.getSelfContact() != null ? () { + showDialog( + barrierColor: Colors.white24, + context: context, + barrierDismissible: true, + builder: (BuildContext context) { + return AlertDialog( + backgroundColor: Colors.black, + content: SizedBox( + width: 200, + height: 220, + child: QrImageView( + data: widget.getSelfContact()!.toVCard(), + version: QrVersions.auto, + backgroundColor: Colors.white, // Ensure QR code is visible on black background + size: 200.0, + ), + ), + ); + }, + ); + }: null, + ) + ], ), - ...contacts.map((contact) { - String phoneNumber = contact.phones.isNotEmpty ? contact.phones.first.number : 'No phone number'; - Color avatarColor = generateColorFromName(contact.displayName); - return ListTile( - leading: (contact.thumbnail != null && contact.thumbnail!.isNotEmpty) - ? CircleAvatar( - backgroundImage: MemoryImage(contact.thumbnail!), - ) - : CircleAvatar( - backgroundColor: avatarColor, - child: Text( - contact.displayName.isNotEmpty ? contact.displayName[0].toUpperCase() : '?', - style: TextStyle(color: Colors.white), + ), + // Contact List + Expanded( + child: ListView.builder( + controller: _scrollController, + itemCount: alphabetKeys.length, + itemBuilder: (context, index) { + String letter = alphabetKeys[index]; + List contacts = alphabetizedContacts[letter]!; + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + // Alphabet Letter Header + Padding( + padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0), + child: Text( + letter, + style: TextStyle( + fontSize: 28, + fontWeight: FontWeight.bold, + color: Colors.white, ), ), - title: Text(contact.displayName), - subtitle: Text(phoneNumber), - onTap: () { - // Handle contact tap - }, - ); - }), - ], - ); - }, + ), + // Contact Entries + ...contacts.map((contact) { + String phoneNumber = contact.phones.isNotEmpty ? contact.phones.first.number : 'No phone number'; + Color avatarColor = generateColorFromName(contact.displayName); + return ListTile( + leading: (contact.thumbnail != null && contact.thumbnail!.isNotEmpty) + ? CircleAvatar( + backgroundImage: MemoryImage(contact.thumbnail!), + ) + : CircleAvatar( + backgroundColor: avatarColor, + child: Text( + contact.displayName.isNotEmpty ? contact.displayName[0].toUpperCase() : '?', + style: TextStyle(color: Colors.white), + ), + ), + title: Text(contact.displayName, style: TextStyle(color: Colors.white)), + subtitle: Text(phoneNumber, style: TextStyle(color: Colors.white70)), + onTap: () { + // Handle contact tap + }, + ); + }), + ], + ); + }, + ), + ), + ], + ), ); } diff --git a/pubspec.yaml b/pubspec.yaml index 94c0f54..f38aaac 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: flutter_contacts: ^1.1.9+2 permission_handler: ^10.2.0 # For handling permissions cached_network_image: ^3.2.3 # For caching contact images + qr_flutter: ^4.1.0 dev_dependencies: flutter_test: