From 7ebcd7eea4e5eb03f86adab57610b17b1f06638e Mon Sep 17 00:00:00 2001 From: stcb <21@stcb.cc> Date: Fri, 8 Nov 2024 11:42:57 +0200 Subject: [PATCH] Separate buttons from alphabet_scroll_page.dart --- .../contacts/widgets/add_contact_button.dart | 43 ++++++++++ .../widgets/alphabet_scroll_page.dart | 81 ++----------------- .../contacts/widgets/share_own_qr.dart | 58 +++++++++++++ lib/main.dart | 2 - 4 files changed, 107 insertions(+), 77 deletions(-) create mode 100644 lib/features/contacts/widgets/add_contact_button.dart create mode 100644 lib/features/contacts/widgets/share_own_qr.dart diff --git a/lib/features/contacts/widgets/add_contact_button.dart b/lib/features/contacts/widgets/add_contact_button.dart new file mode 100644 index 0000000..2f6aa65 --- /dev/null +++ b/lib/features/contacts/widgets/add_contact_button.dart @@ -0,0 +1,43 @@ +import 'package:flutter/material.dart'; + +class AddContactButton extends StatelessWidget { + const AddContactButton({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return 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)), + ), + ], + ), + ); + }, + ); + }, + ); + } +} diff --git a/lib/features/contacts/widgets/alphabet_scroll_page.dart b/lib/features/contacts/widgets/alphabet_scroll_page.dart index e772248..92976e7 100644 --- a/lib/features/contacts/widgets/alphabet_scroll_page.dart +++ b/lib/features/contacts/widgets/alphabet_scroll_page.dart @@ -1,23 +1,16 @@ import 'package:dialer/widgets/username_color_generator.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; -import 'package:qr_flutter/qr_flutter.dart'; import '../contact_state.dart'; +import 'add_contact_button.dart'; +import 'share_own_qr.dart'; class AlphabetScrollPage extends StatefulWidget { final List contacts; 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(); @@ -40,7 +33,6 @@ 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() : '#'; @@ -55,76 +47,15 @@ class _AlphabetScrollPageState extends State { return Scaffold( backgroundColor: Colors.black, body: Column( - children: [ - // Top buttons row + 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, - ) + AddContactButton(), + QRCodeButton(contacts: widget.contacts), ], ), ), diff --git a/lib/features/contacts/widgets/share_own_qr.dart b/lib/features/contacts/widgets/share_own_qr.dart new file mode 100644 index 0000000..e47c7c8 --- /dev/null +++ b/lib/features/contacts/widgets/share_own_qr.dart @@ -0,0 +1,58 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_contacts/contact.dart'; +import 'package:qr_flutter/qr_flutter.dart'; + +class QRCodeButton extends StatelessWidget { + final List contacts; + + const QRCodeButton({Key? key, required this.contacts}) : super(key: key); + + Contact? getSelfContact() { + if (kDebugMode) { + debugPrint("Checking for self contact"); + } + for (var contact in contacts) { + if (contact.groups.isNotEmpty) { + debugPrint("Contact ${contact.displayName} is in groups: ${contact.groups.map((group) => group.name).join(", ")}"); + } + if (contact.groups.any((group) => group.name.toLowerCase() == "user")) { + return contact; + } + } + return null; + } + + @override + Widget build(BuildContext context) { + final selfContact = getSelfContact(); + + return IconButton( + icon: Icon(Icons.qr_code, color: selfContact != null ? Colors.blue : Colors.grey), + onPressed: selfContact != 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: selfContact.toVCard(), + version: QrVersions.auto, + backgroundColor: Colors.white, // Ensure QR code is visible on black background + size: 200.0, + ), + ), + ); + }, + ); + } + : null, + ); + } +} diff --git a/lib/main.dart b/lib/main.dart index 851c5f8..f2752f2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,3 @@ -// This is DEV - import 'package:dialer/features/home/home_page.dart'; import 'package:flutter/material.dart'; import 'package:dialer/features/contacts/contact_state.dart';